Solved

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

Posted on 2012-04-02
20
283 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
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
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 

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 500 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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
VB.Net - KeyPress Event 4 36
C# bracket error 3 31
VB.Net/LINQ: ".Contains" condition not finding existing result in MSSQL 16 35
Send SMS from VB via Sierra Wireless Modem 2 21
ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…

773 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