ListBox (Multi-Extended Selection Mode)

  • Thread starter Thread starter Steven Smith
  • Start date Start date
S

Steven Smith

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

///
 
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.
 
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
 
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
 
-----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
 
Steven Smith said:
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())
///
 
-----Original Message-----
"Steven Smith" <[email protected]> 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
 
-----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. SubClass
this and override the toString() function and return the strings of all the
items in the collection.

///
Public Overrides Function toString() As String

Return text

End Function
///

now I see the light..!
 
Steven Smith said:
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.

;-)
 
Back
Top