|
-
Nov 6th, 2007, 06:18 PM
#1
Thread Starter
Hyperactive Member
[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.
-
Nov 6th, 2007, 06:23 PM
#2
Re: [02/03] Convert Variable Contents to Control Name
vb.net Code:
Me.Controls(controlNameString).Visible = True
-
Nov 6th, 2007, 06:32 PM
#3
Thread Starter
Hyperactive Member
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".
-
Nov 6th, 2007, 06:48 PM
#4
Thread Starter
Hyperactive Member
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!
Last edited by rjbudz; Nov 6th, 2007 at 07:00 PM.
-
Nov 6th, 2007, 06:50 PM
#5
Re: [02/03] Convert Variable Contents to Control Name
Ah, sorry. That overload of ControlCollection.Item was added in .NET 2.0.
-
Nov 6th, 2007, 07:39 PM
#6
Thread Starter
Hyperactive Member
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.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|