Results 1 to 7 of 7

Thread: Cant get Item text

  1. #1

    Thread Starter
    Addicted Member macuiare's Avatar
    Join Date
    Jan 2009
    Posts
    229

    Cant get Item text

    Hey

    Just wondering if anyone knows how I can get an item text from a listbox while in another thread, when I try listbox1.items.item(1) I dont get the usual list but Gethashcode etc

    Thanks in advance

    Reguards

    Macuiare

  2. #2
    PowerPoster cicatrix's Avatar
    Join Date
    Dec 2009
    Location
    Moscow, Russia
    Posts
    3,654

    Re: Cant get Item text

    Post your relevant code.
    To access an object from another thread you should first create a sub or function that changes the properties of your object, then you should check whether your listbox1 requires invokation, then your thread should invoke this sub or function in the UI thread using Me.Invoke

  3. #3
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Cant get Item text

    When you retrieve an item from a ListBox it is returned as an Object reference. That's because a ListBox can contain any type of object. As such, you can only access members of the Object class unless you cast the item as its actual type, e.g.
    vb.net Code:
    1. Dim item As SomeType = DirectCast(myListBox.Items(index), SomeType)
    That said, if what you want is the text displayed in the ListBox then you can get that the same way no matter what the type of the items:
    vb.net Code:
    1. Dim itemText As String = myListBox.GetItemText(myListBox.Items(index))
    If you had read the documentation for the ListBox class then you'd have seen that for yourself. Why does it never occur to anyone to read the documentation? That's what it's for.

    As far as the multi-threading angle goes, follow the CodeBank link in my signature and check out my post on accessing controls from worker threads.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  4. #4
    Addicted Member
    Join Date
    Oct 2008
    Posts
    152

    Re: Cant get Item text

    Quote Originally Posted by jmcilhinney View Post
    Why does it never occur to anyone to read the documentation?
    I enjoy helping others & it helps improve my own proficiency at the same time but I have to agree with you about the above. It must be a pretty large percentage of the time where I see a post and wonder to myself why im looking up the answer for someone else instead of them doing it for themselves.

  5. #5
    PowerPoster cicatrix's Avatar
    Join Date
    Dec 2009
    Location
    Moscow, Russia
    Posts
    3,654

    Re: Cant get Item text

    I have one little excuse for at least some people asking trivial questions - the language barrier.
    An English speaking person can easily find tons of examples and documentation on any given subject, but if someone spends quite a lot of time for just formulating a question in English with a dictionary or an online translation tool this can be forgiven I think. In my country I often encounter this problem. Fortunately, the situation has been improving over the years and more documentation is available in languages other than English.

  6. #6
    Addicted Member
    Join Date
    Oct 2008
    Posts
    152

    Re: Cant get Item text

    If a question & answer can be read in english here then the same can be done in a search or help file.

  7. #7

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width