| | |
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:
Solved Threads: 0
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:
Here's my routine where I create the new tab, picturebox:
My 'Select' button does this: ie gets the selected file name.
I just want to know how to refere to the objects on the currently selected tab, by name in my loadimage routine
I'm sure it must be simple but I just can't seem to figure it out.
Thanks in advance for any help.
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 SubMy '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 IfI 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 SubI'm sure it must be simple but I just can't seem to figure it out.
Thanks in advance for any help.
Hi,
I give example for iterate currently selected tab controls
The code is
But it gets all the text boxes in the currently Selected Tab.
You can also add Condition like that
Or to Dynamically select
I give example for iterate currently selected tab controls
The code is
VB.NET Syntax (Toggle Plain Text)
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 If Ctrl.Name = ("TextBox" & (TabControl1.SelectedIndex + 1) )Then
'Code for TextBox
' Make sure Tab1 Contains TextBox1, Tab2 contains TextBox2 etc
.......
End IfSelva •
•
Join Date: Jun 2008
Posts: 3
Reputation:
Solved Threads: 0
Thanks selvaganapathy,
I got as far as doing this...
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.
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
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
* ctrl.Imagelocation = sFilename
* ctrl.SizeMode = PictureBoxSizeMode.StretchImage
End If
Your Coding is
VB.NET Syntax (Toggle Plain Text)
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
Selva ![]() |
Similar Threads
- Tutorial: Understanding ASP classes (ASP)
- illegal call of non-static member function (C++)
- Ask a problem,About CEdit control (C)
- about static variable (Java)
- vb6/common dialog/access db (Visual Basic 4 / 5 / 6)
- AnsiString Template Data Return Problem Builder 6 (C++)
- Need to make my program access a web page automatic when user opens program (C)
Other Threads in the VB.NET Forum
- Previous Thread: DLL
- Next Thread: Load registry hive
Views: 5446 | Replies: 4
| Thread Tools | Search this Thread |
Tag cloud for VB.NET
.net 2005 2008 access account application arithmetic array arrays basic bing button buttons c# center check checkbox code combobox component convert crystalreport data database databasesearch datagrid datagridview design designer dissertation dissertations dropdownlist excel file-dialog folder ftp generatetags google gridview hardcopy highlighting images inline insert installer intel internet listview mobile monitor ms net networking output passingparameters peertopeervideostreaming picturebox picturebox1 plugin port print printing problem problemwithinstallation project save searchbox searchvb.net select serial server soap sorting studio syntax table tcp text textbox time timer toolbox trim update updown user vb vb.net vb.netcode vb.netformclosing()eventpictureboxmessagebox vb2008 vbnet view visual visualbasic visualbasic.net visualstudio visualstudio2008 web wpf





