|
-
Oct 15th, 2012, 01:11 AM
#1
Thread Starter
Member
How can i multi select items in listbox and pass values to a textbox
Please I need help.
I have a listbox that has multiselect property.
I am having problems writing code to help me multi select items in the listbox and on a click event, items will display on a textbox that has a multi line property.
Thanks in advance
-
Oct 15th, 2012, 01:32 AM
#2
Re: How can i multi select items in listbox and pass values to a textbox
Um, you posted this exact same question in the VB6 forum a couple of hours ago. Firstly, please don't double post. If you find that you have posted in the wrong forum then please ask the moderators to move the thread. VB6 and VB.NET are two different languages so if people start posting to both threads then at least one group is going to be wasting their time. That's not the way to make friends.
So, which language are you using? Is it VB6 or VB.NET? Note that VB 2005 and later are VB.NET. We can then get the mods to delete the irrelevant thread and people can try to help you out, safe in the knowledge that there answers will be relevant and their time not wasted.
-
Oct 15th, 2012, 01:37 AM
#3
Thread Starter
Member
Re: How can i multi select items in listbox and pass values to a textbox
-
Oct 15th, 2012, 01:43 AM
#4
Re: How can i multi select items in listbox and pass values to a textbox
Assuming that the items in the ListBox are Strings in the first place, the simplest option is:
Code:
myTextBox.Lines = myListBox.SelectedItems.Cast(Of String)().ToArray()
That creates a String array from the selected items and displays it one element per line in the TextBox.
-
Oct 15th, 2012, 01:47 AM
#5
Thread Starter
Member
Re: How can i multi select items in listbox and pass values to a textbox
I tried your code and this error came up
Unable to cast object of type 'System.Data.DataRowView' to type 'System.String
Please help
-
Oct 15th, 2012, 02:11 AM
#6
Re: How can i multi select items in listbox and pass values to a textbox
try this:
vb.net Code:
Private Sub ListBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ListBox1.SelectedIndexChanged
If ListBox1.DisplayMember = "" Then Return
TextBox1.Lines = ListBox1.SelectedItems.Cast(Of DataRowView).Select(Function(drv) drv.Item(ListBox1.DisplayMember).ToString).ToArray()
End Sub
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Oct 15th, 2012, 03:39 AM
#7
Re: How can i multi select items in listbox and pass values to a textbox
 Originally Posted by smile4kenny
I tried your code and this error came up
Unable to cast object of type 'System.Data.DataRowView' to type 'System.String
Please help
Given that I said:
Assuming that the items in the ListBox are Strings in the first place
that's not a surprise.
 Originally Posted by .paul.
try this:
vb.net Code:
Private Sub ListBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ListBox1.SelectedIndexChanged
If ListBox1.DisplayMember = "" Then Return
TextBox1.Lines = ListBox1.SelectedItems.Cast(Of DataRowView).Select(Function(drv) drv.Item(ListBox1.DisplayMember).ToString).ToArray()
End Sub
There's a better approach that will work in all cases:
Code:
TextBox1.Lines = ListBox1.SelectedItems.
Cast(Of Object).
Select(Function(o) ListBox1.GetItemText(o)).
ToArray()
-
Oct 15th, 2012, 09:03 AM
#8
Thread Starter
Member
Re: How can i multi select items in listbox and pass values to a textbox
Okay thanks bro...
I modified your code and it worked this way
For Each objDataRowView As DataRowView In Me.ClassTitleListBox.SelectedItem
Me.TextBox1.Text &= (objDataRowView("ClassTitle").ToString() & ",")
Next
But, If I wanna save the output to an array instead of displaying on a textbox, do you know how I can do it?
Thanks in advance
-
Oct 15th, 2012, 09:13 AM
#9
Re: How can i multi select items in listbox and pass values to a textbox
Rather than an array, use a List(of String). Your inner line changes from:
Me.TextBox1.Text &= (objDataRowView("ClassTitle").ToString() & ",")
into
whateverMyListIsCalled.Add(objDataRowView("ClassTitle").ToString)
You just have to declare a New List(of String) with whatever name is better than what I used as a name.
My usual boring signature: Nothing
 
-
Oct 15th, 2012, 10:50 AM
#10
Thread Starter
Member
Re: How can i multi select items in listbox and pass values to a textbox
Hi Shaggy,
what if I am using this code, how can I store the output as a list of string
If CourseTitleListBox.DisplayMember = "" Then Return
TextBox1.Lines = CourseTitleListBox.SelectedItems.Cast(Of DataRowView).Select(Function(drv) drv.Item(CourseTitleListBox.DisplayMember).ToString).ToArray()
-
Oct 15th, 2012, 11:25 AM
#11
Thread Starter
Member
Re: How can i multi select items in listbox and pass values to a textbox
Please someone help!
I need to store the output in an array or string with comma seperating each item, because I will run an sql query that will search each of the stored item with a where statement.
-
Oct 15th, 2012, 11:32 AM
#12
Re: How can i multi select items in listbox and pass values to a textbox
The code you just showed already returns an array of string. That's what the .ToArray is. Alternatively, you could still use a list by changing that to .ToList, but since you are using that code, I see no reason to do that. TextBox1.Lines is an array of string. You could also have your own array of strings:
Dim MyArray() As String
and assign to that rather than assigning to TextBox1.Lines.
My usual boring signature: Nothing
 
-
Oct 15th, 2012, 11:44 AM
#13
Thread Starter
Member
Re: How can i multi select items in listbox and pass values to a textbox
Thanks Shaggy I really do appreciate.
Instead of using an array,
do you know how I can store this as a string with comma seperating each items
-
Oct 15th, 2012, 01:00 PM
#14
Thread Starter
Member
Re: How can i multi select items in listbox and pass values to a textbox
When I assign to Dim MyArray() As String
The output is system.String[]
-
Oct 15th, 2012, 01:02 PM
#15
Re: How can i multi select items in listbox and pass values to a textbox
dim outPut as string = string.join(",", CourseTitleListBox.SelectedItems.Cast(Of DataRowView).Select(Function(drv) drv.Item(CourseTitleListBox.DisplayMember).ToString).ToArray())
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Oct 15th, 2012, 01:20 PM
#16
Thread Starter
Member
Re: How can i multi select items in listbox and pass values to a textbox
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|