Results 1 to 13 of 13

Thread: [RESOLVED] [2005] Small Iteration Puzzling.

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    May 2006
    Posts
    426

    Resolved [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:
    1. For i As Integer = 0 To Me.Controls.Count
    2.     If lbl.Text.ToLower = "added label" Then
    3.         i += 1
    4.         lbl.Text &= i
    5.     Else
    6.         lbl.Text = "Added Label"
    7.     End If
    8.  
    9. Next

  2. #2
    Addicted Member BobTheBuilder.'s Avatar
    Join Date
    Apr 2005
    Location
    Ohio
    Posts
    149

    Re: [2005] Small Iteration Puzzling.

    How are you adding your labels in?

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    May 2006
    Posts
    426

    Re: [2005] Small Iteration Puzzling.

    VB Code:
    1. Private Sub cmnuAddLabel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmnuAddLabel.Click
    2.         lbl = New Label
    3.         lbl.Name = "**AddedLabel"
    4.         For i As Integer = 0 To Me.Controls.Count
    5.             If lbl.Text.ToLower = "added label" Then
    6.                 i += 1
    7.                 lbl.Text &= i
    8.             Else
    9.                 lbl.Text = "Added Label"
    10.             End If
    11.  
    12.         Next
    13.         AddHandler lbl.MouseDown, AddressOf MyEventHandler
    14.         lbl.Location = pt
    15.         lbl.AutoSize = True
    16.         Me.Controls.Add(lbl)
    17.     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?
    Last edited by maps; Sep 12th, 2006 at 11:38 AM.

  4. #4
    Addicted Member BobTheBuilder.'s Avatar
    Join Date
    Apr 2005
    Location
    Ohio
    Posts
    149

    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:
    1. Dim maxLabelNumber As Integer = 0

    Now in the loop just do the following:
    VB Code:
    1. Dim lbl As Label = New Label
    2.  
    3. maxLabelNumber += 1
    4. lbl.Name = "Label" & maxLabelNumber.ToString()
    5. Me.Controls.Add(lbl)

  5. #5

    Thread Starter
    Hyperactive Member
    Join Date
    May 2006
    Posts
    426

    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.

  6. #6
    Code Monkey wild_bill's Avatar
    Join Date
    Mar 2005
    Location
    Montana
    Posts
    2,993

    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.

  7. #7
    Addicted Member BobTheBuilder.'s Avatar
    Join Date
    Apr 2005
    Location
    Ohio
    Posts
    149

    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!

  8. #8

    Thread Starter
    Hyperactive Member
    Join Date
    May 2006
    Posts
    426

    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.

  9. #9
    Addicted Member BobTheBuilder.'s Avatar
    Join Date
    Apr 2005
    Location
    Ohio
    Posts
    149

    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.

  10. #10
    Code Monkey wild_bill's Avatar
    Join Date
    Mar 2005
    Location
    Montana
    Posts
    2,993

    Re: [2005] Small Iteration Puzzling.

    You could create your lable's at design time, then toggle the visible property.

  11. #11

    Thread Starter
    Hyperactive Member
    Join Date
    May 2006
    Posts
    426

    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.
    Attached Images Attached Images  
    Last edited by maps; Sep 13th, 2006 at 01:12 AM.

  12. #12
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,222

    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:
    1. Private labelsByNumber As New SortedDictionary(Of Integer, Label)
    2.  
    3. Private Sub AddLabel()
    4.     Dim lbl As New Label
    5.  
    6.     'Set properties here.
    7.  
    8.     Dim labelNumber As Integer = 1
    9.  
    10.     'Find the first label number not in the collection.
    11.     While Me.labelsByNumber.ContainsKey(labelNumber)
    12.         labelNumber += 1
    13.     End While
    14.  
    15.     lbl.Text = "Label" & labelNumber
    16.  
    17.     Me.labelsByNumber.Add(labelNumber, lbl)
    18.     Me.Controls.Add(lbl)
    19. End Sub
    20.  
    21. Private Sub RemoveLabel(ByVal lbl As Label)
    22.     For Each key As Integer In Me.labelsByNumber.Keys
    23.         If Me.labelsByNumber(key) Is lbl Then
    24.             Me.labelsByNumber.Remove(key)
    25.             Me.Controls.Remove(lbl)
    26.             lbl.Dispose()
    27.  
    28.             Exit For
    29.         End If
    30.     Next key
    31. End Sub
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  13. #13

    Thread Starter
    Hyperactive Member
    Join Date
    May 2006
    Posts
    426

    Re: [2005] Small Iteration Puzzling.

    Thats EXACTLY what i wanted. TTTTTTTHX a million.
    Your code not only loves every Australian but everyone.

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