Connecting Tech Pros Worldwide Forums | Help | Site Map

ListBox (Multi-Extended Selection Mode)

Steven Smith
Guest
 
Posts: n/a
#1: Nov 20 '05
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

///

One Handed Man
Guest
 
Posts: n/a
#2: Nov 20 '05

re: ListBox (Multi-Extended Selection Mode)


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" <Steven.Smith@emailaccount.com> wrote in message
news:1cba01c372eb$4edaf7c0$a601280a@phx.gbl...[color=blue]
> 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
>
> ///[/color]


Cor
Guest
 
Posts: n/a
#3: Nov 20 '05

re: ListBox (Multi-Extended Selection Mode)


Hi Steve,[color=blue]
> ///
> Me.ResultLabel.Text = Me.NamesListBox.SelectedItems
> ///[/color]
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


Steven Smith
Guest
 
Posts: n/a
#4: Nov 20 '05

re: ListBox (Multi-Extended Selection Mode)



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
Cor
Guest
 
Posts: n/a
#5: Nov 20 '05

re: ListBox (Multi-Extended Selection Mode)


OK Steve ,
No he was not this is another Option,
Nice that you did reply.
Cor


Fergus Cooney
Guest
 
Posts: n/a
#6: Nov 20 '05

re: ListBox (Multi-Extended Selection Mode)


:-))


Steven SMith
Guest
 
Posts: n/a
#7: Nov 20 '05

re: ListBox (Multi-Extended Selection Mode)


[color=blue]
>-----Original Message-----
>:-))[/color]
[color=blue]
>-----Original Message-----
>:-))[/color]

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
Herfried K. Wagner [MVP]
Guest
 
Posts: n/a
#8: Nov 20 '05

re: ListBox (Multi-Extended Selection Mode)


"Steven Smith" <Steven.Smith@emailaccount.com> schrieb:[color=blue]
> 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[/color]

\\\
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


Steven Smith
Guest
 
Posts: n/a
#9: Nov 20 '05

re: ListBox (Multi-Extended Selection Mode)


[color=blue]
>-----Original Message-----
>"Steven Smith" <Steven.Smith@emailaccount.com> schrieb:[/color]
[color=blue]
>\\\
>Dim o As Object
>Dim sb As System.Text.StringBuilder = New [/color]
System.Text.StringBuilder()[color=blue]
>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 [/color]
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
Steven Smith
Guest
 
Posts: n/a
#10: Nov 20 '05

re: ListBox (Multi-Extended Selection Mode)


[color=blue]
>-----Original Message-----
>Firstly, you are getting the error because you did not[/color]
specify .ToString()[color=blue]
>on the end.[/color]

[color=blue]
>If you want to display a messagebox of all the values[/color]
concatenated. SubClass[color=blue]
>this and override the toString() function and return the[/color]
strings of all the[color=blue]
>items in the collection.[/color]

///
Public Overrides Function toString() As String

Return text

End Function
///

now I see the light..!


Herfried K. Wagner [MVP]
Guest
 
Posts: n/a
#11: Nov 20 '05

re: ListBox (Multi-Extended Selection Mode)


"Steven Smith" <Steven.Smith@emailaccount.com> schrieb:[color=blue]
>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.[/color]

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


Closed Thread