[RESOLVED] [02/03] Convert Variable Contents to Control Name
I have several panels on a form. I get a datareader from a table that gives me the names of the panels on the form (you'll see why in a second).
The dataset writes its data to a infrogistics datagrid, along with other info such as Item ID.
Now, based on the panel name on the selected row, I want to make visible that panel and hide the rest.
I can get the contents of the cell, within which holds the name of the panel I want to display. However, I need to access the Visible property of a control with the variable contents as its name.
For example, the cell contains "Panel1". I need to do this
Code:
Panel1.Visible = True
To hide the remaining panels I can do a For Each loop.
Unfortunately a Select Case statement won't do because the whole point of the exercise is to not include hard-coded control names.
The panels can be added any time with different data on them, but I need to only put the new panel on the form, and an entry in the database for the Item ID that tell the app which Panel to show.
Re: [02/03] Convert Variable Contents to Control Name
vb.net Code:
Me.Controls(controlNameString).Visible = True
Re: [02/03] Convert Variable Contents to Control Name
Me.Controls(controlNameString).Visible = True would equate to Me.Controls("Panel1").Visible = True
The error message I get paraphrased is "Expected Integer, not a String".
Re: [02/03] Convert Variable Contents to Control Name
Code:
Dim ctl As Control
Dim newstr As String = "Panel2" ' from cell
For Each ctl In Me.Controls
If (TypeOf (ctl) Is Panel) Then
If ctl.Name = newstr Then ctl.Visible = True
Exit For
End If
Next
This compare each control's name against the string in the variable. If it matches, then it sets the visible property. Sometimes you just gotta ask the question to be able to think it through all the way!
Re: [02/03] Convert Variable Contents to Control Name
Ah, sorry. That overload of ControlCollection.Item was added in .NET 2.0.
Re: [RESOLVED] [02/03] Convert Variable Contents to Control Name
Actually it should be:
Code:
Dim ctl As Control
Dim newstr As String = "Panel2" ' from cell
For Each ctl In Me.Controls
If (TypeOf (ctl) Is Panel) Then
If ctl.Name = newstr Then
ctl.Visible = True
Exit For
End If
End If
Next
This prevents the first panel on the form (regardless of its name) to hit the Exit For thus preventing the proper panel from becoming visible.