• 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

    • I'd switch to the new outlook readily ... if it was not such utter dogshit! Trying to replace the original Outlook with all it's functionality with that knock-off mobile app is downright insulting.
    • Yeah, it seemed like a bunch of 2D decal assets to me, so I didn't think it would be a huge issue to replace them. Assuming, of course, that Sony didn't just decide to settle and pay the artist instead. Do we know if that happened?
    • No worries, I wasn't pointing to your post specifically at all. I was talking in general and building upon your idea.
    • This 2TB NVMe Gen4 SSD is priced just $94 and you also get a free 64GB UHS-I memory card by Sayan Sen A few days ago, we reported on Team Group's T-FORCE G50 4TB NVMe SSD that was up for grabs at just $200 thanks to a promo coupon. Sadly, that deal has expired although you can still WD's SN8100 (Gen5) and SN7100 (Gen4) offerings as they are still running the discount. If you don't have the budget for those or are shopping for lower capacity drives then Crucial's T500 Gen4 drive discount is still live, and you can get them for just $125. And while the G50 4TB deal has expired, Team Group is now offering its 2TB model at its lowest ever price and you also get a free Micro SD card with it. The Team Group G50 is also a TLC (triple level cell) NAND flash SSD, and thus the endurance on the T-FORCE SSD is quite good, as it is rated for 1300 TBW (terabytes written) for the 2TB variant. Its MTBF, or Mean Time Between Failure, is claimed at 3,000,000 hours. The operating temperature is 0~70 C. The G50 does not have a dedicated DRAM cache (only the G50 Pro SKUs have it), but since it is based on NVMe version 1.4 which supports HMB (host memory buffer) technology; thus, the drive can use system memory for caching. In terms of performance, Team Group promises sequential read and write speeds of up to 5000 MB/s and 4500 MB/s, respectively. However, the firm does not disclose random throughput metrics. Get the Team Group G50 at the link below (deal is said to be ending in less than 10 hours): Team Group T-FORCE G50 SSD (TM8FFE002T0C129) + Team Group 64GB Elite microSDXC UHS-I U3, V30, A1, Micro SD with SD Adapter, to 100MB/s (TEAUSDX64GIV30A103): $105.99 + $12 off with promo code SSETA665 (Shipped and Sold by Newegg US) This Amazon deal is US-specific and not available in other regions unless specified. If you don't like it or want to look at more options, check out the Amazon US deals page here. Get Prime (SNAP), Prime Video, Audible Plus or Kindle / Music Unlimited. Free for 30 days. As an Amazon Associate, we earn from qualifying purchases.
  • Recent Achievements

    • First Post
      Ian_ earned a badge
      First Post
    • Explorer
      JaviAl went up a rank
      Explorer
    • Reacting Well
      Cole Multipass earned a badge
      Reacting Well
    • Reacting Well
      JLP earned a badge
      Reacting Well
    • Week One Done
      Rhydderch earned a badge
      Week One Done
  • Popular Contributors

    1. 1
      +primortal
      646
    2. 2
      ATLien_0
      269
    3. 3
      Michael Scrip
      218
    4. 4
      +FloatingFatMan
      184
    5. 5
      Steven P.
      146
  • Tell a friend

    Love Neowin? Tell a friend!