Jump to content
Xtreme .Net Talk

Is it possibe to dynamically create textboxes during runtime?


Recommended Posts

Posted

for i = 0 to lstbox.items.count - 1

dim txtQty(i) as text box

 

locatoin, size, color and stuff....

 

next

 

 

i can run it but no txt boxes are created

 

any ideas?

I feel the pivitol moment of failure approaching.
  • *Experts*
Posted

Yes it is.

For x as integer = 0 to lixbox1.Items.Count - 1
   dim txt as new TextBox 'create a textbox
   txt.Location = new Point(x,x)
   'set some other desired properties
   me.Controls.Add(txt) 'add the textbox to the control collection of the form
Next

Posted

Your sample code implies that you were thinking of control arrays as you had them in VB6 times. This is no longer possible the way you tried it.

 

You have no access to the newly created control through their name.

.nerd
Posted
You have no access to the newly created control through their name.

 

Not wanting to cause trouble with my very first post, but, umm, well you could access the Name property of a control created at runtime if you really wanted to.

 

'  Some demo data in listbox
Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load
       With ListBox1
           .Items.Add(44)
           .Items.Add(70)
           .Items.Add(222)
       End With
   End Sub

'  Create some textboxes at runtime
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
       Dim x As Integer
       For x = 0 To ListBox1.Items.Count - 1
           Dim txt As New TextBox() 'create a textbox
           txt.Location = New Point(ListBox1.Items(x), ListBox1.Items(x))
           txt.Name = "TextBox" & x.ToString  '  Give it a name here
           'set some other desired properties
           Me.Controls.Add(txt) 'add the textbox to the control collection of the form
       Next

   End Sub

'  Enumerate through form's controls looking for one by name:
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
       Dim ctrl As Control
       Dim TB As TextBox
       For Each ctrl In Me.Controls
           If TypeOf ctrl Is TextBox Then
               TB = CType(ctrl, TextBox)
               If TB.Name = "TextBox2" Then TB.Text = "Found TextBox2 in code"
           End If
       Next
   End Sub

Posted

OK. Point taken.

 

But it's a bit awkward IMHO to code a meaning in the name just to decode it later when you want to access the textbox.

 

Either it is a very interesting textbox. Then there's nothing against keeping an object reference of the textbox itself.

 

Or the Textbox is autogenerated (by some sort of rule) and mayne you have to access it later. Then the best way is to access it by whatever attribute makes it special (i.e. the rule) and not the name.

 

Just my 3c. (Inflation in Euroland)

.nerd
Posted

Yeah, Heiko, no sweat . - I couldn't think of a really good reason why I would ever want to do it. It was just to put the record straight, is all. :)

 

SamSmithNZ,

Re the other question - no, the textbox's Visible Property is set to True by default as soon as it's created - whether at design time or run time, makes no difference. (Of course you could set it to False, but then we'd really be going into never-neverland scenarios) :D

Posted

just to throw in a (more of less) new thing.. ;) you CAN access a control by its name (needs some extra effort -> see attachment).. so if you create TextBoxes (or any other Control) dynamically all you have to do is give them names you can use later to access them and you won't have any trouble doing with them whatever you want later...

 

Andreas

controlsbyname.zip

Posted

SamSmithNZ,

Re the other question - no, the textbox's Visible Property is set to True by default as soon as it's created - whether at design time or run time, makes no difference. (Of course you could set it to False, but then we'd really be going into never-neverland scenarios) :D

 

Ok thanks, I wasn't sure, and was a little confused with yucky vb6 land (which this week I've been stuck in a bit - yucky yucky old projects :))

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...