Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Generic way to get the values from multiple controls

I have a Form with multiple different controls like ComboBox, TextBox and CheckBox. I am looking for a generic way to get values from these controls while looping over them.

For example, something like this:

foreach(Control control in controls)
{
    values.Add(control.Value);
}

Is it possible or do I need to treat each control separately?

like image 404
mooper Avatar asked Dec 29 '25 00:12

mooper


1 Answers

Try this:

Panel myPanel = this.Panel1;

List<string> values = new List<string>();

foreach (Control control in myPanel.Controls)
{
    values.Add(control.Text);
}

But make sure you get only the controls you want. You can check the type just like

if(control is ComboBox)
{
    // Do something
}
like image 139
GameScripting Avatar answered Dec 31 '25 12:12

GameScripting



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!