[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)
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.
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.
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:
Private Function AddLabel(ByVal loc As Point) As Label
lbl = New Label
lbl.Name = "lbl" & currentNo
lbl.Text = currentNo
lbl.Location = loc
currentNo += 1
Return lbl
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.
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
Re: Setting Labels in a Loop
Quote:
Originally Posted by
Deslyxia
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 :wave:
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:
frmControl.Controls.Remove "lblLabel1"
Re: Setting Labels in a Loop
Try this to remove them:
vb Code:
Do Until currentNo = -1
If Me.PictureBox1.Controls.ContainsKey("lbl" & currentNo) Then
Me.PictureBox1.Controls.RemoveByKey("lbl" & currentNo)
End If
currentNo -= 1
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
Re: Setting Labels in a Loop
Man that works perfect thank you so much for your help
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!
Re: [RESOLVED] Setting Labels in a Loop