• 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

    • One can also use fastFetch. Gives you a quick hardware spec in a CMD or PowerShell console: https://imgur.com/a/fastfetch-kC0E3xT
    • Looking back at your message, you mention "less harsh terms." If by that you mean that requiring the pentagon to pass an audit is harsh, then to that I would say, that was already passed into law back in 1990. I wasn't planning to debate if that law was reasonable or not. If you are calling it harsh, then that is your opinion and we can disagree, but our options matter little considering it passed long ago. If you don't think the law itself is harsh, then I really can't understand how a reasonable enforcement of said law could be considered harsh either.
    • I have a strong feeling more than one store did that, and idk who is to blame more, Nintendo's terrible choice of packaging layout, or the sellers. I can't wait to see some class action lawsuit if Nintendo or said retailer doesn't step up. The question is what wonderful name we can come up with for the thing. Screengate(Staingate) already happened with Apple, so that's taken.
    • I'm definitely like this. I carry my rucksack everywhere with me and feel quite naked without it on my back. What if I need my laptop? What if the guys want to play a game of Magic? What if I need a plaster, or rubber gloves? Or my lockpicks and binoculars? Or my chessboard set? Or my portable power bank for my phone? I can't go anywhere without it, it's a life saver.
    • Bethesda's Deathloop is free to claim on the Epic Games Store this week by Pulasthi Ariyasinghe The Epic Games Store's Mega Sale promotion is in its final week, and that means mystery giveaways are soon ending too. For the finale, Epic Games has called in something big from Bethesda, with the Arkane-developed time loop adventure Deathloop coming in as the latest freebie for PC gamers. Alongside it, the indie adventure Ogu and the Secret Forest is also free. Developed by Arkane Lyon and published by Bethesda, Deathloop comes in touting an action-packed campaign involving plenty of time travel shenanigans. The game is set on the mysterious island of Blackreef, where two rival assassins, Colt and Julianna, are trapped in a time loop. The player, as Colt, has to figure out how to eliminate eight targets within a single day to escape the loop. Each of the assassinations can be taken care of in many ways, including stealth, traps, accidents, or simply going in guns blazing. Aside from gunplay, the title also makes use of supernatural systems very similar to the studio's Dishonored franchise, letting players teleport, go invisible, use telekinesis, and more. There is a multiplayer twist here too, where players, as Julianna, can invade the campaigns of others to take the role of the rival assassin, flipping the tables on the main character and his plans. As for Ogu and the Secret Forest, this is a 2024-released indie adventure featuring hand-drawn characters and various types of puzzles. The 2D game involves befriending characters across a fantasy land as baby Ogu, with plenty of exploration, puzzle solving, and boss battles available. The Deathloop and Ogu and the Secret Forest giveaways are now live on the Epic Games Store for all PC gamers. The promotion is slated to last until June 12, giving you seven days to claim a copy for your library permanently. While the summer mystery giveaways are ending, regular freebies will continue to arrive from the Epic Games Store. When this one comes to an end on Thursday, the SEGA-published humorous hospital simulation entry Two Point Hospital is incoming as the next giveaway.
  • Recent Achievements

    • 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
    • Reacting Well
      James courage Tabla earned a badge
      Reacting Well
  • Popular Contributors

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

    Love Neowin? Tell a friend!