动态创建winforms控件

8

我正在学习C#,想要动态创建一些控件。这是我尝试在表单上动态创建新元素的代码,但它什么也没做。请帮助我解决这个问题。

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

    namespace Sampless
    {
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        int n = 4;

        private void btnDisplay_Click(object sender, EventArgs e)
        {
            TextBox[] textBox = new TextBox[n];
            Label[] label = new Label[n];

            for (int i = 0; i < n; i++)
            {
                textBox[i] = new TextBox();
                textBox[i].Name = "n" + i;
                textBox[i].Text = "n" + i;

                label[i] = new Label();
                label[i].Name = "n" + i;
                label[i].Text = "n" + i;
            }

            for (int i = 0; i < n; i++)
            {
                this.Controls.Add(textBox[i]);
                this.Controls.Add(label[i]);
            }
        }
    }
}

这似乎是一个重复器的工作。你能否使用控件的通用列表并将其绑定到重复器? - Tim
可能是重复问题,参考链接:https://dev59.com/L2DVa4cB1Zd3GeqPfaiJ - DoIt
请提供一个更好的问题陈述,而不是“它什么也不做”。 - Dour High Arch
6个回答

11
你正在将所有控件叠放在一起,这就是为什么它看起来只有一个的原因。你需要将它们放置在一些基于布局的控件或面板(例如FlowLayoutPanel)中,它会根据所需的布局类型自动将结构放置在适当的位置。

7

我刚用Visual Studio写了一个快速的C#项目。下面的代码向表单添加了一个新的文本框。

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication3
{
    public partial class Form1 : Form
    {
        TextBox txtBox;


        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            txtBox = new TextBox();
            txtBox.Location = new Point(10, 50);
            txtBox.Visible = true;
            Controls.Add(txtBox);
        }
    }
}

下面的代码将添加4个文本框和4个标签,就像你想要的那样。您可以看到代码末尾的图片,它展示了我的示例。

提示:您需要正确配置位置。

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication3
{
    public partial class Form1 : Form
    {
        TextBox[] txtBox;
        Label[] lbl;

        int n = 4;
        int space = 20;

        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            txtBox = new TextBox[n];
            lbl = new Label[n];

            for (int i = 0; i < n; i++)
            {
                txtBox[i] = new TextBox();
                txtBox[i].Name = "n" + i;
                txtBox[i].Text = "n" + i;

                lbl[i] = new Label();
                lbl[i].Name = "n" + i;
                lbl[i].Text = "n" + i;
            }


            for (int i = 0; i < n; i++)
            {
                txtBox[i].Visible = true;
                lbl[i].Visible = true;
                txtBox[i].Location = new Point(40, 50 + space);
                lbl[i].Location = new Point(10, 50 + space);
                this.Controls.Add(txtBox[i]);
                this.Controls.Add(lbl[i]);
                space += 50;
            }

        }
    }
}

截图:http://shrani.si/f/1F/Y/22BgTuBX/example.png 我还花时间将制作的项目上传到了Sendspace。
下载链接:http://www.sendspace.com/file/glc7h2

