使用C#动态文本框输入

时间:2016-10-11 02:00:14

标签: button dynamic textbox

使用C#,我使用Windows窗体拖放一个按钮,并动态创建一个按钮和文本框。我想显示按钮上文本框的输入。硬编码按钮工作正常,但动态按钮使用System.NullReferenceException崩溃程序。

请帮助我学习如何通过点击动态创建的按钮从动态创建的文本框中获取输入。

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

namespace WindowsFormsApplication8
{
public partial class Form1 : Form
{
    Button btnDynamic;
    TextBox txtBoxYear;
    public Form1()
    {
        InitializeComponent();
        tp();

    }

    private void Form1_Load(object sender, EventArgs e)
    {
        Button btn = (Button)sender;
        btnHardCode.Text = txtBoxYear.Text;       //This hard coded button works
       // btnDynamic.Text = txtBoxYear.Text;   //The dynamic button crashes the program with a 
                                            //System.NullReferenceException
    }
    private void tabPage1_Click(object sender, EventArgs e)
    {

    }

    private void tp()  //put textbox and button on tab on tab click
    {
        var tab = tabControl1.TabPages[0];
        //tab.Controls.Clear();
        var btnDynamic = new Button()
        {
            Size = new Size(75, 30),
            Left = 40,
            Top = 50,
            Text = "try",
        };
        btnDynamic.Click += new EventHandler(this.Form1_Load);
        tab.Controls.Add(btnDynamic);
        txtBoxYear = new TextBox()
        {
            Size = new Size(200, 100),
            Left = 20,
            Top = 10,
        };
        tab.Controls.Add(txtBoxYear);

    }

    private void button1_Click(object sender, EventArgs e)
    {

    }
}

}

1 个答案:

答案 0 :(得分:0)

我想出了如何通过点击动态创建的按钮从动态创建的文本框中获取输入。首先,在类级别声明按钮的名称。其次,在实例化按钮对象时,不要像上面那样使用var关键字。像这样实例化:     btnDynamic = new Button()

以下是正常运作的代码:

using System;
using System.Drawing;
using System.Windows.Forms;

namespace dynamicButton
{
    public partial class Form1 : Form
    {
        TextBox txtBoxYear;
        Button btnDynamic;

        public Form1()
        {
            InitializeComponent();
            tp();
        }


        private void Form1_Load(object sender, EventArgs e)
        {
                Button btn = (Button)sender;
                btnHardCode.Text = txtBoxYear.Text;
                btnDynamic.Text = txtBoxYear.Text;         
        }
        private void tabPage1_Click(object sender, EventArgs e)
        {

        }

        private void tp()  //put textbox and button on tab on tab click
        {
            var tab = tabControl1.TabPages[0];
            //tab.Controls.Clear();
            btnDynamic = new Button()
            {
                Size = new Size(100, 30),
                Left = 40,
                Top = 50,
                Text = "DynamicButton",
            };
            btnDynamic.Click += new EventHandler(this.Form1_Load);
            tab.Controls.Add(btnDynamic);
            txtBoxYear = new TextBox()
            {
                Size = new Size(200, 100),
                Left = 20,
                Top = 10,
            };
            tab.Controls.Add(txtBoxYear);

        }

        private void btnHardCode_Click(object sender, EventArgs e)
        {

        }
    }
}