A.bakker
A.bakker

Reputation: 241

Altering a control based on a variable name

I got a series of comboboxes that have items that are retrieved from a query. Depending on what the user selects the correct combobox needs to be filled (or updated). So if the user selects "Fish" then "ComboBox_Fish" would need to be changed.

But how can i accomplish this? For testing purposes i tried to change the Text with the following code:

((ComboBox)Controls["ComboBox_" + Name ]).Text = "Test";

but it gives me this error:

System.NullReferenceException: 'Object reference not set to an instance of an object.'

Info that might be relevant: All ComboBoxes are located in the same TableLayoutPanal which is in a TabPage of a TabControl.

Upvotes: 0

Views: 50

Answers (1)

Mong Zhu
Mong Zhu

Reputation: 23732

Info that might be relevant: All ComboBoxes are located in the same TableLayoutPanal which is in a TabPage of a TabControl.

Then you need to access the Controls property of the panel in question

((ComboBox)this.tableLayoutPanel1.Controls["ComboBox_" + Name ]).Text = "Test";

Upvotes: 1

Related Questions