Results 1 to 7 of 7

Thread: i look stupid but i need help

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Dec 2002
    Posts
    70

    i look stupid but i need help

    hello. i m new 2 this forum and i m new 2 programming. so i have an elementary question(actually much more than one, but just 4 now)

    how do i get a selected value from the listbox?

    eg.
    choice = list1.getselecteditem?

    also, how do i declare variables in general, as in it can be shared between 2 different forms.


    sorry if i sound really stupid, but i have2 start sumwhere. i tried looking 4 tutorials online, but i don't really understand the variables question.

    and online tutorials i can't find the code 2 get selected value from listbox. i don't evn know what most of the properties do 4 the listbox.

    my listbox has say, 5 values
    qwerty
    qwert
    qwer
    qwe
    qw

    how 2 make it when i select qwer, it return value 3.



    thx alot in advance and sorry 4 spamming ur board with useless questions. i expect 2 learn much from u all.

  2. #2
    Frenzied Member John McKernan's Avatar
    Join Date
    Jan 2002
    Location
    SE PA
    Posts
    1,295
    If you want the text of the item selected from a listbox:
    VB Code:
    1. Choice = List1.Text
    ...the index of the item selected
    VB Code:
    1. Choice = List1.ListIndex
    Please note the listbox is 0-based, meaning the first item has an index of 0
    To declare variables for use throughout your program, declare them as public
    VB Code:
    1. Public Choice as String

  3. #3
    Addicted Member
    Join Date
    Sep 2000
    Location
    UK
    Posts
    199
    to get the listbox value use this code:

    Code:
    msgbox me.listbox1.listindex

    For the variable to be shared between 2 forms I would add a module to your project and declare the variable publically ie:

    Code:
    Public MyVariable as String
    Good Luck

  4. #4

    Thread Starter
    Lively Member
    Join Date
    Dec 2002
    Posts
    70
    thx all...

  5. #5

    Thread Starter
    Lively Member
    Join Date
    Dec 2002
    Posts
    70
    what is the itemdata property for?

    how izzit different from index? index is auto numbered?

    so if thta's the case, how do i get teh itemdata instead

    choice = listbox.itemdata?

  6. #6
    Addicted Member
    Join Date
    Sep 2000
    Location
    UK
    Posts
    199
    When you enter a value into the listbox you can also set the itemdata which holds a numeric value. This can be a value of your choice which can be queried once the user has selected your item from the list ie:

    If the list was:

    baked beans
    tomato sauce
    carrots

    the item data could hold the price ie:

    399
    299
    199

    these prices are in pence of course.

    Regards

  7. #7
    Frenzied Member John McKernan's Avatar
    Join Date
    Jan 2002
    Location
    SE PA
    Posts
    1,295
    Index is autonumbered and read-only (0-based)

    ItemData can be set to any number (long) you wish. For example, if you wanted to index your values starting with 1, you could do so in ItemData, simply setting the value when you load the listbox.

    VB Code:
    1. choice = listbox.ItemData(Index)
    2. 'or
    3. listbox.ItemData(Index) = 1
    You must specify the index of the ItemData you want to return or set.

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