Results 1 to 9 of 9

Thread: [RESOLVED] Create a Control During RunTime

  1. #1

    Thread Starter
    Fanatic Member Vectris's Avatar
    Join Date
    Dec 2008
    Location
    USA
    Posts
    941

    Resolved [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?
    If your problem is solved, click the Thread Tools button at the top and mark your topic as Resolved!

    If someone helped you out, click the button on their post and leave them a comment to let them know they did a good job

    __________________
    My Vb.Net CodeBank Submissions:
    Microsoft Calculator Clone
    Custom TextBox Restrictions
    Get the Text inbetween HTML Tags (or two words)

  2. #2
    PowerPoster 2.0 Negative0's Avatar
    Join Date
    Jun 2000
    Location
    Southeastern MI
    Posts
    4,367

    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?

  3. #3
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,479

    Re: Create a Control During RunTime

    you must have another control obscuring the new label. try this:

    vb Code:
    1. Dim lbl As New Label
    2. lbl.Text = "Hello"
    3. lbl.Location = New Point(20, 20)
    4. lbl.Visible = True
    5. lbl.Name = "Label1"
    6. Me.Controls.Add(lbl)
    7. lbl.bringtofront

  4. #4

    Thread Starter
    Fanatic Member Vectris's Avatar
    Join Date
    Dec 2008
    Location
    USA
    Posts
    941

    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.
    If your problem is solved, click the Thread Tools button at the top and mark your topic as Resolved!

    If someone helped you out, click the button on their post and leave them a comment to let them know they did a good job

    __________________
    My Vb.Net CodeBank Submissions:
    Microsoft Calculator Clone
    Custom TextBox Restrictions
    Get the Text inbetween HTML Tags (or two words)

  5. #5
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,344

    Re: Create a Control During RunTime

    Quote Originally Posted by Vectris View Post
    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:
    1. Dim test As Label = Me.Controls("Label" & 1)

  6. #6

    Thread Starter
    Fanatic Member Vectris's Avatar
    Join Date
    Dec 2008
    Location
    USA
    Posts
    941

    Re: Create a Control During RunTime

    Quote Originally Posted by jmcilhinney View Post
    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:
    1. 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.
    If your problem is solved, click the Thread Tools button at the top and mark your topic as Resolved!

    If someone helped you out, click the button on their post and leave them a comment to let them know they did a good job

    __________________
    My Vb.Net CodeBank Submissions:
    Microsoft Calculator Clone
    Custom TextBox Restrictions
    Get the Text inbetween HTML Tags (or two words)

  7. #7
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,344

    Re: [RESOLVED] Create a Control During RunTime

    Quote Originally Posted by Vectris View Post
    Generally avoid getting controls by name, or just getting controls period?
    Quote Originally Posted by jmcilhinney View Post
    If you want to get a control by name, which you should generally avoid if possible,
    Quote Originally Posted by Vectris View Post
    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.

  8. #8
    Addicted Member Abrium's Avatar
    Join Date
    Feb 2007
    Location
    The Great State of Texas
    Posts
    205

    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:
    1. Public Sub loadbottomblack(ByVal checked As Boolean)
    2.  
    3.         If checked = True Then
    4.             For counter = 1 To 12
    5.                 Dim testlabel As New Label
    6.                 With testlabel
    7.                     .Name = "testlabel" & counter
    8.                     .Image = Image.FromFile("..\checkerGreentileRed.gif")
    9.                     .AutoSize = False
    10.                     .Size = New Size(45, 45)
    11.                     .Location = frmBoard(New Point(950 + counter, 500))
    12.                     .Visible = True
    13.                     .BringToFront()
    14.                 End With
    15.             Next
    16.  
    17.         End If
    18.     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.

  9. #9
    Addicted Member Abrium's Avatar
    Join Date
    Feb 2007
    Location
    The Great State of Texas
    Posts
    205

    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:
    1. Public Sub loadbottomblack(ByVal checked As Boolean)
    2.  
    3.         If checked = True Then
    4.             For counter = 1 To 12
    5.                 Dim testlabel As New Windows.Forms.Label
    6.                 With testlabel
    7.                     .Name = "testlabel" & counter
    8.                     .Image = Image.FromFile("..\checkerGreentileRed.gif")
    9.                     .AutoSize = False
    10.                     .Size = New Size(45, 45)
    11.                     [COLOR="Red"].Parent = frmBoard[/COLOR]
    12.                     .Location = New Point(900 + counter, 500)
    13.                     .Visible = True
    14.                     .BringToFront()
    15.                 End With
    16.             Next
    17.  
    18.         End If
    19.     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
  •  



Click Here to Expand Forum to Full Width