Solved

Help with adding sequential numbers to each row added in a list box

Posted on 2012-04-02
20
280 Views
Last Modified: 2012-06-21
Hi,

How do I create a loop to enter (increment) a number in front of each line I add in my list box? For example when I add the first line, I would like to see:

1 – data
 And when adding data aagain,

1 – data
2- data

etc..

However, if I have 4 lines and the user deletes lines 3, I would like like 4 to automatically become line 3.

ListBox1.Items.Add(C1NSN.Text)
        ListBox1.DataBind()
        'Dim checkInteger As Integer
        For i As Integer = 0 To ListBox1.Items.Count - 1
            Math.DivRem(i + 1, 2, checkInteger)
            If checkInteger = 0 Then
                ListBox1.Items(i).Attributes.Add("style", "background-color: Grey")
            Else
                ListBox1.Items(i).Attributes.Add("style", "background-color: Silver")
            End If
        Next

Thanks,
Victor
0
Comment
Question by:vcharles
  • 12
  • 8
20 Comments
 

Author Comment

by:vcharles
Comment Utility
Hi again,

Also is it possible during the form load event to load the listbox with 5 rows with alternating background colors?

Victor
0
 

Author Comment

by:vcharles
Comment Utility
Hello,

To rephrase the second part of my question.

Also is it possible during the form load event to load the listbox with 5 rows (numbered) with alternating background colors?  For example:

1-
2-
3-
4-
5-
0
 
LVL 13

Expert Comment

by:Rick
Comment Utility
Try something like this: after you bind the data to your listbox, call a subroutine to format it the way you want:

Sub FormatListBox()

        For i As Integer = ListBox1.Items.Count - 1 To 0 Step -1

            ListBox1.Items(i).Text = i & "-" & ListBox1.Items(i).ToString

            If i Mod 2 = 0 Then
                ListBox1.Items(i).Attributes.Add("style", "background-color: Grey")
            End If

        Next

End Sub

Open in new window

0
 

Author Comment

by:vcharles
Comment Utility
Hi,

Do you mean i the button click event add code in part A, which will call code in Part B?

A. ListBox1.Items.Add(C1NSN.Text)
        ListBox1.DataBind() 'New Line
        FormatListBox()
       


B. Sub FormatListBox()

        For i As Integer = ListBox1.Items.Count - 1 To 0 Step -1

            ListBox1.Items(i).Text = i & "-" & ListBox1.Items(i).ToString

            If i Mod 2 = 0 Then
                ListBox1.Items(i).Attributes.Add("style", "background-color: Grey")
            End If

        Next

End Sub

Thanks
0
 
LVL 13

Expert Comment

by:Rick
Comment Utility
Yes. Try it.
0
 

Author Comment

by:vcharles
Comment Utility
Thanks, after I load the data in the listbox as
1- Data
2-Data
3-Data

How do I loop through the rows and copy all the rows in a textbox in the following format.

1-Data; 2-Data; 3-Data (Format data will be saved in XML file)

and is there a way to display the data back in the List back as

1- Data
2-Data
3-Data
0
 
LVL 13

Expert Comment

by:Rick
Comment Utility
Under FormatListBox(), call a subroutine to copy the listbox items to the textbox. For example: CopyListToTextBox()

Sub CopyListToTextBox()

        TextBox1.Text = ListBox1.Items(0).ToString & "; "

        For i As Integer = 1 To ListBox1.Items.Count - 1
            TextBox1.Text += ListBox1.Items(i).ToString & "; "
        Next

    End Sub

Open in new window

0
 

Author Comment

by:vcharles
Comment Utility
Thanks, Can I do the reverse when I load the listbox again, for example go throught the following string in my text box and display the data in the following format:
1- data
2- data
3- data

etc..
0
 

Author Comment

by:vcharles
Comment Utility
Another important point, Besides having an Add, Delete button there will also be an amend button.  when the user selects a row in the listbox and presses Amend, how do I move its data to the textbox?
0
 
LVL 13

Expert Comment

by:Rick
Comment Utility
>> Thanks, Can I do the reverse when I load the listbox again, for example go throught the following string in my text box and display the data in the following format...

What do you mean?
Do you have a multiline textbox and want it to have this text?

1- data
2- data
3- data
0
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 

Author Comment

by:vcharles
Comment Utility
Hi again,

I figured out the Amend button, below is the code for all three buttons: I will test the Add button and get back to you.

Add:

ListBox1.Items.Add(C1NSN.Text)
 ListBox1.DataBind() 'New Line
 FormatListBox()
       

Sub FormatListBox()

        For i As Integer = ListBox1.Items.Count - 1 To 0 Step -1

            ListBox1.Items(i).Text = i & "-" & ListBox1.Items(i).ToString

            If i Mod 2 = 0 Then
                ListBox1.Items(i).Attributes.Add("style", "background-color: Grey")
            End If

        Next
