Page 1 of 2 12 LastLast
Results 1 to 15 of 18
  1. #1
    Bronze Lounger
    Join Date
    Feb 2001
    Location
    Escondido, California, USA
    Posts
    1,458
    Thanks
    0
    Thanked 1 Time in 1 Post

    List Boxes (VB 6.0 / W98)

    I'm still a beginner with regard to using List boxes. I guess I really don't thoroughly understand several things:

    1. How does one refer to or retrieve an item in a List box? For example, if the box is called lstSpeed, what is the syntax for a command to return a value that was clicked on? And what format is the value that gets returned -- a string or ?

    2. What is the code to set the focus to an item in the list?

    3. I remain a bit confused as to how to refer to an item entered into the List property vs. the ItemData property.

    Finally, is there an on-line tutorial covering these subjects? That would probably set me straight.

  2. #2
    Star Lounger
    Join Date
    Jun 2002
    Posts
    98
    Thanks
    1
    Thanked 0 Times in 0 Posts
    __________________________________________________ ____
    <img src=/S/nun.gif border=0 alt=nun width=20 height=20> Sister Dory
    Our Lady of Perpetual Help, Holstein Falls, Wisconsin, USA

  3. #3
    Bronze Lounger
    Join Date
    Feb 2001
    Location
    Escondido, California, USA
    Posts
    1,458
    Thanks
    0
    Thanked 1 Time in 1 Post

    Re: List Boxes (VB 6.0 / W98)

    Thanks for the tips. However, I still have a real puzzler.

    I'm using:

    Private Sub Form_Load()
    lstSpeed.ListIndex = 0

    to set the focus on the first item in the lstSpeed box. But upon loading the form I get the error message: "Invalid procedure call or argument". When I click on OK the error message dissapears, the focus is on the first item in the list, and everything works ok from there on. I use the

    lstSpeed.ListIndex = Integer.variable

    code elsewhere in the program without any error messages cropping up. Why i get this error upon opening the form I don't understand. If I comment out the instruction the form opens just fine, but the focus isn't on the list box like I want.

    I would welcome any suggestions.

  4. #4
    Gold Lounger
    Join Date
    Feb 2001
    Location
    Dublin, Ireland, Republic of
    Posts
    2,697
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Re: List Boxes (VB 6.0 / W98)

    Chuck,

    When you execute lstSpeed.ListIndex = 0, is the listbox populated with items ?. If it is, that line should not cause an error.

    Do you need to set the ListIndex to 0 on loading the form ?. I would expect it to default to the first item.

    Andrew C

  5. #5
    Star Lounger
    Join Date
    Jun 2002
    Posts
    98
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Re: List Boxes (VB 6.0 / W98)

    To set the focus to the Listbox itself all you need is:

    lstSpeed.SetFocus

    To set the default selected line in the list box:

    If the form is a data entry form with a listbox "bound" to a field, define a default for the field in the table. Then it will automatically appear selected in the listbox when you add a new record. You don't want to wreck the existing record when they display in the form by forcing the listbox value to change.

    If your listbox is NOT displaying database field, just getting user input (not bound to a dataset) then try this:

    List0.SetFocus
    List0.ListIndex = 1

    If I don't setfocus first I get an error message. If you don't want that to be the selected control than add another line to setfocus to another field. This may not be the best way but it's workin' <img src=/S/wink.gif border=0 alt=wink width=15 height=15>
    __________________________________________________ ____
    <img src=/S/nun.gif border=0 alt=nun width=20 height=20> Sister Dory
    Our Lady of Perpetual Help, Holstein Falls, Wisconsin, USA

  6. #6
    Bronze Lounger
    Join Date
    Feb 2001
    Location
    Escondido, California, USA
    Posts
    1,458
    Thanks
    0
    Thanked 1 Time in 1 Post

    Re: List Boxes (VB 6.0 / W98)

    Hi Andrew,

    The list box is populated with several items (it is not referring to items in a data base table). It is used simply to let the user select a number from a short list that I use in a subsequent part of the program. I agree, it should not cause an error, and I actually use the exact same code later on in the program without any such error problems. It only occurs in the Form_Load() section. I actually tried putting it in another sub that gets called from Form_Load(), but got the same error condition.

    I am trying to implement the same function that occurs when the user clicks on the first item in the list, i.e. that item gets highlighted on the display (white text on a colored background), and the list index is set to the first item. I simply want it to perform the very same function upon initially loading the form. As it is, I now have to make the user click on the item before I can execute the rest of the application.

    You are right in that the form defaults to the 0 index anyway upon loading, so I can get by without forcing it to zero. The only difference is that zeroeth item in the list doesn't get highlghted like it does when you click on it.

    I find it quite curious that when I click on OK in the error message dialog box, the program continues normally and seemingly executes the lstSpeed_Click() function. (That is, as if the user had actually clicked on the first item in the list.) I say this because the program executes another action that only appears in the lstSpeed_Click() sub, so it must be getting executed after I clear the error message.

    This is all very puzzling, and I appreciate your help.

  7. #7
    Bronze Lounger
    Join Date
    Feb 2001
    Location
    Escondido, California, USA
    Posts
    1,458
    Thanks
    0
    Thanked 1 Time in 1 Post

    Re: List Boxes (VB 6.0 / W98)

    Hi Dory,

    The code: lstBox.SetFocus results in an "invalid procedure call or argument" message on my computer! Perhaps mine works differently than yours.

    You can see a more detailed description of the problem in my previous reply.

    Thanks anyway.

  8. #8
    Gold Lounger
    Join Date
    Dec 2000
    Location
    New Hampshire, USA
    Posts
    3,386
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: List Boxes (VB 6.0 / W98)

    The most cost effective way to learn about VB 6 is to read a good book such as Gary Cornell's Visual Basic 6 from the Ground Up.

  9. #9
    Bronze Lounger
    Join Date
    Feb 2001
    Location
    Escondido, California, USA
    Posts
    1,458
    Thanks
    0
    Thanked 1 Time in 1 Post

    Re: List Boxes (VB 6.0 / W98)

    Thanks for the suggestion Howard. I started with Visual Basic 6 for Dummies, but I'm probably ready for some meatier stuff now that I'm digging into it. I'll give a look at your suggestion.

  10. #10
    Silver Lounger
    Join Date
    Mar 2001
    Location
    Springfield, Ohio, USA
    Posts
    2,136
    Thanks
    0
    Thanked 1 Time in 1 Post

    Re: List Boxes (VB 6.0 / W98)

    Remember, you cannot do a setfocus in the Load event: there's no form yet. That's what TabIndex is for.

    There must be something that you're not mentioning. lstSpeed.ListIndex = 0 works for me in the Load event, but as Andrew said, the list box must have items in the box before you can set the ListIndex. Can you post the code for your Load event? --Sam
    <font face="Comic Sans MS">Sam Barrett, CACI </font face=comic>
    <small>And the things that you have heard... commit these to faithful men who will be able to teach others also. 2 Timothy 2:2</small>

  11. #11
    Star Lounger
    Join Date
    Jan 2001
    Location
    L.A., California
    Posts
    77
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: List Boxes (VB 6.0 / W98)

    I've found that sometimes objects on my forms are not really "there" yet at form load time. I'm not sure if this applies to ListBoxes, but it sounds like that, cause you can run the same code later.

    I've solved this type of problem in the past by moving all of my Form_Load code into the Form_Activate event. I know... the Form_Activate event fires whenever you click back on the form from another form, or when it was minimized then restored.

    To solve that problem, I define a Boolean variable in the form's Declaration section, say bLoading. In the Form_Load event, I only have: bLoading = True. In the Form_Activate event, I have:

    If bLoading Then
    code
    code
    code
    bLoading = False
    End If

    Try that and see if it helps.

  12. #12
    Gold Lounger
    Join Date
    Dec 2000
    Location
    New Hampshire, USA
    Posts
    3,386
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: List Boxes (VB 6.0 / W98)

    Gary's book is useful both for learning and as a reference, and he writes well.

  13. #13
    Bronze Lounger
    Join Date
    Feb 2001
    Location
    Escondido, California, USA
    Posts
    1,458
    Thanks
    0
    Thanked 1 Time in 1 Post

    Re: List Boxes (VB 6.0 / W98)

    Hi Sammy,

    I did set the tab index to 0 for the list box, and the focus is there (the typical dashed line appears around the first item in the list) upon opening the form. However, the first item isn't highlighted (white text on colored background) like it is when the user actually clicks on the item. This is not a major problem at the moment, as the application works OK this way.

    Here is the initial part of my code:

    Private Sub Form_Load()
    intItem = 0 'Set speed to first item in the List & set focus there.
    lstSpeed.ListIndex = 0 'THIS RESULTS IN ERROR MESSAGE ????
    .....

    The second line in the sub is the one that give me the error condition, and as I mentioned before i use the same code elswhere in the application without any problems.

    I set the items in the list box using the list's properties, and perhaps they are not yet loaded at the form load time. I suppose I could use code to set the items in the list and then it might work. What do you think?

    Thanks for your help.

  14. #14
    Bronze Lounger
    Join Date
    Feb 2001
    Location
    Escondido, California, USA
    Posts
    1,458
    Thanks
    0
    Thanked 1 Time in 1 Post

    Re: List Boxes (VB 6.0 / W98)

    Hi Ron,

    That's a clever work-around. I'll have to give it a try.

  15. #15
    Silver Lounger
    Join Date
    Mar 2001
    Location
    Springfield, Ohio, USA
    Posts
    2,136
    Thanks
    0
    Thanked 1 Time in 1 Post

    Re: List Boxes (VB 6.0 / W98)

    That can't happen, <img src=/S/wink.gif border=0 alt=wink width=15 height=15> so let's try it from scratch. Close down any copies of VB that you have and

    1) start VB with a new standard EXE.
    2) double-click on the list box icon in the toolbox, so that you have a list box in the middle of your new project
    3)click on List in the Properties panel, and click the down arrow to reveal an empty list
    4)Put A, B and C in the list. After you type A press <Ctrl> <Enter> so that you can enter B, then press <Ctrl> <Enter> and then C and finally press Enter.
    5)Press the drop-down arrow again to make sure you still have the list
    6)Double-click the form, so that you have the outline for the form-load event.
    7)Enter one line of code:<pre> List1.ListIndex = 0</pre>

    8)Press the run button on the toolbar. You will see the list box with two elements visible, the first one of which is selected.

    That's what you wanted to do, right?

    OK, that was the easy part, now you need to find out what you did differently with the real thing. Wish that I could look over your shoulder. It's probably something simple like a spelling error. You do have <pre>Option Explicit</pre>

    as the first line of code, don't you? This will save you many posts on the loungue. <img src=/S/grin.gif border=0 alt=grin width=15 height=15> If you, don't, add it and rerun to see if you get a better error message. Also, use the Tools | Options | Editor tab menu to check the "Require Variable Definition." The students that do this usually get an A; the one's that don't, never get an A! Let us know what the problem really was. --Sam
    <font face="Comic Sans MS">Sam Barrett, CACI </font face=comic>
    <small>And the things that you have heard... commit these to faithful men who will be able to teach others also. 2 Timothy 2:2</small>

Page 1 of 2 12 LastLast

Posting Permissions

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