哪个控件最适合在Windows窗体应用程序中添加文本和单选按钮

本文关键字:应用程序 添加 文本 单选按钮 窗体 Windows 控件 | 更新日期: 2023-09-27 18:18:00

我想要显示问题和选项,对于每个选项,应该添加单选按钮,并且应该在数字圈中添加问题编号。建议我如何做这件事

        public Form2(string paperid)
    {
        InitializeComponent();
        if (paperid != "")
        {
            var papers = doc.Descendants("paper");
            foreach (var paper in papers)
            {
                if (paper.Attribute("id").Value == paperid)
                {
                    var questions = paper.Descendants("question");
                    foreach (var question in questions)
                    {
                        Label ques = new Label();
                        ques.Text = question.Attribute("ques").Value;
                        this.Controls.Add(ques);
                        var options = question.Descendants("option");
                        var i = 0;
                        foreach (var option in options)
                        {
                            RadioButton rdbtn = new RadioButton();
                            rdbtn.Name = "rdbtn" + i;
                            this.Controls.Add(rdbtn);
                            rdbtn.Text = option.Value;
                            i++;
                        }
                    }
                    break;
                }
            }

        }
    }

哪个控件最适合在Windows窗体应用程序中添加文本和单选按钮

在评论中,你似乎不知道什么是Control。每个人都告诉你要用LabelRadioButton,那就照做吧。

在VS设计器中,从工具箱中选择Label并将其拖动到窗体中。改变它的一些属性,然后BOOM!你做到了!

在评论中,你说你需要在Label中"存储"文本。在某些情况下,这可能是错误的。您将文本存储在字符串中,而不是标签中。后一个显示文本。

您还提到了如何从XML获取文本。但这无关紧要,你可以把从XML得到的文本存储在一个字符串中,我们称它为text。然后更改标签的Text属性。

label.Text = text;

现在你的标签将显示文本。

编辑

让我假设你没有使用VisualStudio。您也可以通过代码来实现这一点。首先,您需要创建一个Form

Form form = new Form();
form.Show();
//set properties of the form
Label label = new Label();
//set properties of the label. E.g. Text, width, position etc
form.Controls.Add(label);

创建标签后,使用编辑前的代码设置文本,现在你的标签应该出现在表单上了

 private void button1_Click(object sender, EventArgs e)
    {
        if (id != "")
        {
            var papers = doc.Descendants("paper");
            foreach (var paper in papers)
            {
                if (paper.Attribute("id").Value == id)
                {
                    var questions = paper.Descendants("question");
                    var j = 1;
                    foreach (var question in questions)
                    {
                       GroupBox Ques&Ansoptn = new GroupBox();
                       Ques&Ansoptn.Size = new System.Drawing.Size(720, 120);
                       Ques&Ansoptn.Text = question.Attribute("ques").Value;
                       Ques&Ansoptn.Location = new Point(15, 40*j);
                       Ques&Ansoptn.Font = new Font("Microsoft Sans Serif", 10);
                        this.Controls.Add(Ques&Ansoptn);
                        var options = question.Descendants("option");
                        var i =1;
                        foreach (var option in options)
                        {
                            RadioButton rdbtn = new RadioButton();
                            rdbtn.Size = new System.Drawing.Size(400, 20);
                            rdbtn.Location = new Point(20, 20 * i);
                            rdbtn.Font = new Font("Microsoft Sans Serif", 10);
                            rdbtn.Text = option.Value;
                            Ques&Ansoptn.Controls.Add(rdbtn);
                            i++;
                        }
                        j = j+3;
                    }
                    break;
                }
            }

        }
    }