How to get both dynamic numericupdown and textbox values in c# windows forms

99封情书 提交于 2019-12-25 08:58:32

问题


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.

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

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

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


回答1:


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

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.



来源:https://stackoverflow.com/questions/39166868/how-to-get-both-dynamic-numericupdown-and-textbox-values-in-c-sharp-windows-form

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!