Adam Posted February 23, 2004 Posted February 23, 2004 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? Quote I feel the pivitol moment of failure approaching.
*Experts* mutant Posted February 23, 2004 *Experts* Posted February 23, 2004 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 Quote
Adam Posted February 23, 2004 Author Posted February 23, 2004 Will this work if i'm creating 3 or 23 textboxes? Quote I feel the pivitol moment of failure approaching.
*Experts* mutant Posted February 23, 2004 *Experts* Posted February 23, 2004 Yes, you can add however many controls you want. (of course until you reach memeory limits and such :)) Quote
Heiko Posted February 24, 2004 Posted February 24, 2004 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. Quote .nerd
kas Posted February 24, 2004 Posted February 24, 2004 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 Quote
samsmithnz Posted February 24, 2004 Posted February 24, 2004 One other thing, don't you have to set the visible property to true after you've created the control? Quote Thanks Sam http://www.samsmith.co.nz
Heiko Posted February 24, 2004 Posted February 24, 2004 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) Quote .nerd
kas Posted February 24, 2004 Posted February 24, 2004 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 Quote
Hamburger1984 Posted February 24, 2004 Posted February 24, 2004 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... Andreascontrolsbyname.zip Quote
samsmithnz Posted February 24, 2004 Posted February 24, 2004 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 :)) Quote Thanks Sam http://www.samsmith.co.nz
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.