[Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 296
  • Last Modified:

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

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
vcharles
Asked:
vcharles
  • 12
  • 8
1 Solution
 
vcharlesAuthor Commented:
Hi again,

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

Victor
0
 
vcharlesAuthor Commented:
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
 
RickCommented:
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
Veeam and MySQL: How to Perform Backup & Recovery

MySQL and the MariaDB variant are among the most used databases in Linux environments, and many critical applications support their data on them. Watch this recorded webinar to find out how Veeam Backup & Replication allows you to get consistent backups of MySQL databases.

 
vcharlesAuthor Commented:
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
 
RickCommented:
Yes. Try it.
0
 
vcharlesAuthor Commented:
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
 
RickCommented:
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
 
vcharlesAuthor Commented:
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
 
vcharlesAuthor Commented:
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
 
RickCommented:
>> 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
 
vcharlesAuthor Commented:
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
 
vcharlesAuthor Commented:
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
 
RickCommented:
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
 
vcharlesAuthor Commented:
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
 
RickCommented:
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
 
vcharlesAuthor Commented:
Thanks, also is it possible to start the first line with 1- instead of 0-?
0
 
RickCommented:
The first listbox item? FormatListBox() is taking care of that.
0
 
vcharlesAuthor Commented:
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
 
RickCommented:
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
 
vcharlesAuthor Commented:
THANK YOU!!!
0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

  • 12
  • 8
Tackle projects and never again get stuck behind a technical roadblock.
Join Now