Looks like the sample is broken. Change this:
Private Sub ListBox1_SelectedValueChanged(ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles ListBox1.SelectedValueChanged
If ListBox1.SelectedIndex <> -1 Then
textBox1.Text = ListBox1.SelectedValue
End If
End Sub
to this:
Private Sub ListBox1_SelectedValueChanged(ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles ListBox1.SelectedValueChanged
If ListBox1.SelectedIndex <> -1 Then
textBox1.Text = ListBox1.SelectedValue.ToString()
End If
End Sub
/claes
"fiaolle" <(E-Mail Removed)> wrote in message
news:XHVqg.7858$(E-Mail Removed)...
> Hi
>
> I Can't get the MSDN's example for Listbox to work.The example is below. I
> get an error in Listbox's sub ListBox1_SelectedValueChanged at row
> textBox1.Text = ListBox1.SelectedValue and it happens after the line
> ListBox1.ValueMember = "ShortName" is run. The error says Cast from type
> 'USState' to type 'String' is not valid.
> I don't know why the code doesn't work, can anyone please explain that for
> me.
>
> Thank's
> Fia
>
>
> Public Class Form1
> Inherits System.Windows.Forms.Form
> Public Class USState
> Private myShortName As String
> Private myLongName As String
> Public Sub New(ByVal strlongName As String, ByVal strShortName As String)
> MyBase.New()
> Me.myShortName = strShortName
> Me.myLongName = strLongName
> End Sub
> Public ReadOnly Property ShortName() As String
> Get
> Return myShortName
> End Get
> End Property
> Public ReadOnly Property LongName() As String
> Get
> Return myLongName
> End Get
> End Property
> Public Overrides Function ToString() As String
> Return Me.ShortName & " - " & Me.LongName
> End Function
> End Class
> Public Class ListBoxSample3
> Inherits Form
> Friend WithEvents ListBox1 As ListBox = New ListBox
> Dim textBox1 As TextBox = New TextBox
> <System.STAThreadAttribute()> _
> Public Shared Sub Main()
> System.Windows.Forms.Application.Run(New ListBoxSample3)
> End Sub
> Public Sub New()
> Me.AutoScaleBaseSize = New Size(5, 13)
> Me.ClientSize = New Size(292, 181)
> Me.Text = "ListBox Sample3"
> ListBox1.Location = New Point(24, 16)
> ListBox1.Name = "ListBox1"
> ListBox1.Size = New Size(232, 130)
>
> textBox1.Location = New Point(24, 160)
> textBox1.Name = "textBox1"
> textBox1.Size = New Size(40, 24)
> Me.Controls.AddRange(New Control() {ListBox1, textBox1})
> ' Populates the list box using DataSource.
> ' DisplayMember is used to display just the long name of each state.
> Dim USStates As New ArrayList
> USStates.Add(New USState("Washington", "WA"))
> USStates.Add(New USState("West Virginia", "WV"))
> USStates.Add(New USState("Wisconsin", "WI"))
> USStates.Add(New USState("Wyoming", "WY"))
> ListBox1.DataSource = USStates
> ListBox1.DisplayMember = "LongName"
> ListBox1.ValueMember = "ShortName"
> End Sub
> Private Sub InitializeComponent()
> End Sub
> Private Sub ListBox1_SelectedValueChanged(ByVal sender As System.Object,
> ByVal e As System.EventArgs) Handles ListBox1.SelectedValueChanged
> If ListBox1.SelectedIndex <> -1 Then
> textBox1.Text = ListBox1.SelectedValue
> End If
> End Sub
> End Class
> End Class
>
>
|