Answered by:
Listbox multi select

Question
-
User1542511682 posted
Hi there,
I have a listbox (20 Items) with multiselection and label that disply the selection and one button
So I wrote (it works fine)
Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim i As Integer
Label1.Text = " "
For i = 0 To ListBox1.Items.Count - 1
If ListBox1.Items(i).Selected Then
Label1.Text = Label1.Text & ListBox1.Items(i).Text & ", ")
End If
Next
End SubNow when I click on button the label show lets say Item2,Item5,Item10,
How do I take off the last ","
somthing like that Item2,Item5,Item10I used to do
Dim s As String = Trim(Label1.Text)
Label1.Text = s.Substring(0, s.Length - 1)but it give me an error converting intiger to string
Thanks,
Oded Dror
Saturday, December 25, 2010 12:45 PM
Answers
-
User1992938117 posted
Use below code:
Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click Dim i As Integer Dim j As Integer Dim k As Integer Label1.Text = " " For i = 0 To ListBox1.Items.Count - 1 If ListBox1.Items(i).Selected Then j = j + 1 End If Next For i = 0 To ListBox1.Items.Count - 1 If ListBox1.Items(i).Selected Then k = k + 1 If (j = k) Then Label1.Text = Label1.Text & ListBox1.Items(i).Text Else Label1.Text = Label1.Text & ListBox1.Items(i).Text & ", " End If End If Next End Sub
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Saturday, December 25, 2010 2:28 PM -
User-1887867400 posted
Hi, you are appending ", " i.e a comma and a space. So, either you extract the substring upto (Length-2) or you use the following code:
Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click Dim i As Integer Label1.Text = " " For i = 0 To ListBox1.Items.Count - 1 If ListBox1.Items(i).Selected Then Label1.Text = Label1.Text & ListBox1.Items(i).Text & ", " End If Next 'Obtain index of last comma (,) Dim lastCommIndex As Integer = Label1.Text.LastIndexOf(",") 'Extract only required sub string. Label1.Text = Label1.Text.Substring(0, lastCommIndex) End Sub
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Saturday, December 25, 2010 2:38 PM -
User-1887867400 posted
Hi Rajneesh your code may work but its using 2 loops and will increase the execution time of this code. So i think ur approach isn't more efficient.
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Saturday, December 25, 2010 2:46 PM -
User1992938117 posted
your code may workIt surely work. I have tested it..
its using 2 loops and will increase the execution time of this code
I am agree with you.
//Each developer has its own approach........
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Saturday, December 25, 2010 3:05 PM
All replies
-
User1992938117 posted
Use below code:
Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click Dim i As Integer Dim j As Integer Dim k As Integer Label1.Text = " " For i = 0 To ListBox1.Items.Count - 1 If ListBox1.Items(i).Selected Then j = j + 1 End If Next For i = 0 To ListBox1.Items.Count - 1 If ListBox1.Items(i).Selected Then k = k + 1 If (j = k) Then Label1.Text = Label1.Text & ListBox1.Items(i).Text Else Label1.Text = Label1.Text & ListBox1.Items(i).Text & ", " End If End If Next End Sub
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Saturday, December 25, 2010 2:28 PM -
User-1887867400 posted
Hi, you are appending ", " i.e a comma and a space. So, either you extract the substring upto (Length-2) or you use the following code:
Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click Dim i As Integer Label1.Text = " " For i = 0 To ListBox1.Items.Count - 1 If ListBox1.Items(i).Selected Then Label1.Text = Label1.Text & ListBox1.Items(i).Text & ", " End If Next 'Obtain index of last comma (,) Dim lastCommIndex As Integer = Label1.Text.LastIndexOf(",") 'Extract only required sub string. Label1.Text = Label1.Text.Substring(0, lastCommIndex) End Sub
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Saturday, December 25, 2010 2:38 PM -
User-1887867400 posted
Hi Rajneesh your code may work but its using 2 loops and will increase the execution time of this code. So i think ur approach isn't more efficient.
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Saturday, December 25, 2010 2:46 PM -
User1992938117 posted
your code may workIt surely work. I have tested it..
its using 2 loops and will increase the execution time of this code
I am agree with you.
//Each developer has its own approach........
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Saturday, December 25, 2010 3:05 PM -
User1542511682 posted
Rajneesh,
Thank you for contacting me
I found another way
Left(Label1.Text, (Len(Label1.Text) - 1))
Thanks,
Oded Dror
Sunday, December 26, 2010 6:32 AM -
User1542511682 posted
vijaypant ,
Thank you for contacting me
I found another way
Left(Label1.Text, (Len(Label1.Text) - 1))
Thanks,
Oded Dror
Sunday, December 26, 2010 6:34 AM