动态创建winforms控件

Muh*_*ail 5 c# winforms

我正在学习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]);
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

Ser*_*rvy 7

您将所有控件添加到彼此之上,这就是为什么它看起来只有一个.您需要将它们放在某种基于布局的控件/面板(例如a FlowLayoutPanel)中,它将根据您需要的布局类型自动将结构放置在适当的位置.


MiK*_*iKE 7

我刚刚在我的视觉工作室中编写了一个快速的 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);
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

- - - - - - - - - - - - - - - - - - - - - 添加 - - - - ------------------------------------------

下面的代码将添加 4 个文本框和 4 个标签,如您所愿。您可以看到图片(在代码末尾),它在我的示例中是如何显示的。

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

        }
    }
}
Run Code Online (Sandbox Code Playgroud)

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

我还花时间把我做的项目上传到Sendspace。

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

  • 该网站旨在创建高质量的答案,而不是任何旧的答案。当答案质量不高时,“期望”用户通过投票和评论来指出。“不”希望用户忽略低质量的答案。您的解决方案可能会回答该问题,但这不是一个很好的答案,并且会在此过程中产生许多新问题。这是高度相关的,并且发表评论表明这在本网站上是完全合适的。鉴于你已经在这里呆了一天,而我已经在这里呆了两年,我想说我对这个网站的运作方式有了更好的了解。 (2认同)