[RESOLVED] [2005] Small Iteration Puzzling.
I am Adding Lables to a form, one at a time.
I want the caption/text of each label to be different from the other, in that if the form contains, Label1, i iterate through it and when i add the next, it should be Lable2....etc. As the numbers on the far end add on. How can i do this.
This is what i have.
VB Code:
For i As Integer = 0 To Me.Controls.Count
If lbl.Text.ToLower = "added label" Then
i += 1
lbl.Text &= i
Else
lbl.Text = "Added Label"
End If
Next
Re: [2005] Small Iteration Puzzling.
How are you adding your labels in?
Re: [2005] Small Iteration Puzzling.
VB Code:
Private Sub cmnuAddLabel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmnuAddLabel.Click
lbl = New Label
lbl.Name = "**AddedLabel"
For i As Integer = 0 To Me.Controls.Count
If lbl.Text.ToLower = "added label" Then
i += 1
lbl.Text &= i
Else
lbl.Text = "Added Label"
End If
Next
AddHandler lbl.MouseDown, AddressOf MyEventHandler
lbl.Location = pt
lbl.AutoSize = True
Me.Controls.Add(lbl)
End Sub
What i think i need is to iterate through all labels, getting the last number on thier right. Then get the maximum number of all and add one to it as i am adding the new lable. But how can i do this?
Re: [2005] Small Iteration Puzzling.
Ok here is a very simple way to do this...On your form add a global variable like so...
VB Code:
Dim maxLabelNumber As Integer = 0
Now in the loop just do the following:
VB Code:
Dim lbl As Label = New Label
maxLabelNumber += 1
lbl.Name = "Label" & maxLabelNumber.ToString()
Me.Controls.Add(lbl)
Re: [2005] Small Iteration Puzzling.
That wont work since i some time remove the controls from the form. So i wont be able to track the MaxLabelNumber.
Re: [2005] Small Iteration Puzzling.
Quote:
Originally Posted by maps
That wont work since i some time remove the controls from the form. So i wont be able to track the MaxLabelNumber.
Put all the controls in a panel, then remove the panel when need be.
Re: [2005] Small Iteration Puzzling.
Quote:
Originally Posted by maps
That wont work since i some time remove the controls from the form. So i wont be able to track the MaxLabelNumber.
Do the numbers always need to be sequential? So the numbers have coorespond with the exact amount of labels on the form (if this is the case then you need to renumber ALL the labels again when you remove one)? If not then you can just keep incrementing the number. Let me know! :ehh:
Re: [2005] Small Iteration Puzzling.
Yes, i want the numbers to be sequential.But still i dont want to go through the renaming hurstle. There should be a way out.
Re: [2005] Small Iteration Puzzling.
Quote:
Originally Posted by maps
Yes, i want the numbers to be sequential.But still i dont want to go through the renaming hurstle. There should be a way out.
Well let's say you have 10 labels named Label1 - Label10 and then you delete Label5. This leaves you with 9 labels 1-4 and 6-10. Now for the next label you add you have the choice. Create Label11, create Label11 and rename 6 to 5, 7 to 6, 8 to 7, etc... or you can just make the new label Label5. Which of those absolutely has to be the case?
If it is mission critical that you have no gaps in numbers that when you delete a label you need to fill these gaps and you can decrement the count by how ever many you delete. If you allows the gaps in the numbers its really easy.
Re: [2005] Small Iteration Puzzling.
You could create your lable's at design time, then toggle the visible property.
1 Attachment(s)
Re: [2005] Small Iteration Puzzling.
Wild Bill. If you read my first post, you see that i create my labels programatically, one at a time.
Any Idea?
This is what i am trying to do.
Re: [2005] Small Iteration Puzzling.
Use a SortedDictionary with an Integer as the key and the Label as the value. Loop through the list to find the first gap and, if you find one, use the Integer that would go there as the number of the Label. If you don't find a gap then you just use the next number:
VB Code:
Private labelsByNumber As New SortedDictionary(Of Integer, Label)
Private Sub AddLabel()
Dim lbl As New Label
'Set properties here.
Dim labelNumber As Integer = 1
'Find the first label number not in the collection.
While Me.labelsByNumber.ContainsKey(labelNumber)
labelNumber += 1
End While
lbl.Text = "Label" & labelNumber
Me.labelsByNumber.Add(labelNumber, lbl)
Me.Controls.Add(lbl)
End Sub
Private Sub RemoveLabel(ByVal lbl As Label)
For Each key As Integer In Me.labelsByNumber.Keys
If Me.labelsByNumber(key) Is lbl Then
Me.labelsByNumber.Remove(key)
Me.Controls.Remove(lbl)
lbl.Dispose()
Exit For
End If
Next key
End Sub
Re: [2005] Small Iteration Puzzling.
Thats EXACTLY what i wanted. TTTTTTTHX a million.
Your code not only loves every Australian but everyone.