9 Replies - 482 Views - Last Post: 07 May 2012 - 01:30 PM Rate Topic: -----

#1 huzi8t9  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 23
  • View blog
  • Posts: 326
  • Joined: 11-July 07

ListBox Control proving difficult.

Posted 07 May 2012 - 11:19 AM

Hello, D.I.C heads.

I'm writing an application; I have an Advanced Tree control and a ListBox control. When I populate the ListBox programmatically, using a function that returns a ListBoxItem, it adds the field fine, but displaying incorrectly; it doesn't just add say "My item," it adds "ListViewItem: {My item}"

When I add the item using just a string, it adds fine but I need to use the return function to set the tag data and such, but when I use that function, it adds the above which is really annoying.

Is there something I'm doing wrong, or..?

Thanks, any help appreciated.

~huzi

Is This A Good Question/Topic? 0
  • +

Replies To: ListBox Control proving difficult.

#2 modi123_1  Icon User is online

  • Suitor #2
  • member icon



Reputation: 9096
  • View blog
  • Posts: 34,161
  • Joined: 12-June 08

Re: ListBox Control proving difficult.

Posted 07 May 2012 - 11:25 AM

Moving help question out of 'advanced topics'...
Was This Post Helpful? 0
  • +
  • -

#3 huzi8t9  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 23
  • View blog
  • Posts: 326
  • Joined: 11-July 07

Re: ListBox Control proving difficult.

Posted 07 May 2012 - 11:43 AM

View Postmodi123_1, on 07 May 2012 - 07:25 PM, said:

Moving help question out of 'advanced topics'...


Sorry

~huzi
Was This Post Helpful? 0
  • +
  • -

#4 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2257
  • View blog
  • Posts: 9,445
  • Joined: 29-May 08

Re: ListBox Control proving difficult.

Posted 07 May 2012 - 12:05 PM

Show us the code you are using, as we ain't sitting next to you at your computer.
Was This Post Helpful? 0
  • +
  • -

#5 woodjom  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 29
  • View blog
  • Posts: 549
  • Joined: 08-May 08

Re: ListBox Control proving difficult.

Posted 07 May 2012 - 12:09 PM

Without knowing how your adding the ListItems, we wont be able to instruct you on the correct or logical approach for this.

Although it seems like you have not set the Key and Value components of the ListBox from what it sounds like.
Was This Post Helpful? 0
  • +
  • -

#6 huzi8t9  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 23
  • View blog
  • Posts: 326
  • Joined: 11-July 07

Re: ListBox Control proving difficult.

Posted 07 May 2012 - 12:24 PM

View PostAdamSpeight2008, on 07 May 2012 - 08:05 PM, said:

Show us the code you are using, as we ain't sitting next to you at your computer.


Here's my function used to return my ListBoxItem:

    Private Function GetItem(tag As FATXFileEntry) As ListViewItem
        Dim x As New ListViewItem()
        With x
            .Name = tag.Name
            .Text = tag.Name
            .Tag = tag
        End With
        Return x
    End Function



Furthermore, here's how I'm implementing my function:

        lbFiles.Items.Clear()
        For Each y As MY_OBJECT In x.Files
            status = "Processing files..."
            Dim Item As ListViewItem = GetItem(y)
            lbFiles.Items.Add(Item)

            Application.DoEvents()
        Next



Thanks for your replies and willingness to help, it's appreciated.

~huzi
Was This Post Helpful? 0
  • +
  • -

#7 Nerfpl  Icon User is offline

  • D.I.C Head

Reputation: 43
  • View blog
  • Posts: 165
  • Joined: 15-January 12

Re: ListBox Control proving difficult.

Posted 07 May 2012 - 12:29 PM

Try overriding ToString() for objects you add. Make it return whatever you want to see as name.

No code needed for this :P

This post has been edited by Nerfpl: 07 May 2012 - 12:57 PM

Was This Post Helpful? 0
  • +
  • -

#8 huzi8t9  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 23
  • View blog
  • Posts: 326
  • Joined: 11-July 07

Re: ListBox Control proving difficult.

Posted 07 May 2012 - 12:31 PM

View PostNerfpl, on 07 May 2012 - 08:29 PM, said:

Try overloading ToString() for objects you add. Make it return whatever you want to see as name.

No code needed for this :P


If I do that, then what of my object's properties..?

~huzi
Was This Post Helpful? 0
  • +
  • -

#9 Nerfpl  Icon User is offline

  • D.I.C Head

Reputation: 43
  • View blog
  • Posts: 165
  • Joined: 15-January 12

Re: ListBox Control proving difficult.

Posted 07 May 2012 - 12:47 PM

I posted this at same time as you posted the code. Are you using ListBox or ListView?
ListBox takes object,
ListViewItem is for ListView.

ListBox will run ToString() on object to display its name.
ListView have ListViewItems with name etc, and optional tag for object link etc

/Btw typo. I meant 'overriding' instead of 'overloading'; for object meant for class of your object

example of what i meant
listbox ' some ListBox

Public Class ClassX
	Public Name As String = "my name"
End Class

listbox.Items.Add(new ClassX()) ' will display "ClassX" because that's the Object.ToString();

' But 

Public Class ClassX
	Public Name As String = "my name"

	Public Overrides Function ToString() As String
		Return Me.Name
	End Function
End Class

listbox.Items.Add(new ClassX()) ' will now display "my name", and it will also update once name changes and list refreshes.


This post has been edited by Nerfpl: 07 May 2012 - 01:41 PM

Was This Post Helpful? 1
  • +
  • -

#10 huzi8t9  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 23
  • View blog
  • Posts: 326
  • Joined: 11-July 07

Re: ListBox Control proving difficult.

Posted 07 May 2012 - 01:30 PM

View PostNerfpl, on 07 May 2012 - 08:47 PM, said:

I posted this at same time as you posted the code. Are you using ListBox or ListView?
ListBox takes object,
ListViewItem is for ListView.

ListBox will run ToString() on object to display its name.
ListView have ListViewItems with name etc, and optional tag for object link etc

/Btw typo. I meant 'overriding' instead of 'overloading'; for object meant for class of your object


Thank you! I was using a ListBox when I should have been using a ListView.

Thanks again, greatly appreciated.

~huzi
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1