By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,853 Members | 1,012 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,853 IT Pros & Developers. It's quick & easy.

ListBox (Multi-Extended Selection Mode)

P: n/a
I'm trying to display the contents of the
me.NamesListBox.SelectedItems property to a label on the
form. This works fine in single selection mode but
there's obviously something wrong with my syntax when
trying to call multiple items. the line in my code below
gives me the pre-compiler error along the lines of

value of type 'system.windows.form.textbox selected
object collection cannot be converted to string

///
Me.ResultLabel.Text = Me.NamesListBox.SelectedItems
///

any ideas guys, thanks in advance

Steve


///

Private Sub ExitButton_Click(ByVal sender As Object,
ByVal e As System.EventArgs) _
Handles ExitButton.Click
Me.Close()
End Sub

Private Sub MultiForm_Load(ByVal sender As Object,
ByVal e As System.EventArgs) _
Handles MyBase.Load
Me.NamesListBox.Items.Add("Ahmad")
Me.NamesListBox.Items.Add("Jim")
Me.NamesListBox.Items.Add("Debbie")
Me.NamesListBox.Items.Add("Jeanne")
Me.NamesListBox.Items.Add("Bill")
End Sub

Private Sub SingleButton_Click(ByVal sender As
Object, ByVal e As System.EventArgs) _
Handles SingleButton.Click

Me.ResultLabel.Text = Me.NamesListBox.SelectedItem

End Sub

Private Sub MultiButton_Click(ByVal sender As Object,
ByVal e As System.EventArgs) _
Handles MultiButton.Click

Me.ResultLabel.Text = ""
Me.ResultLabel.Text =
Me.NamesListBox.SelectedItems

///
Nov 20 '05 #1
Share this Question
Share on Google+
10 Replies


P: n/a
Firstly, you are getting the error because you did not specify .ToString()
on the end.

Secondly, this will not display anyting meaningfull, simply the
windows.forms.listbox.selecteditemscollection etc

If you want to display a messagebox of all the values concatenated. SubClass
this and override the toString() function and return the strings of all the
items in the collection.
;-D

HTH

--
Regards - One Handed Man

Author : Fish .NET & Keep .NET
=========================================
This posting is provided "AS IS" with no warranties,
and confers no rights.
"Steven Smith" <St**********@emailaccount.com> wrote in message
news:1c****************************@phx.gbl...
I'm trying to display the contents of the
me.NamesListBox.SelectedItems property to a label on the
form. This works fine in single selection mode but
there's obviously something wrong with my syntax when
trying to call multiple items. the line in my code below
gives me the pre-compiler error along the lines of

value of type 'system.windows.form.textbox selected
object collection cannot be converted to string

///
Me.ResultLabel.Text = Me.NamesListBox.SelectedItems
///

any ideas guys, thanks in advance

Steve


///

Private Sub ExitButton_Click(ByVal sender As Object,
ByVal e As System.EventArgs) _
Handles ExitButton.Click
Me.Close()
End Sub

Private Sub MultiForm_Load(ByVal sender As Object,
ByVal e As System.EventArgs) _
Handles MyBase.Load
Me.NamesListBox.Items.Add("Ahmad")
Me.NamesListBox.Items.Add("Jim")
Me.NamesListBox.Items.Add("Debbie")
Me.NamesListBox.Items.Add("Jeanne")
Me.NamesListBox.Items.Add("Bill")
End Sub

Private Sub SingleButton_Click(ByVal sender As
Object, ByVal e As System.EventArgs) _
Handles SingleButton.Click

Me.ResultLabel.Text = Me.NamesListBox.SelectedItem

End Sub

Private Sub MultiButton_Click(ByVal sender As Object,
ByVal e As System.EventArgs) _
Handles MultiButton.Click

Me.ResultLabel.Text = ""
Me.ResultLabel.Text =
Me.NamesListBox.SelectedItems

///

Nov 20 '05 #2

P: n/a
Cor
Hi Steve,
///
Me.ResultLabel.Text = Me.NamesListBox.SelectedItems
///

