Results 1 to 11 of 11

Thread: [RESOLVED] Setting Labels in a Loop

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Sep 2010
    Posts
    230

    Resolved [RESOLVED] Setting Labels in a Loop

    Basically what i am doing is clicking on a picture in a Picture box and posting a label to the spot i click on. in some instances i will need to put as many as 60 labels on an individual picture. Below is a snippet of code that works perfectly fine to place 1 point.

    What i would like to do is setup an array of labels. Something along the lines of
    Dim i As Integer
    i = 5
    Dim Lab(i) As Label

    then be able to loop through the code below until i get all my labels placed. The only issue is that when i try doing that i get an error that Label is a type and cannot be used as an expression.

    Is there another way to do this that i am not seeing?



    Below is the working code snippet

    Label3.Parent = PictureBox1
    Label3.Visible = True
    Label3.BackColor = Color.Transparent
    Label3.BringToFront()
    Label3.ForeColor = Color.White
    Label3.Text = "3"
    Label3.Location = New Point(LocalMousePosition.X, LocalMousePosition.Y)

  2. #2
    PowerPoster cicatrix's Avatar
    Join Date
    Dec 2009
    Location
    Moscow, Russia
    Posts
    3,654

    Re: Setting Labels in a Loop

    What's wrong with just drawing the text on the picturebox instead of using Label?

    Code:
    Private Sub PictureBox1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles PictureBox1.Paint
            Dim fnt As New Font("Arial", 10, FontStyle.Regular, GraphicsUnit.Pixel)
            Dim brsh As New SolidBrush(Color.White)
            e.Graphics.DrawString("3", fnt, brsh, LocalMousePosition.X, LocalMousePosition.Y)
    End Sub
    P.S. You are defining Lab(i) As Label, but you forget to instantiate them.
    All you say is that you have an array of labels, but you don't create any. You should create labels to use them:

    Code:
    Dim Lab(5) As Label
    For i = 0 To 5
       Lab(i) = New Label
    Next
    After this you can use them but I still think that you ought to draw the text directly on your picture box instead of creating so many labels.

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Sep 2010
    Posts
    230

    Re: Setting Labels in a Loop

    Ohh no no no. Im sorry if i was misleading in my post. I am only able to place 1 label onto the picture box. I have attempted to create the array of labels but VB will not allow me to do that and pops the error i mentioned above.

    I will give drawing a shot. I appreciate the advice.

  4. #4
    Fanatic Member
    Join Date
    Aug 2010
    Posts
    624

    Re: Setting Labels in a Loop

    Just create a new procedure for creating a new "unique" label like so:

    Declare globally:
    Code:
    Dim currentNo As Int32 = 0
    Dim lbl As Label
    Now this function
    vb Code:
    1. Private Function AddLabel(ByVal loc As Point) As Label
    2.  
    3.         lbl = New Label
    4.         lbl.Name = "lbl" & currentNo
    5.         lbl.Text = currentNo
    6.         lbl.Location = loc
    7.         currentNo += 1
    8.  
    9.         Return lbl
    10.     End Function

    obviously you're going to have to add more attributes to the new label to do what you want, but that should be a start. To add it to your picturebox you can do this:

    Code:
    PictureBox1.Controls.Add(AddLabel(New Point(20, 20)))
    It will create it at a point (20, 20) in relation to the picturebox.

    Or you can remove the "loc" parameter from the function and put a constant location for the lbl.Location property in the function, it's all up to you.

  5. #5

    Thread Starter
    Addicted Member
    Join Date
    Sep 2010
    Posts
    230

    Re: Setting Labels in a Loop

    J

    Im going to give this method a shot when i get home. I think this is exactly what i was trying to accomplish.

    Thank you

  6. #6
    Fanatic Member
    Join Date
    Aug 2010
    Posts
    624

    Re: Setting Labels in a Loop

    Quote Originally Posted by Deslyxia View Post
    J

    Im going to give this method a shot when i get home. I think this is exactly what i was trying to accomplish.

    Thank you
    Okay, post results here when you test it out

  7. #7

    Thread Starter
    Addicted Member
    Join Date
    Sep 2010
    Posts
    230

    Re: Setting Labels in a Loop

    J its working perfect. I set it up to accept the X,Y coordinates of my cursor as the location. I also modified the font color and size along with the background color to my liking and it works like a charm.

    I ran into a small issue with not being able to put the points too close together .... but i was able to solve that by decreasing the width of the label.

    I am working on setting up a command button that will loop until currentNo is 0 again and remove the labels basically clearing them out... but the last time i programmed i used a dos based C++ compiler so im trying to learn the syntax i can actually use vs ... things that were available in VB6 like :

    vb Code:
    1. frmControl.Controls.Remove "lblLabel1"

  8. #8
    Fanatic Member
    Join Date
    Aug 2010
    Posts
    624

    Re: Setting Labels in a Loop

    Try this to remove them:

    vb Code:
    1. Do Until currentNo = -1
    2.             If Me.PictureBox1.Controls.ContainsKey("lbl" & currentNo) Then
    3.                 Me.PictureBox1.Controls.RemoveByKey("lbl" & currentNo)
    4.             End If
    5.             currentNo -= 1
    6.         Loop

    And yeah I also found the need to change the label width when I was testing just then :P Sorry about that I should have though of it

  9. #9

    Thread Starter
    Addicted Member
    Join Date
    Sep 2010
    Posts
    230

    Re: Setting Labels in a Loop

    Man that works perfect thank you so much for your help

  10. #10
    Fanatic Member
    Join Date
    Aug 2010
    Posts
    624

    Re: Setting Labels in a Loop

    Haha no problem mate, I'm glad it worked for you! please mark the thread as resolved and leave rep if appropriate

    Cheers!

  11. #11

    Thread Starter
    Addicted Member
    Join Date
    Sep 2010
    Posts
    230

    Re: [RESOLVED] Setting Labels in a Loop

    Rep Sent

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