CopyListToTextBox()
End Sub

Sub CopyListToTextBox()
TextBox1.Text = ListBox1.Items(0).ToString & "; "

        For i As Integer = 1 To ListBox1.Items.Count - 1
            TextBox1.Text += ListBox1.Items(i).ToString & "; "
        Next
    End Sub



Delete:
If ListBox1.SelectedIndex <> -1 Then
            ListBox1.Items.RemoveAt(ListBox1.SelectedIndex)
        End If

Amend:
C1NSN.Text = ListBox1.SelectedItem.Text
0
 

Author Comment

by:vcharles
Comment Utility
Hello,

Is there a way to start with 1 instead of 0, also if my list is a follows:

1.AAAAAAAAAA
2.BBBBBBBBBBBB
3.CCCCCCCCCCCC


If I delete line 2, can I automatically change 3 to 2

1.AAAAAAAAAA
2.CCCCCCCCCCCC
0
 
LVL 13

Expert Comment

by:Rick
Comment Utility
Hey, to start with 1, just change this:

Sub FormatListBox()

        For i As Integer = ListBox1.Items.Count - 1 To 0 Step -1

            ListBox1.Items(i).Text = i & "-" & ListBox1.Items(i).ToString

            If i Mod 2 = 0 Then
                ListBox1.Items(i).Attributes.Add("style", "background-color: Grey")
            End If

        Next

End Sub

Open in new window


To this:

Sub FormatListBox()

        For i As Integer = ListBox1.Items.Count - 1 To 0 Step -1

            ListBox1.Items(i).Text = i+1 & "-" & ListBox1.Items(i).ToString

            If i Mod 2 = 0 Then
                ListBox1.Items(i).Attributes.Add("style", "background-color: Grey")
            End If

        Next

End Sub

Open in new window



>> If I delete line 2, can I automatically change 3 to 2?

Yes. Whenever you delete an item. Just call FormatListBox() again.
0
 

Author Comment

by:vcharles
Comment Utility
Hello,

Just noticed when press the Add button multiple times I get duplicate numbers, for examples

0-0-0-0 Data
1-1-1-Data
2-2-Data

How do I fix this problem?

Thanks.
0
 
LVL 13

Expert Comment

by:Rick
Comment Utility
OK. Change this:

Sub FormatListBox()

        For i As Integer = ListBox1.Items.Count - 1 To 0 Step -1

            ListBox1.Items(i).Text = i+1 & "-" & ListBox1.Items(i).ToString

            If i Mod 2 = 0 Then
                ListBox1.Items(i).Attributes.Add("style", "background-color: Grey")
            End If

        Next

End Sub

Open in new window


To this:
    Sub FormatListBox()

        For i As Integer = ListBox1.Items.Count - 1 To 0 Step -1

            If Not ListBox1.Items(i).Text.StartsWith(i + 1 & "-") Then
                ListBox1.Items(i).Text = i + 1 & "-" & ListBox1.Items(i).ToString
            End If

            If i Mod 2 = 0 Then
                ListBox1.Items(i).Attributes.Add("style", "background-color: Grey")
            End If

        Next

    End Sub

Open in new window

0
 

Author Comment

by:vcharles
Comment Utility
Thanks, also is it possible to start the first line with 1- instead of 0-?
0
 
LVL 13

Expert Comment

by:Rick
Comment Utility
The first listbox item? FormatListBox() is taking care of that.
0
 

Author Comment

by:vcharles
Comment Utility
Thanks a lot, the only problem is with the delete. When I delete a row 2 in the list box it shows:

From:

1.AAAAAAAAAA
2.BBBBBBBBBBBB
3.CCCCCCCCCCCC

To:

1.AAAAAAAAAA
2.3.CCCCCCCCCCCC

How do I remove the 3.?
0
 
LVL 13

Accepted Solution

by:
Rick earned 500 total points
Comment Utility
OK. Sorry... try this:

    Sub FormatListBox()

        For i As Integer = ListBox1.Items.Count - 1 To 0 Step -1

            If (IsNumeric(ListBox1.Items(i).ToString.Substring(0, 1)) And ListBox1.Items(i).ToString.Contains("-")) Then
                ListBox1.Items(i).Text = i + 1 & "-" & ListBox1.Items(i).ToString.Substring(InStr(ListBox1.Items(i).ToString, "-"), ListBox1.Items(i).ToString.Length - InStr(ListBox1.Items(i).ToString, "-"))
            Else
                ListBox1.Items(i).Text = i + 1 & "-" & ListBox1.Items(i).ToString
            End If

            If i Mod 2 = 0 Then
                ListBox1.Items(i).Attributes.Add("style", "background-color: Grey")
            End If

        Next

    End Sub

Open in new window

0
 

Author Comment

by:vcharles
Comment Utility
THANK YOU!!!
0

Featured Post

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
This video discusses moving either the default database or any database to a new volume.
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now