How to access a control by its (variable) name?

Please support our VB.NET advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved

Join Date: Jun 2008
Posts: 3
Reputation: FastCat is an unknown quantity at this point 
Solved Threads: 0
FastCat FastCat is offline Offline
Newbie Poster

How to access a control by its (variable) name?

 
0
  #1
Jun 5th, 2008
My first vb.net post...

What I've got:

On a form, I have 'Add New Tab' button, a 'Select Picture' button and a TabControl.

On the first Tab I have a textbox1and a picturebox1, when the user uses the 'Select picture' button (which calls opentdialog) I load the picture into the picture box and put the selected filename into the text box. – so far so good.

However, I want the user to be able to add as many Pictures as they want so what I've done is:
I dynamically add a new tab every time the user presses the 'Add new Tab', and dynamically create new text box and picture box on each new tab. I call (name) them by the number of total tabs I have, so the textbox and picturebox on tab2 will be: textbox2 and a picturebox2 , the textbox and picturebox on tab3 will be: textbox3 and a picturebox3 etc.

My problem:
When I get the selected filename , I wish to populate the textbox dependent upon which tab is currently selected.
So how to I refer to the control (in this case my textbox) by name.




My 'new' button does this:

CreateNewImageTab(tcImages.TabCount + 1)
Here's my routine where I create the new tab, picturebox:

    
Sub CreateNewImageTab(ByVal iImage As Integer)

        Dim sImageId As String
        Dim sImageN As String
        Dim sNewtabName As String


        'New Image Id
        sImageId = CStr(iImage)
        sImageN = "Image" & sImageId

        ' New tab
        Dim newtabpage As New TabPage
        sNewtabName = "tab" & sImageN

        ' New Picture box
        Dim ImagePicturebox As New PictureBox
        Dim sNewImageboxName As String = "pb" & sImageN

       
        '
        ' New tab
        '
        frmMain.tcImages.Controls.Add(newtabpage)

       ' New Picture box
        newtabpage.Controls.Add(ImagePicturebox)

 End Sub



My 'Select' button does this: ie gets the selected file name.

        
If Me.OpenFileDialogImages.ShowDialog() = System.Windows.Forms.DialogResult.OK Then
            LoadImage(Me.OpenFileDialogImages.FileName)
        End If

I just want to know how to refere to the objects on the currently selected tab, by name in my loadimage routine

Sub LoadImage(ByVal sFilename As String)

       Dim iImage As Integer
        Dim FilenameboxName As String
        Dim PictureBoxName As String

        iImage = frmMain.tcImages.SelectedIndex + 1

     PictureBoxName = "PbImage" & Cstr(iImage)
???
???


End Sub

I'm sure it must be simple but I just can't seem to figure it out.

Thanks in advance for any help.
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 526
Reputation: selvaganapathy is an unknown quantity at this point 
Solved Threads: 89
selvaganapathy's Avatar
selvaganapathy selvaganapathy is offline Offline
Posting Pro

Re: How to access a control by its (variable) name?

 
0
  #2
Jun 6th, 2008
Hi,
I give example for iterate currently selected tab controls
The code is
  1. For Each ctrl As Control In TabControl1.SelectedTab.Controls
  2. If TypeOf ctrl Is TextBox Then
  3. MsgBox(ctrl.Text)
  4. End If
  5. Next

But it gets all the text boxes in the currently Selected Tab.
You can also add Condition like that
    If Ctrl.Name = "TextBox3" Then
              'Code for TextBox3
               .......
    End If
Or to Dynamically select
    If Ctrl.Name = ("TextBox" & (TabControl1.SelectedIndex + 1) )Then
              'Code for TextBox 
              ' Make sure Tab1 Contains TextBox1, Tab2 contains TextBox2 etc
               .......
    End If
Selva
Reply With Quote Quick reply to this message  
Join Date: Jun 2008
Posts: 3
Reputation: FastCat is an unknown quantity at this point 
Solved Threads: 0
FastCat FastCat is offline Offline
Newbie Poster

Re: How to access a control by its (variable) name?

 
0
  #3
Jun 6th, 2008
Thanks selvaganapathy,

I got as far as doing this...

 Sub LoadImage(ByVal sFilename As String)
        Dim iImage As Integer
        Dim FilenameboxName As String
        Dim PictureBoxName As String

        '
        'Determine which tab is selected
        '
        iImage = frmMain.tcImages.SelectedIndex + 1
        FilenameboxName = "FilenameBoxImage" & CStr(iImage)
        PictureBoxName = "pbImage" & CStr(iImage)

'
' Iterate though controls on tab.
'
        For Each ctrl As Control In frmMain.tcImages.SelectedTab.Controls
            '
            ' Populate txt box
            '
            If TypeOf ctrl Is TextBox And ctrl.Name = FilenameboxName Then
                ctrl.Text = sFilename
            End If
            '
            ' populate picture box
            '
            If TypeOf ctrl Is PictureBox And ctrl.Name = PictureBoxName Then
             *   ctrl.Imagelocation = sFilename
              *  ctrl.SizeMode = PictureBoxSizeMode.StretchImage
            End If
        Next
    End Sub        '

But I get compile/build errors on the lines I highlighted with a *.
Error 1 'Imagelocation' is not a member of 'System.Windows.Forms.Control'.
Error 2 'SizeMode' is not a member of 'System.Windows.Forms.Control'.

so it wont run. Any ideas how to get round this.
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 526
Reputation: selvaganapathy is an unknown quantity at this point 
Solved Threads: 89
selvaganapathy's Avatar
selvaganapathy selvaganapathy is offline Offline
Posting Pro

Re: How to access a control by its (variable) name?

 
0
  #4
Jun 7th, 2008
Hi
If TypeOf ctrl Is PictureBox And ctrl.Name = PictureBoxName Then
* ctrl.Imagelocation = sFilename
* ctrl.SizeMode = PictureBoxSizeMode.StretchImage
End If
In this coding just Ctrl is Control reference not a PictureBox. so You Need to Cast it to PictureBox reference

Your Coding is
  1. If TypeOf ctrl Is PictureBox And ctrl.Name = PictureBoxName Then
  2. Dim MyPictureBox as PictureBox
  3. MyPictureBox = ctrl
  4.  
  5. MyPictureBox.Imagelocation = sFilename
  6. MyPictureBox.SizeMode = PictureBoxSizeMode.StretchImage
  7. End If
Selva
Reply With Quote Quick reply to this message  
Join Date: Jun 2008
Posts: 3
Reputation: FastCat is an unknown quantity at this point 
Solved Threads: 0
FastCat FastCat is offline Offline
Newbie Poster

Re: How to access a control by its (variable) name?

 
0
  #5
Jun 9th, 2008
Fantastic. Thanks selvaganapathy - That worked just fine.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:




Views: 5446 | Replies: 4
Thread Tools Search this Thread



Tag cloud for VB.NET
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC