如何在C#Windows窗体中同时获取动态numericupdown和文本框值 [英] How to get both dynamic numericupdown and textbox values in c# windows forms

查看:307
本文介绍了如何在C#Windows窗体中同时获取动态numericupdown和文本框值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个代码,其中TextBox和NumericUpDown工具是通过foreach循环动态生成的.现在,我希望能够获得两个值,在每次迭代中,这两个值都将被添加到数据库的同一行中.我有用于获取NumericUpDown值的代码,我想知道如何也可以实现获取TextBox值.

I have a code where TextBox and NumericUpDown tools are generated dynamically through a foreach loop. Now, I want to be able to get both values in which with each iteration, both their values would be added to the same row in the database. I have the code for getting the NumericUpDown values and I was wondering how can I implement getting the TextBox values too.

用于生成动态NumericUpDown和TextBox的代码

Code for generating the dynamic NumericUpDown and TextBox

t = temp.Split(';'); // e.g. t = Arial;32

int pointX = 70;
int pointY = 80;

int pointA = 300;
int pointB = 80;

for (int i = 0; i < N; i++)
{

    foreach (object o in t) 
    {
        TextBox a = new TextBox();
        a.Text = o.ToString();
        a.Location = new Point(pointX, pointY);
        a.Size = new System.Drawing.Size(150, 25);

        panel.Controls.Add(a);
        panel.Show();
        pointY += 30;

        NumericUpDown note = new NumericUpDown();
        note.Name = "Note" + i.ToString();
        note.Location = new Point(pointA, pointB);
        note.Size = new System.Drawing.Size(40, 25);
        note.Maximum = new decimal(new int[] { 10, 0, 0, 0 });
        panel.Controls.Add(note);
        pointB += 30;
    }
 }

在单击按钮时获取动态NumericUpDown值的代码(数据库代码有效)

Code for getting dynamic NumericUpDown values on button click (the database code works)

foreach (NumericUpDown ctlNumeric in panel.Controls.OfType<NumericUpDown>())
        {
            var value = ctlNumeric.Value;
            int number = Decimal.ToInt32(value);
            sample_save = "INSERT INTO forthesis (testVal) VALUES ('" + number + "');";
            MySqlConnection myConn = new MySqlConnection(connectionString);
            MySqlCommand myCommand = new MySqlCommand(sample_save, myConn);
            MySqlDataAdapter da = new MySqlDataAdapter(myCommand);
            MySqlDataReader myReader;
            myConn.Open();
            myReader = myCommand.ExecuteReader();
            myConn.Close();
            MessageBox.Show(number.ToString());
        }

样本输出,每行将插入数据库中

Sample output, each row would be inserted in the database

我如何也可以在TextBox中获取值?您的帮助将不胜感激.非常感谢!!!

How can I get the values in the TextBox(es) too? Your help will be much appreciated. Thank you so much!!!

推荐答案

也许您可以使用字符串-TextBox字典来保存匹配的配对文本框,如下所示:

Maybe you could use a string - TextBox dictionary to save the matching paired text box like this :

Dictionary<string, TextBox> textboxes = new Dictionary<string, TextBox>();
for (int i = 0; i < N; i++)
{
    foreach (object o in t) 
    {
        TextBox a = new TextBox();
        .....

        NumericUpDown note = new NumericUpDown();
        note.Name = "Note" + i.ToString();
        ......

        textboxes.Add(note.Name, a);
    }
}

foreach (NumericUpDown ctlNumeric in panel.Controls.OfType<NumericUpDown>())
{
    var value = ctlNumeric.Value;
    int number = Decimal.ToInt32(value);

    TextBox tb = textboxes[ctrlNumeric.Name];
    String text = tb.Text;
    .........
}

如果要删除文本框& NumericUpDowns-那么您也需要清除Dictionary-否则它将保留对它们的引用&垃圾收集器将不会清理它们.

If you are removing the TextBoxs & NumericUpDowns - then you will need to clear the Dictionary as well - otherwise it will retain references to them & they will not be cleaned up by the Garbage Collector.

这篇关于如何在C#Windows窗体中同时获取动态numericupdown和文本框值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