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.
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?
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!
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.
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.