简体   繁体   English

动态创建 winform 控件

[英]Creating winforms controls dynamically

I am learning c#.我正在学习 C#。 I want to create some controls dynamically.我想动态创建一些控件。 Here is the code that I am trying to create new elements on form dynamically, but it doesn't do anything.这是我试图在表单上动态创建新元素的代码,但它没有做任何事情。 Please help me to solve this problem.请帮我解决这个问题。

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]);
            }
        }
    }
}

You're adding all of the controls on top of each other, which is why it looks like there is only one of them. 您将所有控件添加到彼此之上,这就是为什么它看起来只有一个。 You'll want to put them in some sort of layout based control/panel (such as a FlowLayoutPanel ) that will automatically place the structures in the appropriate location based on the type of layout that you want. 您需要将它们放在某种基于布局的控件/面板(例如FlowLayoutPanel )中,该控件/面板将根据您需要的布局类型自动将结构放置在适当的位置。

I just wrote a fast C# project in my visual studio. 我刚刚在我的视觉工作室写了一个快速的C#项目。 The code below adds a new textbox to the form. 下面的代码在表单中添加了一个新的文本框。

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);
        }
    }
}

------------------------------------------ADDED------------------------------------- - - - - - - - - - - - - - - - - - - - - - 添加 - - - - ------------------------------

The code below, will add 4 textboxes and 4 labels, like you want it. 下面的代码将添加4个文本框和4个标签,就像您想要的那样。 You can see the picture(at the end of the code), how it shows on my example. 您可以看到图片(在代码的末尾),它在我的示例中的显示方式。

Ps: You will need to configure the position properly though. Ps:您需要正确配置位置。

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;
            }

        }
    }
}

Screenshot: http://shrani.si/f/1F/Y/22BgTuBX/example.png 截图: http//shrani.si/f/1F/Y/22BgTuBX/example.png

I also took the time and uploaded the project i made to Sendspace. 我也花时间把我上传的项目上传到Sendspace。

Download link: http://www.sendspace.com/file/glc7h2 下载链接: http//www.sendspace.com/file/glc7h2

The Textboxes and Labels are placed on top of each other, since you didn't specify a location for them. 文本框和标签彼此重叠,因为您没有为它们指定位置。

Change your code to: 将您的代码更改为:

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]);
            }
        }
     }
}

Textboxes and Labels will be shown in 2 columns, adding 25 to their Y values every row. 文本框和标签将显示在2列中,每行添加25个Y值。

The array creation call just initializes the elements to null. 数组创建调用只是将元素初始化为null。 You need to individually create them. 您需要单独创建它们。 Try This 试试这个

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;
}

Yes first define where you want to add the control in the page and add the control on that main control. 是首先定义要在页面中添加控件的位置,并在该主控件上添加控件。 Example page have one panel and it's name is pnl1 so use like below 示例页面有一个面板,它的名称是pnl1,所以请使用如下所示

pnl1.Controls.Add(textBox[i]);
pnl1.Controls.Add(label[i]);
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);

        }

    }
}

Have the same problem.有同样的问题。 Help me.帮我。


声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM