[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

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

Posted on 2012-04-02
20
Medium Priority
?
295 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
Get your Disaster Recovery as a Service basics

Disaster Recovery as a Service is one go-to solution that revolutionizes DR planning. Implementing DRaaS could be an efficient process, easily accessible to non-DR experts. Learn about monitoring, testing, executing failovers and failbacks to ensure a "healthy" DR environment.

 

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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…

656 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