但它没有添加4个文本框和4个标签。尝试运行原帖中的代码,看看他的问题出在哪里。 - Servy
你是对的。因此,我创建了一个新项目,其中包含他正在寻找的解决方案。至少我希望这就是他正在寻找的东西! :) - MiKE
你几乎肯定不想手动指定位置。这会导致表单无法处理动态大小的控件(这是非常普遍的情况),无法调整大小等。即使所有内容的大小都是静态的,尝试正确对齐所有内容也是一项繁重的工作。你几乎肯定要使用某种布局管理器。 - Servy
提问,获取答案,无干扰。这个网站的重点在于获取答案。它不是一个讨论论坛。没有闲聊。接受并不意味着它是最好的答案,它只是意味着对提问者有效。我知道你的意思,但是他正在询问如何动态创建控件。我的答案应该解决他所问的问题。你所说的是另一个与他的问题无关的话题。 - MiKE
2
该网站旨在创建高质量的答案,而不是任何老旧的答案。用户通过投票和评论来指示答案是否高质量是期望的。用户不应忽略低质量的答案,即使您的解决方案可以回答问题,但它并不是一个很好的答案,并且在此过程中会产生许多新问题。这是非常相关的,在该网站上发布指示的评论是完全适当的。考虑到您在这里只有一天,而我已经在这里两年了,我认为我对该网站的运作有更好的理解。 - Servy
1
我真的不知道是谁把我的答案踩了。我发布了一个正确的解决方案来解决他的问题。它可能不是最好的,但它按照问题所要求的方式工作。 :( - MiKE

1

首先确定您要在页面上添加控件的位置,并将控件添加到该主控件上。 例如,页面有一个面板,它的名称为pnl1,因此可以像下面这样使用:

pnl1.Controls.Add(textBox[i]);
pnl1.Controls.Add(label[i]);

1
文本框和标签被放置在彼此之上,因为您没有为它们指定位置。
将您的代码更改为:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

    namespace Sampless
    {
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        int n = 4;

        private void btnDisplay_Click(object sender, EventArgs e)
        {
            TextBox[] textBox = new TextBox[n];
            Label[] label = new Label[n];
            int labelX, labelY, textboxX, textboxY;

            labelX = 20;
            labelY = 20;
            textboxX = 50;
            textboxY = 20;

            for (int i = 0; i < n; i++)
            {
                textBox[i] = new TextBox();
                textBox[i].Name = "n" + i;
                textBox[i].Text = "n" + i;
                textBox[i].Location = new Point(textboxX, textboxY);

                label[i] = new Label();
                label[i].Name = "n" + i;
                label[i].Text = "n" + i;
                label[i].Location = new Point(labelX, labelY);

                labelY += 25;
                textboxY += 25;
            }

            for (int i = 0; i < n; i++)
            {
                this.Controls.Add(textBox[i]);
                this.Controls.Add(label[i]);
            }
        }
     }
}

文本框和标签将显示在两列中,每行的Y值增加25。

你几乎肯定不想手动指定位置。这会导致表单无法处理动态大小的控件(这是非常普遍的情况),无法调整大小等。即使所有内容的大小都是静态的,尝试正确对齐所有内容也是一项繁重的工作。你几乎肯定要使用某种布局管理器。 - Servy
@Servy 我明白,但这是为了解决用户的具体问题而提供的答案,通过向他展示出错的地方。下一步应该是正确对齐标签和文本框,但这应该由他自己来处理。 - Max
这是一个试图解决所提出的问题而创造了许多新问题的答案。 这是一种我断言根本不应该使用来解决问题的方法。OP不应该必须自己花费大量时间来正确布置控件。他应该使用其中之一旨在为他安排它们的布局面板。 这将极其容易更加有效。 这样的解决方案绝不需要接触Location属性。 - Servy

0
namespace Sample
{
    public partial class Form1 : Form
    {

        public Form1()
        {
            InitializeComponent();
        }
        TextBox txbx = new TextBox();
        private void button1_Click(object sender, EventArgs e)
        {
            AddNewTextBox();

            txbx = new TextBox();

            txbx.Location = new Point(10, 20);

            txbx.Visible = true;

            Controls.Add(txbx);

        }

    }
}

这也在运行... 动态创建一个新的文本框控件 ... - Dinesh D

0
数组创建调用只是将元素初始化为null。您需要逐个创建它们。 尝试这个。
TextBox[] txtTeamNames = new TextBox[teams];
for (int i = 0; i < txtTeamNames.Length; i++) {
  var txt = new TextBox();
  txtTeamNames[i] = txt;
  txt.Name = name;
  txt.Text = name;
  txt.Location = new Point(172, 32 + (i * 28));
  txt.Visible = true;
}

1
你几乎肯定不想手动指定位置。这会导致表单无法处理动态大小的控件(这是非常普遍的),无法调整大小等。即使所有内容的大小都是静态的,尝试正确对齐所有内容也需要大量工作。你几乎肯定要使用某种布局管理器。 - Servy

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接