Results 1 to 6 of 6

Thread: [RESOLVED] [02/03] Convert Variable Contents to Control Name

  1. #1

    Thread Starter
    Hyperactive Member rjbudz's Avatar
    Join Date
    Jul 2005
    Location
    San Diego
    Posts
    262

    Resolved [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.

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [02/03] Convert Variable Contents to Control Name

    vb.net Code:
    1. Me.Controls(controlNameString).Visible = True
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Hyperactive Member rjbudz's Avatar
    Join Date
    Jul 2005
    Location
    San Diego
    Posts
    262

    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".

  4. #4

    Thread Starter
    Hyperactive Member rjbudz's Avatar
    Join Date
    Jul 2005
    Location
    San Diego
    Posts
    262

    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.

  5. #5
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [02/03] Convert Variable Contents to Control Name

    Ah, sorry. That overload of ControlCollection.Item was added in .NET 2.0.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  6. #6

    Thread Starter
    Hyperactive Member rjbudz's Avatar
    Join Date
    Jul 2005
    Location
    San Diego
    Posts
    262

    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
  •  



Click Here to Expand Forum to Full Width