• 0

VB.Net - Dynamically accessing controls then accessing them after


Question

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

10 answers to this question

Recommended Posts

  • 0

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?

  • 0
  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.

  • 0

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!

  • 0

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.

  • 0

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.

  • 0
  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

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!

  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?

  • 0
  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

  • 0
  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

  • 0
  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 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!

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.

  • 0
  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.

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.
  • Posts

    • My best decision: SHIFT+DELETE WINDOWS Then Installed Fedora Linux. Now I am a Happy Person
    • I value a game as a whole including graphics. But going backwards in terms of graphics with a game I'm not already invested in (as in something I played and loved in the past and still do) is off-putting. Its a very poor demo trailer if they didn't crank the settings to max that would be ridiculous. Different strokes for different folks i guess, i'm not into slop the trailer and likely game could have been far better, it doesn't it instead looks like it was made on a shoe string budget or with lack of experience. But that could also be down to trying to get it running on low budget/outdated hardware and sacrificing the top end.
    • > Our goal is to ensure that the App Store remains an outstanding opportunity for developers and a safe, trusted experience for our users. There are so many scam apps on the platform that it is hard to believe they are truly interested in having a safe, trusted experience for their users.
    • Google announces upgraded Gemini 2.5 Pro model with enhanced capabilities by Pradeep Viswanathan Google today announced an updated Gemini 2.5 model with several improvements that can be observed in popular AI benchmarks. Google particularly highlighted that the new Gemini 2.5 Preview 06-05 "Thinking" model performs better in coding, math, science, and reasoning. Last month, during Google I/O, Google released an updated version of the Gemini 2.5 Pro model with significant improvements. Today's update builds on the release of the previous month with further enhancements. In addition to benchmark improvements, based on user feedback, this updated Gemini 2.5 Pro model also comes with improved style and structure, which will result in creative and better-formatted responses for end users. You can find the benchmark comparison with other leading AI models below. As you can notice in the above table, this updated Gemini 2.5 Pro preview model is now SOTA (State-of-the-Art) in coding benchmarks like Aider Polyglot. It also scored SOTA performance on GPQA and Humanity’s Last Exam (HLE) benchmarks, which test math, science, knowledge, and reasoning capabilities. In real-world testing, this latest 2.5 Pro model scored 24 points better on LMArena, maintaining its lead, and a 35-point jump on WebDevArena at 1443. Developers can access this latest Gemini 2.5 Pro preview model through the Gemini API via Google AI Studio and Vertex AI. General consumers will be able to access this model via the Gemini app. Google also confirmed that the Gemini 2.5 Pro model will be generally available in a couple of weeks, allowing developers to start using it in production-ready enterprise-scale applications.
    • The Bromance is definitely over! 🤣🤣🤣
  • Recent Achievements

    • One Year In
      survivor303 earned a badge
      One Year In
    • Week One Done
      jbatch earned a badge
      Week One Done
    • First Post
      Yianis earned a badge
      First Post
    • Rookie
      GTRoberts went up a rank
      Rookie
    • First Post
      James courage Tabla earned a badge
      First Post
  • Popular Contributors

    1. 1
      +primortal
      419
    2. 2
      +FloatingFatMan
      182
    3. 3
      snowy owl
      181
    4. 4
      ATLien_0
      176
    5. 5
      Xenon
      135
  • Tell a friend

    Love Neowin? Tell a friend!