5 Replies - 6158 Views - Last Post: 03 February 2011 - 04:30 PM Rate Topic: -----

#1 Garbag3man117  Icon User is offline

  • D.I.C Head

Reputation: -8
  • View blog
  • Posts: 54
  • Joined: 23-January 11

Tags for listbox items

Posted 03 February 2011 - 03:23 PM

I'm trying to be able to get a tag from a listbox item, but I can't find a listbox class. All I can find is the listviewitem class. Here is my code so far:

Class Form3


dim item as new listviewitem


Private Sub AddItem() Handles Button1.click

item.tag(0) = "red"
item.tag(1) = "wave"
item.text = "favorite colour"
form1.listbox1.items.add(item)

me.close
form1.show

end sub



There is 2 problems with that piece of code.
1. The text of the item in the listbox comes up as "ListViewItem: {favorite colour}". It should just say "favorite colour"

2. I have no way to access the tag. Then I need something like:
msgbox(Form1.listbox1.selecteditem.tag(0))



The only problem is that there is no tag property in listbox1.selecteditem. Does anyone know of a way to solve these two problems?

Is This A Good Question/Topic? 1
  • +

Replies To: Tags for listbox items

#2 _HAWK_  Icon User is offline

  • Master(Of Foo)
  • member icon

Reputation: 1046
  • View blog
  • Posts: 4,072
  • Joined: 02-July 08

Re: Tags for listbox items

Posted 03 February 2011 - 03:47 PM

You make a custom class object:
Public Class myItem
  Public Tag1 As String
  Public Tag2 As String
  Public Text As String
  Protected Overrides Function ToString() As String
    Return Me.Text
  End Function
End Class


Since we override the ToString() method the listbox displays the text field:
Dim item As New myItem
item.Tag1 = "Red"
item.tag2 = "Wave"
item.Text = "favorite colour"
form1.listbox1.items.add(item)


Then to get the value out (selectedIndexChanged event):
Dim item As myItem = CType(listbox1.SelectedItem, myItem)
Debug.Write(item.Tag1)

This post has been edited by hawkvalley1: 03 February 2011 - 04:05 PM

Was This Post Helpful? 1
  • +
  • -

#3 Garbag3man117  Icon User is offline

  • D.I.C Head

Reputation: -8
  • View blog
  • Posts: 54
  • Joined: 23-January 11

Re: Tags for listbox items

Posted 03 February 2011 - 03:54 PM

View Posthawkvalley1, on 03 February 2011 - 05:47 PM, said:

You make a custom class object:
Public Class myItem
  Public Tag1 As String
  Public Tag2 As String
  Public Text As String
  Protected Overrides Function ToString() As String
    Return Me.Text
  End Function
End Class


Since we override the ToString() method the listbox displays the text field:
Dim item As New myItem
item.Tag1 = "Red"
item.tag2 = "Wave"
item.Text = "favorite colour"
form1.listbox1.items.add(item)


Then to get the value out (selectedIndexChanged event):
Dim item As myItem = CType(sender, myItem)
Debug.Write(item.Tag1)

Thanks! Exactly what I needed!
Was This Post Helpful? 1
  • +
  • -

#4 Garbag3man117  Icon User is offline

  • D.I.C Head

Reputation: -8
  • View blog
  • Posts: 54
  • Joined: 23-January 11

Re: Tags for listbox items

Posted 03 February 2011 - 04:04 PM

Oh wait. There was something I didn't notice. When I try to access a tag this is what I get.
First I changed this
Public Class myItem
  Public Tag1 As String
  Public Tag2 As String
  Public Text As String
  Protected Overrides Function ToString() As String
    Return Me.Text
  End Function
End Class


to this:

Public Class TagItem
  Public Tag(1) as string
  Public Text As String
  Public Overrides Function ToString() As String
    Return Me.Text
  End Function
End Class



I put this in the listbox1.selectedindexchanged handeler:

    selecteditem = CType(sender, TagItem)
  

I have a global named "SelectedItem"

which is
(Public SelectedItem as TagItem)


When I try to select an item in the listbox I get:

InvalidCastException was unhandled
Unable to cast object of type 'System.Windows.Forms.ListBox' to type 'RuneScape_AutoTyper.TagItem'.

This post has been edited by Garbag3man117: 03 February 2011 - 04:11 PM

Was This Post Helpful? 1
  • +
  • -

#5 _HAWK_  Icon User is offline

  • Master(Of Foo)
  • member icon

Reputation: 1046
  • View blog
  • Posts: 4,072
  • Joined: 02-July 08

Re: Tags for listbox items

Posted 03 February 2011 - 04:06 PM

Look at the post again, I fixed an error I had with CType.

This post has been edited by hawkvalley1: 03 February 2011 - 04:08 PM

Was This Post Helpful? 0
  • +
  • -

#6 Garbag3man117  Icon User is offline

  • D.I.C Head

Reputation: -8
  • View blog
  • Posts: 54
  • Joined: 23-January 11

Re: Tags for listbox items

Posted 03 February 2011 - 04:30 PM

View Posthawkvalley1, on 03 February 2011 - 06:06 PM, said:

Look at the post again, I fixed an error I had with CType.

Oh wow. I sat here for 15 minutes just waiting for you to respond, then noticed what your last post actually said :sweatdrop:

Thanks! That worked!

This post has been edited by Garbag3man117: 03 February 2011 - 04:31 PM

Was This Post Helpful? 1
  • +
  • -

Page 1 of 1