473,396 Members | 1,714 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Can't get MSDN's example for Listbox to work

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
Jul 5 '06 #1
1 3211
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" <fi*****@telia.comwrote in message
news:XH*****************@newsb.telia.net...
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


Jul 6 '06 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Russell Campbell | last post by:
Posting again, since my first attempt never appeared: In the onClick method of a listbox, I am attempting to retrieve the selectedIndex property. The listbox is set up to allow multiple...
4
by: f_salazar | last post by:
English Version !! Hi !! I have a page with a Form, inside the form I have listbox with 'X' elements. Wend I submit the page, an ASP process inside the page have to read those elements and...
4
by: J | last post by:
I think Microsoft purposely makes it difficult to learn their products. Two examples of this are evident in the following MS example....
4
by: Mantova | last post by:
Maybe it's simple, but I can't get it to work. I'm originally a webdeveloper, and just started to work on a C# windows application. I want a list, something similar to a <select><option> object,...
10
by: Dica | last post by:
i've used threads in a couple of c# desktop apps with no problems before, but can't seem to get this code to update my listBox: Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As...
3
by: Uma sakshi | last post by:
Hi I have one VB.NET application,in that application i have one datagrid control.The datagrid control contains somedata.I want to copy the data in a particular cell and paste it into my C#.NET...
2
by: aName | last post by:
How can this work ? http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfsystemwindowsformsdragactionclasstopic.asp In Vbasic( see code below) e is pass ByVal so a...
9
by: Matthew Wells | last post by:
OK, I've narrowed down the problem. This works when in the aspx page <script type="text/javascript" > function btnFirst_Click() { alert("Hello");...
22
by: raylopez99 | last post by:
Here is a classic example of why C# is like beating your head against the wall. I can tell you where the error is in the below code--it's the "NumberStyles" property of DecimalParse (which...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.