如何从动态创建的文本框中获取数据?

时间:2016-11-18 08:44:51

标签: c# winforms

这是my previous question的后续问题。

然而,在这个问题中,我创建了这样一个Control,所以每当我点击按钮时,就会出现两个TextBox es(无限期计数/点击)。每当我再次输入一个值时,他们会进行添加并将结果放入其行的第三个TextBox

   private void buttonNewRow_Click(object sender, EventArgs e)
        {
            int count = 1;
            DateTimePicker date = new DateTimePicker();
            count = panel1.Controls.OfType<DateTimePicker>().ToList().Count;
            date.Location = new Point(0, 15 * count);
            date.Size = new Size(91, 20);
            panel1.Controls.Add(date);

           //Textbox 1
            TextBox textboxTranspo = new TextBox();
            textboxTranspo.Location = new Point(576, 45 * count);
            textboxTranspo.Size = new Size(81, 20);
            panel1.Controls.Add(textboxTranspo);

            //Textbox 2
            TextBox textboxDaily = new TextBox();
            textboxDaily.Location = new Point(663, 45 * count);
            textboxDaily.Size = new Size(81, 20);
            panel1.Controls.Add(textboxDaily);

            //Textbox 1 + Textbox 2 result
            TextBox textboxTotal = new TextBox();
            textboxTotal.Location = new Point(772, 45 * count);
            textboxTotal.Size = new Size(100, 20);
            panel1.Controls.Add(textboxTotal);
}

我该如何做到这一点?

2 个答案:

答案 0 :(得分:0)

我将在上面作为答案解释我的评论。

首先,您必须定义UserControl并将其命名为您喜欢的名称,但请将其命名为Uc1

在设计器中将datetimepicker和3 textboxes拖到Uc1。假设我们为dt1命名datetimepicker,为txtTranspo命名为textbox1,为txtDaily命名为textbox2,为最后一个命名为txtTotal 。由于我们需要计算txtTranspotxtDaily的值,为了简单起见,我想为计算逻辑添加1 button名为btnCompute

然后在Uc1

class Uc1
{

    public Uc1() 
    {
        InitializeComponent();
        this.wireEvents();
    }


    // This will handle all controls events for well organized form sakes.
    private void wireEvents()
    {
        this.btnCompute.Click += OnBtnComputeEvent;
    }


    private void OnBtnComputeEvent(object sender, EventArgs e)
    {
        // i used int since i do not know exactly how to do this.
        int transpo = Int32.Parse(this.txtTranspo.Text);
        int daily = Int32.Parse(this.txtDaily.Text);
        double total = ((transpo + daily) / 100 * 100) // <--- this is just a random computation.

        this.txtTotal.Text = string.Format("{0:c}", total); // this will format the total to a currency.
    }

}

然后构建解决方案。或单击Ctrl + Shift + B的组合。在此之后,Uc1将作为新控件显示在toolbox

然后你可以根据自己的逻辑来做这件事。

private void buttonNewRow_Click(object sender, EventArgs e)
{
        Uc1 c = new Uc1();
        c.Dock = DockStyle.Top;
        c.BringToFront();

        this.panel.Controls.Add(c);
}

你很高兴。

答案 1 :(得分:0)

您可以添加如下事件处理程序:

private void buttonNewRow_Click(object sender, EventArgs e)
{
    DateTimePicker date = new DateTimePicker();
    int count = panel1.Controls.OfType<DateTimePicker>().ToList().Count;
    date.Location = new Point(0, 15 * count);
    date.Size = new Size(91, 20);
    panel1.Controls.Add(date);

    //Textbox 1
    TextBox textboxTranspo = new TextBox();
    textboxTranspo.Location = new Point(576, 45 * count);
    textboxTranspo.Size = new Size(81, 20);
    panel1.Controls.Add(textboxTranspo);

    //Textbox 2
    TextBox textboxDaily = new TextBox();
    textboxDaily.Location = new Point(663, 45 * count);
    textboxDaily.Size = new Size(81, 20);
    panel1.Controls.Add(textboxDaily);

    //Textbox 1 + Textbox 2 result
    TextBox textboxTotal = new TextBox();
    textboxTotal.Location = new Point(772, 45 * count);
    textboxTotal.Size = new Size(100, 20);
    panel1.Controls.Add(textboxTotal);

    EventHandler textChanged = (o, e) => {
        int intTranspo, intDaily;
        if (int.TryParse(textboxTranspo.Text, out intTranspo)
         && int.TryParse(textboxDaily.Text, out intDaily))
            textboxTotal.Text = (intTranspo + intDaily).ToString();
    };
    textboxTranspo.TextChanged += textChanged;
    textboxDaily.TextChanged += textChanged;
}