1.

Solve : ListBox - visual studio 2005?

Answer»

Hi, I created a project with multiple forms using visual studio 2005. 1 of the forms has a listbox that is supposed to display data from a linked list. The linked list is working fine cos i tried to display data using MessageBox.Show() and it was ok. The problem is when i try to display it into the listbox, i get an exception error. How can i get it to display the items from the linked list?

My listbox is in frmTraversal and it is called lstItems.

The CODE in the linked list is as follows:

frmTraversal.lstItems.DataSource = nodedata.Name & _
" (" & nodedata.Age & ", " & _
nodedata.Gender & ")" & "-->"

you are SETTING to datasource to a string object. Datasource must be an object that implements the IList or IListSource interfaces- such as the linked list itself.ok, so which property should i use? I can't use Items cos it's a read-only property. Is there a better way of getting the items to be displayed into the listbox?if nodedata is the linked list then try:

Code: [Select]frmTraversal.lstItems.DataSource=nodedata

Thanks, but still no joy I've decided to simplify life and use a Label instead... it's more friendly However, i would really appreciate it if someone could help me out with the ListBox problem.Alright at the moment I'm very tired so I'll just take a wild guess on how to get your data to display correctly:

frmTraversal.lstItems.DataSource = nodedata;

for (int i=0; i{
frmTraversal.lstItems.Text = nodedata.Name & _
" (" & nodedata.Age & ", " & _
nodedata.Gender & ")" & "-->"
}

I might be completely wrong but hey let me KNOW. =P
The point is, you assign a DataSource object (which contains the data of LinkedList) to fill in the ListBox,
you then traverse each item in this DataSource and assign every Name+Age+Gender
as a String to put into the ListBox' items as the .Text value of each node.

I see you are working with VB .NET so please translate my code to your own likings.

Hope it helped.
You know what, we really don't have enough information to work with.


LinkedList is a Generic Class.

What type is the linkedlist? that is, how is it declared?



Anyway, I made the following small test:

A single Listbox, a single button, with the default names:

Code: [Select]Public Class Form1
Private Class CNodeClass
Public Sub New(ByVal Name As String, ByVal Gender As String, ByVal Age As Long)
End Sub
Public Sub New()
Name = "Name" + Trim$(Rnd() * 800000)
Gender = IIf(Rnd() > 0.5, "F", "M")
Age = 10 + Int(Rnd() * 40)
End Sub
Public Name As String
Public Gender As String
Public Age As Long
End Class
Private mLinkedList As LinkedList(Of CNodeClass)
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
mLinkedList = New LinkedList(Of CNodeClass)
For I As Integer = 1 To 40
mLinkedList.AddLast(New CNodeClass())
Next
Me.ListBox1.Items.Clear()
Me.ListBox1.DataSource = mLinkedList
For Each litem As CNodeClass In mLinkedList
Me.ListBox1.Items.Add(litem.Name + "," + CSTR(litem.Age) + "," + litem.Gender)


Next


End Sub
End Class
Thanx very much 4 the suggestions. I'll work on it and give you feedback on the outcome...I tried it out but it didn't work 4 me Nothing was displayed and an ArgumentException was thrown. Also, there was a message SAYING..Complex DataBinding accepts as a data source either an IList or an IListSource. I've decided to let it go for now, cos i used a textbox and it displayed my data just fine. Smurfet, please post your full code. =P



Discussion

No Comment Found