MiG- Posted December 21, 2007 Share Posted December 21, 2007 Right, I have some code that dynamically creates controls that I need, and obviously you can give them a name and what have you. The controls will be created on form load, yet how do I access the controls after I've added them? VB.Net doesn't seem to know they are they due to me generating them at run time. Any ideas? [Just Read the Topic Title and realised i've typed a big steaming pile of ****. Lovely.] Link to comment https://www.neowin.net/forum/topic/608427-vbnet-dynamically-accessing-controls-then-accessing-them-after/ Share on other sites More sharing options...
0 Jelly2003 Posted December 21, 2007 Share Posted December 21, 2007 OK, you've created the control, say a TextBox, the page has loaded, the user enters their input, the form is submitted and you want to get the value that the person entered, but it always turns up with no value in it? Link to comment https://www.neowin.net/forum/topic/608427-vbnet-dynamically-accessing-controls-then-accessing-them-after/#findComment-589078972 Share on other sites More sharing options...
0 MiG- Posted December 21, 2007 Author Share Posted December 21, 2007 Jelly2003 said: OK, you've created the control, say a TextBox, the page has loaded, the user enters their input, the form is submitted and you want to get the value that the person entered, but it always turns up with no value in it? No. Created a textbox using Code and not the IDE at runtime, but its not accessible? after you've created it you can't access it by the name you've created for it. Link to comment https://www.neowin.net/forum/topic/608427-vbnet-dynamically-accessing-controls-then-accessing-them-after/#findComment-589078976 Share on other sites More sharing options...
0 neufuse Veteran Posted December 21, 2007 Veteran Share Posted December 21, 2007 If you dynamically create a control the "name" property means nothing... The Name property is for the IDE only and has no meaning at runtime say you make a control like this public class MyForm Dim MyControl as new TextBox public sub new Me.Controls.Add(MyControl) ' add to form with MyControl .size = new size(21,100) .Location = new Point(10,10) end With end sub public sub UpdateControlsText ' Access the control now with the object you made in the class outside of a sub or function MyControl.Text = "Text Here" End Sub end class not that hard eh? anytime you make a control inside a sub though you need a reference to the control so you can access it outside that sub also! Link to comment https://www.neowin.net/forum/topic/608427-vbnet-dynamically-accessing-controls-then-accessing-them-after/#findComment-589078981 Share on other sites More sharing options...
0 Antaris Veteran Posted December 21, 2007 Veteran Share Posted December 21, 2007 Wel, no doubt that you are adding them to the control container of the form (<form>.Controls) ? If so, you need to iterate through that to find your control, you may have to do it recusively if it is a nested control. The best way to do this, is to apply a unique value to either the name, or the tag property of the control at runtime (i.e during the control generation): Public Function FindControl(parent As Control, ident As String) As Control Dim control As Control For Each child As Control In parent.Controls If (child.Tag.Equals(ident, StringComparer.InvariantCultureIgnoreCase)) Then control = child Exit For End If End For Return control End Function Start with passing the main form as the parent control. Link to comment https://www.neowin.net/forum/topic/608427-vbnet-dynamically-accessing-controls-then-accessing-them-after/#findComment-589078983 Share on other sites More sharing options...
0 Jelly2003 Posted December 21, 2007 Share Posted December 21, 2007 Assuming that you're using ASP.net and not Windows Forms then you could use the "FindControl" method (provided that it implements it). Object.FindControl("ControlName") or Object.FindControl("ControlName", true) To recursively search the controls. I am a C# guy so you would need to do some research on the VB.net way of doing it, but it will be very much the same. Link to comment https://www.neowin.net/forum/topic/608427-vbnet-dynamically-accessing-controls-then-accessing-them-after/#findComment-589079016 Share on other sites More sharing options...
0 MiG- Posted December 21, 2007 Author Share Posted December 21, 2007 neufuse said: If you dynamically create a control the "name" property means nothing... The Name property is for the IDE only and has no meaning at runtimesay you make a control like this public class MyForm Dim MyControl as new TextBox public sub new Me.Controls.Add(MyControl) ' add to form with MyControl .size = new size(21,100) .Location = new Point(10,10) end With end sub public sub UpdateControlsText ' Access the control now with the object you made in the class outside of a sub or function MyControl.Text = "Text Here" End Sub end class not that hard eh? anytime you make a control inside a sub though you need a reference to the control so you can access it outside that sub also! Jelly2003 said: Assuming that you're using ASP.net and not Windows Forms then you could use the "FindControl" method (provided that it implements it).Object.FindControl("ControlName") or Object.FindControl("ControlName", true) To recursively search the controls. I am a C# guy so you would need to do some research on the VB.net way of doing it, but it will be very much the same. Its VB.NET but thanks for the PM anyway. And to the first reply, I thought of that but I have no idea how many controls i am going to have, therefore I'd need an array but not knowing how big it is, how would I allocate for that in .Net? Link to comment https://www.neowin.net/forum/topic/608427-vbnet-dynamically-accessing-controls-then-accessing-them-after/#findComment-589079454 Share on other sites More sharing options...
0 neufuse Veteran Posted December 21, 2007 Veteran Share Posted December 21, 2007 MiG- said: Its VB.NET but thanks for the PM anyway. And to the first reply, I thought of that but I have no idea how many controls i am going to have, therefore I'd need an array but not knowing how big it is, how would I allocate for that in .Net? why would you need an array? if you add it to a container control such as a form or panel that control / form has a controls property which you can use to reference controls that have been added to the form / container... that would be the only case you'd want to specify a "name" for a control then you could do a string.compare to compare the name of the control and the item you are looking for Link to comment https://www.neowin.net/forum/topic/608427-vbnet-dynamically-accessing-controls-then-accessing-them-after/#findComment-589079497 Share on other sites More sharing options...
0 MiG- Posted December 21, 2007 Author Share Posted December 21, 2007 neufuse said: why would you need an array? if you add it to a container control such as a form or panel that control / form has a controls property which you can use to reference controls that have been added to the form / container... that would be the only case you'd want to specify a "name" for a control then you could do a string.compare to compare the name of the control and the item you are looking for going on the code you demo'd, which is pretty much how i'm going about things... I'm currently doing... Adding tab pages to a Tab Control based on the number specified. Then for each tab I need to have the same grid drawn on them, which would be a series of textboxes. Seen as I need a variable name for each control that is added, preferably an array seen as its a grid ;) how would I determine how many variables I need? Probably going about this all wrong but whatever! :D Link to comment https://www.neowin.net/forum/topic/608427-vbnet-dynamically-accessing-controls-then-accessing-them-after/#findComment-589079694 Share on other sites More sharing options...
0 AnotherCoder Posted January 3, 2008 Share Posted January 3, 2008 neufuse said: If you dynamically create a control the "name" property means nothing... The Name property is for the IDE only and has no meaning at runtime I'm not sure that I agree with you here. The "Name" property is plenty valid at runtime, and I think that it's good programming practice to set it when you create a control eg Dim newctl As New System.Windows.Forms.TextBox newctl.Location = New System.Drawing.Point(102, 9) newctl.Name = "txtMyNewControl" newctl.Size = New System.Drawing.Size(95, 20) newctl.TabIndex = 9 Me.Controls.Add(newctl) neufuse said: say you make a control like thispublic class MyForm Dim MyControl as new TextBox public sub new Me.Controls.Add(MyControl) ' add to form with MyControl .size = new size(21,100) .Location = new Point(10,10) end With end sub public sub UpdateControlsText ' Access the control now with the object you made in the class outside of a sub or function MyControl.Text = "Text Here" End Sub end class not that hard eh? anytime you make a control inside a sub though you need a reference to the control so you can access it outside that sub also! True - but if you've named the control you can find it ;-) The following function will "find" a control by name on a form, even if it's hiding inside a tab control... Private Function FindControl(ByVal parent As Control, ByVal ident As String) As Control Dim n As Integer Dim tmpctrl As Control Dim tmpctrl2 As Control For n = 0 To parent.Controls.Count - 1 tmpctrl = parent.Controls(n) If tmpctrl.Name = ident Then Return parent.Controls(n) ElseIf tmpctrl.Controls.Count > 0 Then tmpctrl2 = FindControl(tmpctrl, ident) If Not IsNothing(tmpctrl2) Then Return tmpctrl2 End If End If Next ' Not found Return Nothing end function You call it by sending "me" (ie the form) and the name of the control you want to manipulate. Link to comment https://www.neowin.net/forum/topic/608427-vbnet-dynamically-accessing-controls-then-accessing-them-after/#findComment-589107699 Share on other sites More sharing options...
0 Brandon Live Veteran Posted January 3, 2008 Veteran Share Posted January 3, 2008 MiG- said: going on the code you demo'd, which is pretty much how i'm going about things...I'm currently doing... Adding tab pages to a Tab Control based on the number specified. Then for each tab I need to have the same grid drawn on them, which would be a series of textboxes. Seen as I need a variable name for each control that is added, preferably an array seen as its a grid ;) how would I determine how many variables I need? Probably going about this all wrong but whatever! :D Well first of all, it sounds like what you really want is a custom UserControl that includes the appropriate TextBox grid. Then when you need to add a page, you add one of those to it, instead of manually adding all the text boxes each time. You can even build your custom control in the Forms Designer. And you can put simple methods / properties on it to handle any work it needs to do for you, or expose any data you need in the parent form. If the parent form (code outside the UserControl) needs to access the text boxes, or they need to access each other, there are several ways to accomplish that. For example, you can maintain an array / list with references to all the custom controls you added. Or you can simply get to the custom control through the Tab control. Link to comment https://www.neowin.net/forum/topic/608427-vbnet-dynamically-accessing-controls-then-accessing-them-after/#findComment-589107761 Share on other sites More sharing options...
Question
MiG-
Right, I have some code that dynamically creates controls that I need, and obviously you can give them a name and what have you.
The controls will be created on form load, yet how do I access the controls after I've added them?
VB.Net doesn't seem to know they are they due to me generating them at run time.
Any ideas?
[Just Read the Topic Title and realised i've typed a big steaming pile of ****. Lovely.]
Link to comment
https://www.neowin.net/forum/topic/608427-vbnet-dynamically-accessing-controls-then-accessing-them-after/Share on other sites
10 answers to this question
Recommended Posts