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.

Recommended Answers

All 4 Replies

Hi,
I give example for iterate currently selected tab controls
The code is

For Each ctrl As Control In TabControl1.SelectedTab.Controls
            If TypeOf ctrl Is TextBox Then
                MsgBox(ctrl.Text)
            End If
        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

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.

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

If TypeOf ctrl Is PictureBox And ctrl.Name = PictureBoxName Then
   Dim MyPictureBox as PictureBox
   MyPictureBox = ctrl
  
   MyPictureBox.Imagelocation = sFilename
   MyPictureBox.SizeMode = PictureBoxSizeMode.StretchImage
End If

Fantastic. Thanks selvaganapathy :icon_smile: - That worked just fine.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.