I d'nt know if the Option I give beneath is Strict the best but I think you
can go On with it.
\\\\
Dim i As Integer
Me.ResultLabel.Text = Nothing
For i = 0 To NamesListBox.SelectedItems.Count - 1
Me.ResultLabel.Text = Me.ResultLabel.Text &
NamesListBox.SelectedItems(i).ToString & vbCrLf
Next
////
Success
Cor
Nov 20 '05 #3

P: n/a

Thanks Cor that worked perfectly, I think thats what One
Handed Man was trying to explain I'm gonna go study it
through to find out how it works exactly, thanks again to
both of you for your help, keep up the good work

Regards, Steve
Nov 20 '05 #4

P: n/a
Cor
OK Steve ,
No he was not this is another Option,
Nice that you did reply.
Cor
Nov 20 '05 #5

P: n/a
:-))
Nov 20 '05 #6

P: n/a
-----Original Message-----
:-)) -----Original Message-----
:-))


Thanks for in depth analysis of Tic-Tac-Toe, mucho
gracias! on closer examination the first solution I
posted never worked as my error checking at the time
disabled the labels after click to prevent player from
overwriting other players marker, this was a cunning plan
however re-enabling the labels on ClearBoard.Click might
have helped. This leads me to believe that the original
do while...loop served the same functionality as the
later for...next, but I realise that there is a
difference between the two. The next tutorial for me is
a hangman game which I'm sure will throw up plenty new
string manipulation problems for me to chew over, will
continue to work on style to maximise legibility of my
code, thanks again, bye for now...
Regards, Steve
Nov 20 '05 #7

P: n/a
"Steven Smith" <St**********@emailaccount.com> schrieb:
I'm trying to display the contents of the
me.NamesListBox.SelectedItems property to a label on the
form. This works fine in single selection mode but
there's obviously something wrong with my syntax when
trying to call multiple items. the line in my code below
gives me the pre-compiler error along the lines of

value of type 'system.windows.form.textbox selected
object collection cannot be converted to string

///
Me.ResultLabel.Text = Me.NamesListBox.SelectedItems


\\\
Dim o As Object
Dim sb As System.Text.StringBuilder = New System.Text.StringBuilder()
For Each o In Me.ListBox1.SelectedItems

' Change this depending on the type of your objects.
sb.Append(DirectCast(o, String))

sb.Append(" ")
Next o
MsgBox(sb.ToString())
///

--
Herfried K. Wagner
MVP VB Classic, VB.NET
http://www.mvps.org/dotnet
Nov 20 '05 #8

P: n/a
-----Original Message-----
"Steven Smith" <St**********@emailaccount.com> schrieb: \\\
Dim o As Object
Dim sb As System.Text.StringBuilder = New System.Text.StringBuilder()For Each o In Me.ListBox1.SelectedItems

' Change this depending on the type of your objects.
sb.Append(DirectCast(o, String))

sb.Append(" ")
Next o
MsgBox(sb.ToString())
///

--
Herfried K. Wagner
MVP VB Classic, VB.NET
http://www.mvps.org/dotnet
.
Hi Herfried thanks for your response, I think this

solution is slightly beyond my ability just now as
DirectCast isn't in my vb vocabulary yet, although it is
always helpful to see there's more than one way to skin a
cat :) I'll try and implement the code in another project
and I'll see how I get on.

Thanks & appreciation for your help

Regards Steve
Nov 20 '05 #9

P: n/a
-----Original Message-----
Firstly, you are getting the error because you did not specify .ToString()on the end.
If you want to display a messagebox of all the values concatenated. SubClassthis and override the toString() function and return the strings of all theitems in the collection.


///
Public Overrides Function toString() As String

Return text

End Function
///

now I see the light..!
Nov 20 '05 #10

P: n/a
"Steven Smith" <St**********@emailaccount.com> schrieb:
solution is slightly beyond my ability just now as
DirectCast isn't in my vb vocabulary yet, although it is
always helpful to see there's more than one way to skin a
cat :) I'll try and implement the code in another project
and I'll see how I get on.


Have a look at the documentation for 'CType' and 'DirectCast'. There is a
difference you should know.

;-)

--
Herfried K. Wagner
MVP VB Classic, VB.NET
http://www.mvps.org/dotnet
Nov 20 '05 #11

This discussion thread is closed

Replies have been disabled for this discussion.