|
-
Mar 17th, 2009, 08:11 PM
#1
Thread Starter
Fanatic Member
[RESOLVED] Create a Control During RunTime
I googled this and found several different codes, but none of them work. What I want is to be able to create a control, for example a Label, during the runtime of my program. I've tried:
Code:
Dim lbl as New Label
lbl.Text = "Hello"
lbl.Location = New Point(20, 20)
lbl.Visible = True
lbl.Show()
lbl.Name = "Label1"
Me.Controls.Add(lbl)
And many other variations. However on each one of them nothing happens, no label is visible. Anyone know how to do this?
-
Mar 17th, 2009, 08:18 PM
#2
Re: Create a Control During RunTime
That code worked perfect for me on a button click.
Do you have any panels or anything else at 20,20 which would be in front of your label?
-
Mar 17th, 2009, 08:20 PM
#3
Re: Create a Control During RunTime
you must have another control obscuring the new label. try this:
vb Code:
Dim lbl As New Label
lbl.Text = "Hello"
lbl.Location = New Point(20, 20)
lbl.Visible = True
lbl.Name = "Label1"
Me.Controls.Add(lbl)
lbl.bringtofront
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Mar 17th, 2009, 09:16 PM
#4
Thread Starter
Fanatic Member
Re: Create a Control During RunTime
Ok now it works, I had a list box near 0,0 and on moving it the Label showed up at 20,20. The only thing is that before, when I was testing it, I set the x,y to 100 and 100 the first time, which was well past the ListBox, but it never showed up then.
Now I have a second question related to controls.
Is there a way to dim a control and then use it to change another? For instance this:
Code:
Dim test As New Label
test.Name = "Label" & 1
test.Text = "Hi"
Unfortunately that doesn't work though, even though Label1 is already made. In the past I have used a loop to go through controls until I find the one with the same name as Label + X, however I'm wondering if there's a quicker way, such as declaring a new control and giving it the same name as another, allowing me to access the second control through the first.
-
Mar 17th, 2009, 09:41 PM
#5
Re: Create a Control During RunTime
 Originally Posted by Vectris
Ok now it works, I had a list box near 0,0 and on moving it the Label showed up at 20,20. The only thing is that before, when I was testing it, I set the x,y to 100 and 100 the first time, which was well past the ListBox, but it never showed up then.
Now I have a second question related to controls.
Is there a way to dim a control and then use it to change another? For instance this:
Code:
Dim test As New Label
test.Name = "Label" & 1
test.Text = "Hi"
Unfortunately that doesn't work though, even though Label1 is already made. In the past I have used a loop to go through controls until I find the one with the same name as Label + X, however I'm wondering if there's a quicker way, such as declaring a new control and giving it the same name as another, allowing me to access the second control through the first.
Let's say that I have a dog named Rover. If I then go and get a new dog and name it Rover too and then cut its hair short, would I expect my original dog to end up with short hair? If not then why would the same thing happen to a Label? Just because you give two objects the same name it doesn't make them the same object. If you want to change an object then you need to get a reference to that object, not a new object with the same name.
If you want to get a control by name, which you should generally avoid if possible, then you can do like this:
vb.net Code:
Dim test As Label = Me.Controls("Label" & 1)
-
Mar 17th, 2009, 10:46 PM
#6
Thread Starter
Fanatic Member
Re: Create a Control During RunTime
 Originally Posted by jmcilhinney
If you want to get a control by name, which you should generally avoid if possible, then you can do like this:
vb.net Code:
Dim test As Label = Me.Controls("Label" & 1)
Generally avoid getting controls by name, or just getting controls period? Why should I avoid it, is there a better way?
Thanks for the code though.
-
Mar 17th, 2009, 11:03 PM
#7
Re: [RESOLVED] Create a Control During RunTime
 Originally Posted by Vectris
Generally avoid getting controls by name, or just getting controls period?
 Originally Posted by jmcilhinney
If you want to get a control by name, which you should generally avoid if possible,
 Originally Posted by Vectris
Why should I avoid it, is there a better way?
You generally shouldn't rely on the names of controls to identify them, although sometimes it's unavoidable. Otherwise you should use the member variable created to identify them. If you need to loop through a specific set of controls then I'd suggest creating an array or collection. The best way to do something depends on exactly what you're trying to achieve, which we can't really tell from what you've provided to date. You'd never use code exactly like that so it must be an example, but an example of exactly what we don't know.
-
Mar 21st, 2010, 12:00 AM
#8
Addicted Member
Re: [RESOLVED] Create a Control During RunTime
Control created at runtime not working for me. I have read this thread in hopes that I could gain some knowledge and use it in my project but alas, I just can't seem to get it to work. I have a module that is passed a bool value to determine if additional labels are to be created at runtime. This is what I have.
vb Code:
Public Sub loadbottomblack(ByVal checked As Boolean)
If checked = True Then
For counter = 1 To 12
Dim testlabel As New Label
With testlabel
.Name = "testlabel" & counter
.Image = Image.FromFile("..\checkerGreentileRed.gif")
.AutoSize = False
.Size = New Size(45, 45)
.Location = frmBoard(New Point(950 + counter, 500))
.Visible = True
.BringToFront()
End With
Next
End If
End Sub
If controls are to be created they are to be created on frmboard, and on top of a picturebox. I have tried several different ways of doing this. I can't seem to get these controls to populate. I understand that my .location property above does not read frmBoard.picBoard. I have tried that also and nothing. Any suggestions?
Abrium
Asking the beginners questions so you don't have to!
If by chance hell actually froze over and I some how helped you... Please rate.
-
Mar 21st, 2010, 12:13 AM
#9
Addicted Member
Re: [RESOLVED] Create a Control During RunTime
Hahaha I forgot the .Parent container property. My mistake. Just so everyone else knows. When creating a control on an app with multiple forms/modules make sure you use the .Parent Property. Example below:
vb Code:
Public Sub loadbottomblack(ByVal checked As Boolean)
If checked = True Then
For counter = 1 To 12
Dim testlabel As New Windows.Forms.Label
With testlabel
.Name = "testlabel" & counter
.Image = Image.FromFile("..\checkerGreentileRed.gif")
.AutoSize = False
.Size = New Size(45, 45)
[COLOR="Red"].Parent = frmBoard[/COLOR]
.Location = New Point(900 + counter, 500)
.Visible = True
.BringToFront()
End With
Next
End If
End Sub
Happy coding!
Abrium
Asking the beginners questions so you don't have to!
If by chance hell actually froze over and I some how helped you... Please rate.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|