?
Solved

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

Posted on 2012-04-02
20
Medium Priority
?
291 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 12
  • 8
20 Comments
 

Author Comment

by:vcharles
ID: 37796747
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
ID: 37796922
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
ID: 37798046
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
Free Backup Tool for VMware and Hyper-V

Restore full virtual machine or individual guest files from 19 common file systems directly from the backup file. Schedule VM backups with PowerShell scripts. Set desired time, lean back and let the script to notify you via email upon completion.  

 

Author Comment

by:vcharles
ID: 37798209
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
ID: 37801084
Yes. Try it.
0
 

Author Comment

by:vcharles
ID: 37801273
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
ID: 37802309
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
ID: 37802740
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
ID: 37802805
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
ID: 37802891
>> 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
 

Author Comment

by:vcharles
ID: 37802948
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
ID: 37803046
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
ID: 37803198
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
ID: 37803270
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
ID: 37803632
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
ID: 37803714
Thanks, also is it possible to start the first line with 1- instead of 0-?
0
 
LVL 13

Expert Comment

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

Author Comment

by:vcharles
ID: 37803809
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 2000 total points
ID: 37804332
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
ID: 37804829
THANK YOU!!!
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
Suggested Courses

800 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