Computer Hope

Software => Computer programming => Topic started by: Smurfet on April 12, 2010, 10:05:20 AM

Title: ListBox - visual studio 2005
Post by: Smurfet on April 12, 2010, 10:05:20 AM
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 & ")" & "-->"

Title: Re: ListBox - visual studio 2005
Post by: BC_Programmer on April 12, 2010, 11:06:40 AM
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.
Title: Re: ListBox - visual studio 2005
Post by: Smurfet on April 13, 2010, 12:49:22 AM
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?
Title: Re: ListBox - visual studio 2005
Post by: BC_Programmer on April 13, 2010, 01:37:37 AM
if nodedata is the linked list then try:

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

Title: Re: ListBox - visual studio 2005
Post by: Smurfet on April 13, 2010, 10:30:20 AM
Thanks, but still no joy  :-\ I've decided to simplify life and use a Label instead... it's more friendly :P However, i would really appreciate it if someone could help me out with the ListBox problem.
Title: Re: ListBox - visual studio 2005
Post by: Treval on April 13, 2010, 11:33:36 AM
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<listItems.Length-1; 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.
Title: Re: ListBox - visual studio 2005
Post by: BC_Programmer on April 13, 2010, 02:48:51 PM
You know what, we really don't have enough information to work with.


LinkedList is a Generic Class.

What type is <IN> 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
Title: Re: ListBox - visual studio 2005
Post by: Smurfet on April 14, 2010, 05:48:22 AM
Thanx very much 4 the suggestions. I'll work on it and give you feedback on the outcome...
Title: Re: ListBox - visual studio 2005
Post by: Smurfet on April 14, 2010, 06:32:02 AM
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.
Title: Re: ListBox - visual studio 2005
Post by: Treval on April 14, 2010, 07:13:33 AM
Smurfet, please post your full code. =P