|
web
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
Create controls at runtimeI'm trying to write a simple multiplication tables program for my little girl. How can I create 200 labels, 100 textboxes. Here is what I've tried so far (without luck). For looper As Int16 = 1 To 101 Dim lab As New Label lab.Name = "labQuestion" & Format(looper) Me.Controls.Add("labQuestion" & Format(looper)) 'seems I can only add them one at a time and it doesn't recognize labquestion1 ... Next 'Me.Controls.Add(lab) Thanks for any help You should probably set WHERE you want the labels to go on the form. Also,
pass the object, not the name, into the add method. Show quoteHide quote "TonyMast" <TonyM***@xxxMSNxxx.Com> wrote in message news:%23736uoBWGHA.5036@TK2MSFTNGP15.phx.gbl... > VB 2005 - XP Pro - Windows forms > I'm trying to write a simple multiplication tables program for my little > girl. > How can I create 200 labels, 100 textboxes. > > Here is what I've tried so far (without luck). > For looper As Int16 = 1 To 101 > > Dim lab As New Label > > > lab.Name = "labQuestion" & Format(looper) > > Me.Controls.Add("labQuestion" & Format(looper)) 'seems I can only add > them one at a time and it doesn't recognize labquestion1 ... > > Next > > 'Me.Controls.Add(lab) > > Thanks for any help > > Hello, Tony,
In addition to Scott's advice, don't forget to also set the Size of the controls. (Actually, you will probably want to do this first, as it will likely influence where the control will sit.) You could modify the code you already have by adding a few lines to make it something like: Private Sub Form1_Load(ByVal sender As Object, _ ByVal e As System.EventArgs) _ Handles MyBase.Load For looper As Int16 = 1 To 101 Dim lab As New Label lab.Height = 16 ' Set Size. lab.Width = 150 lab.Left = 20 ' "...set WHERE". lab.Top = 20 + (looper - 1) * (lab.Height + 4) lab.Name = "labQuestion" & Format(looper) lab.Text = "I am label # " & looper Me.Controls.Add(lab) ' "pass the object, not the name" Next End Sub Cheers, Randy Scott M wrote: Show quoteHide quote > You should probably set WHERE you want the labels to go on the form. > Also, pass the object, not the name, into the add method. > > "TonyMast" <TonyM***@xxxMSNxxx.Com> wrote in message > news:%23736uoBWGHA.5036@TK2MSFTNGP15.phx.gbl... > >> VB 2005 - XP Pro - Windows forms >> I'm trying to write a simple multiplication tables program for my >> little girl. >> How can I create 200 labels, 100 textboxes. >> >> Here is what I've tried so far (without luck). >> For looper As Int16 = 1 To 101 >> >> Dim lab As New Label >> >> >> lab.Name = "labQuestion" & Format(looper) >> >> Me.Controls.Add("labQuestion" & Format(looper)) 'seems I can only >> add them one at a time and it doesn't recognize labquestion1 ... >> >> Next >> >> 'Me.Controls.Add(lab) >> >> Thanks for any help >> >> > Thanks to both, but I'm still stuck on how to use the
me.controls.add Do I have to do this 300 times to create 300 controls? I wanted to use some loops to create the objects. I wanted to create objects with a for next loop, for x =1 to 100 object = "label" & format (X) dim object as new label 'I would also add the sizes and location here next Just can't figure out how to do it. Thanks Tony Show quoteHide quote "R. MacDonald" <sci***@NO-SP-AMcips.ca> wrote in message news:44337830$0$27491$dbd4b001@news.wanadoo.nl... > Hello, Tony, > > In addition to Scott's advice, don't forget to also set the Size of the > controls. (Actually, you will probably want to do this first, as it will > likely influence where the control will sit.) You could modify the code > you already have by adding a few lines to make it something like: > > Private Sub Form1_Load(ByVal sender As Object, _ > ByVal e As System.EventArgs) _ > Handles MyBase.Load > > For looper As Int16 = 1 To 101 > > Dim lab As New Label > > lab.Height = 16 ' Set Size. > lab.Width = 150 > > lab.Left = 20 ' "...set WHERE". > lab.Top = 20 + (looper - 1) * (lab.Height + 4) > > lab.Name = "labQuestion" & Format(looper) > lab.Text = "I am label # " & looper > > Me.Controls.Add(lab) ' "pass the object, not the name" > > Next > > End Sub > > Cheers, > Randy > > > Scott M wrote: > >> You should probably set WHERE you want the labels to go on the form. >> Also, pass the object, not the name, into the add method. >> >> "TonyMast" <TonyM***@xxxMSNxxx.Com> wrote in message >> news:%23736uoBWGHA.5036@TK2MSFTNGP15.phx.gbl... >> >>> VB 2005 - XP Pro - Windows forms >>> I'm trying to write a simple multiplication tables program for my little >>> girl. >>> How can I create 200 labels, 100 textboxes. >>> >>> Here is what I've tried so far (without luck). >>> For looper As Int16 = 1 To 101 >>> >>> Dim lab As New Label >>> >>> >>> lab.Name = "labQuestion" & Format(looper) >>> >>> Me.Controls.Add("labQuestion" & Format(looper)) 'seems I can only add >>> them one at a time and it doesn't recognize labquestion1 ... >>> >>> Next >>> >>> 'Me.Controls.Add(lab) >>> >>> Thanks for any help >>> >>> >> Hello, Tony,
Ummm... Maybe a good night's sleep and then a fresh look through the example in the morning will help. :-) Try pasting the example exactly as it is into the default Form created when you create a new project. Then hit the F5 key. Cheers, Randy TonyMast wrote: Show quoteHide quote > Thanks to both, but I'm still stuck on how to use the > me.controls.add > Do I have to do this 300 times to create 300 controls? > I wanted to use some loops to create the objects. > I wanted to create objects with a for next loop, > for x =1 to 100 > object = "label" & format (X) > dim object as new label > 'I would also add the sizes and location here > next > > Just can't figure out how to do it. > > Thanks > Tony > > Sorry, for some reason it wasn't working in my form. I copied the example
exactly to the form load and it didn't work. However I created a new form and it works fine. Very strange why it wasn't working in the first form but when I created another it did. Thanks and again sorry Tony Show quoteHide quote "R. MacDonald" <sci***@NO-SP-AMcips.ca> wrote in message news:4434231e$0$42166$dbd49001@news.wanadoo.nl... > Hello, Tony, > > Ummm... Maybe a good night's sleep and then a fresh look through the > example in the morning will help. :-) > > Try pasting the example exactly as it is into the default Form created > when you create a new project. Then hit the F5 key. > > Cheers, > Randy > > > TonyMast wrote: > >> Thanks to both, but I'm still stuck on how to use the >> me.controls.add >> Do I have to do this 300 times to create 300 controls? >> I wanted to use some loops to create the objects. >> I wanted to create objects with a for next loop, >> for x =1 to 100 >> object = "label" & format (X) >> dim object as new label >> 'I would also add the sizes and location here >> next >> >> Just can't figure out how to do it. >> >> Thanks >> Tony >> Hello, Tony,
Hey, nothing to be sorry about. Things like that happen to us all. Quite seriously, when things stop making any sense at all, I find it is helpful to just step back for a breather and focus on something else for a while. This is particularly true if you start saying to yourself "I can't...". Our minds, being the amazing things that they are will try to protect us by making us right. Hence, when you think you can't, you find out that you're right, you can't... But when you come back to the problem with a different perspective, i.e. "I can..." or "I will..." often things work much better and begin to make sense again. Keep at it, and good luck. Cheers, Randy TonyMast wrote: Show quoteHide quote > Sorry, for some reason it wasn't working in my form. I copied the example > exactly to the form load and it didn't work. However I created a new form > and it works fine. Very strange why it wasn't working in the first form but > when I created another it did. > > Thanks and again sorry > Tony > "R. MacDonald" <sci***@NO-SP-AMcips.ca> wrote in message > news:4434231e$0$42166$dbd49001@news.wanadoo.nl... > >>Hello, Tony, >> >>Ummm... Maybe a good night's sleep and then a fresh look through the >>example in the morning will help. :-) >> >>Try pasting the example exactly as it is into the default Form created >>when you create a new project. Then hit the F5 key. >> >>Cheers, >>Randy >> >> >>TonyMast wrote: >> >> >>>Thanks to both, but I'm still stuck on how to use the >>>me.controls.add >>>Do I have to do this 300 times to create 300 controls? >>>I wanted to use some loops to create the objects. >>>I wanted to create objects with a for next loop, >>>for x =1 to 100 >>> object = "label" & format (X) >>> dim object as new label >>> 'I would also add the sizes and location here >>>next >>> >>>Just can't figure out how to do it. >>> >>>Thanks >>>Tony >>> > >
To use a combo ornot
Error: Login failed for user ???? Dataset Problem AppDomainSetup: ShadowCopyFiles, ShadowCopyDirectories Example of encryption Printing the contents of a textbox How to detect when ShowDialog object has closed? (2003) Re: String.Trim Base Class Method to use Shadow'ed member variable of Derived Class? rich text maximum number of lines |
|||||||||||||||||||||||