martialarts Posted June 20, 2003 Posted June 20, 2003 My program reads from a data file and outputs a series of controls for each line in the text file. I am using .NET 2002 and from what I can gather, control collections are no longer built in. I am trying to dynamically create and dynamically name these controls. I was able to integrate this code into my program and it works partially: 'this is part of a loop and counter is incremented each time 'element is an instance of a class Dim LabelA1 As New Label() LabelA1.Width = 232 LabelA1.Height = 24 LabelA1.Location = New Point(16, counter * 25) LabelA1.Text = element.name Me.Controls.Add(LabelA1) LabelA1.Name = "LabelA" & counter I don't know if I'm on the right track, but it does display all the controls like I need it to when I run it. The problem is that all the labels are named "LabelA1", and my attempts to manipulate any one of them are treated aggregately. I guess this is because their name was "LabelA1" when added to me.controls. Please point me in the right direction. I am new to VB and .NET, but not to programming in general. Thanks! Quote
*Experts* mutant Posted June 20, 2003 *Experts* Posted June 20, 2003 You could use the array list to keep the control in a collection. Quote
Moderators Robby Posted June 20, 2003 Moderators Posted June 20, 2003 Why are they all named LabelA1, is your Counter incrementing ? Quote Visit...Bassic Software
martialarts Posted June 21, 2003 Author Posted June 21, 2003 Thank you both for responding, especially so quickly. I implemented mutant's solution and it worked... although it led me to another question. I don't know if I completely understand control collections yet, but to answer Robby's question- I intended for it to increment so that I could individually reference each of them with LabelA1,LabelA2, LabelB1,LabelB2... where LabelAx was the first column of labels and LabelBx was the second column, etc. I couldn't implement that correctly because the syntax to add to a collection is: me.controls.add(LabelA1) which doesn't allow me to dynamically name the labels with variables... I don't know if there is an alternate syntax that would work better for me... me.controls.add("LabelA" & x) would be nice, but .net doesn't seem to work that way. Anyway, by using the control collection references I can awkwardly manipulte individual controls, but I don't know of a way to use a gotfocus event to change the background of my text boxes since they are also dynamically generated. Do you know how to do this? Thanks again for your time! Quote
*Experts* mutant Posted June 21, 2003 *Experts* Posted June 21, 2003 Im not sure if im understanding you right :) , but I thinnk you are in need of an event handler. To add one dynamically do this: AddHandler ControlName(counterorwhateveryouuse).GotFocus, AddressOf handlersub Your handler sub must accept arguments that correspond to the event. Then you check what control sent it with the sender argument. Again, I might totally off on this, so tell me if im talking about what you wanted to know :) . Quote
Moderators Robby Posted June 21, 2003 Moderators Posted June 21, 2003 If all you want is to add controls at runtime... For x = 1 To 20 txt = New textbox() With txt AddHandler .GotFocus, AddressOf txtClick .Text = "Textbox " & x.tostring .Location = New Point(0, x * 25) .Size = New Size(200, 60) End With Controls.Add(txt) Next Quote Visit...Bassic Software
martialarts Posted June 21, 2003 Author Posted June 21, 2003 Thanks again. I used your advice and it worked! I took it a couple of steps farther to make sure that I could do everything I needed to do. I am finally able to address my dynamic controls individually, although I have to do by searching through the controls array to find them... which isn't a problem at all. I'm surprised and also greatful that both of you are so active on this forum. If I become experienced at VB I will be sure to check this forum for people I can pass the favor on to. Thanks! Quote
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.