Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Simple list box question ..... that is bugging the life out of me. 1

Status
Not open for further replies.

technovice

Programmer
Sep 14, 2001
11
0
0
GB
I have a list box with 10 items on it. Each item can only be displayed one at a time to the user who selects their item by using the up/down arrow of the list box. However, if a user selects an item from the list box by using the up/down arrow but doesn't actually click on their selected item before moving to the next field the list box item doesn't show in the text box where I have: text1.text = list1.text

Is there a way round having to click on their chosen item once selected with the up/down arrow of the list box?

Or put another way. When the listbox loses focus what code do I write to mimick the user clicking on their selection from the listbox?

Thanks in advance.
 

Give this a try

[tt]
Option Explicit

Private Sub Form_Load()

List1.AddItem "A"
List1.AddItem "B"
List1.AddItem "C"
List1.AddItem "D"
List1.AddItem "E"

End Sub

Private Sub List1_LostFocus()

MsgBox List1.List(List1.ListIndex)

End Sub


[/tt]

Good Luck
 
vb5prgrmr - Thanks for your help.

Unfortunately this still leaves me with the same problem where the only way to display what is in the listbox in the message box is by actually clicking on the list selection once it has been highlighted with the up/down arrow of the listbox. I need a solution that allows the listbox selection to be chosen simply by the up/down arrow key without having to then click on the chosen item. If you have any more ideas it is very much appreciated.

Regards

Dave
 

Well, what I just showed you will do what you want on the lost focus event, to do it on the keydown or keyup events would do what you want but I have to warn you you may get unexpected behavior with your test using a msgbox. So I have changed it to use a textbox

[tt]

Private Sub List1_KeyUp(KeyCode As Integer, Shift As Integer)

Text1.Text = List1.List(List1.ListIndex)

End Sub


[/tt]

Good Luck
 
Private Sub List1_Click()
Debug.Print List1.ListIndex
End Sub [/b][/i][/u][sub]*******************************************************
General remarks:
If this post contains any suggestions for the use or distribution of code, components or files of any sort, it is still your responsibility to assure that you have the proper license and distribution rights to do so!
 
Nope, I don't think any of these solutions will work for technovice. My uderstanding of his question is that his listbox is only high enough to display one item, and the users scroll using the vertical scrollbar until they see the option they want. As far as they are concerned they have now 'selected' the item they want although they will NOT have actually clicked at all in the listbox itself. In which case the following does the trick:
[tt]
Private Sub List1_Scroll()
Text1.Text = List1.List(List1.TopIndex)
End Sub
[/tt]
or, possibly slightly better, since it means we get an real selection occurring:
[tt]
Private Sub List1_Click()
Text1.Text = List1.List(List1.TopIndex)
End Sub

Private Sub List1_Scroll()
List1.Selected(List1.TopIndex) = True
End Sub
[/tt]
A full example, slightly leveraging vb5prgrmr's initial code, requires a form with a listbox and a textbox on it:
[tt]
Option Explicit

Private Sub Form_Load()

List1.AddItem "A"
List1.AddItem "B"
List1.AddItem "C"
List1.AddItem "D"
List1.AddItem "E"
List1.Selected(List1.TopIndex) = True
List1.Height = Form1.TextHeight("M")
End Sub

Private Sub List1_Click()
Text1.Text = List1.List(List1.TopIndex)
End Sub

Private Sub List1_Scroll()
List1.Selected(List1.TopIndex) = True
End Sub

 
Thankyou everyone for your help. Also congratulations to Strongm who not only managed the almost impossible task of deciphering my question but also offered the correct solution.

Regards

Dave
 

Well, then, since strongm "managed the almost impossible task of deciphering" your question do you think he deserves a star for it????
 
Use the mousedown event to determine if a "click" is as a result of Mouse / Keyboard interaction. Then set a boolean flag at form level which can be inspected from within the list box's click event. That should do it.
 
mmilan,
have you posted your answer in the right thread? It doesn't appear to have anything to do with the question!

technovice,
I've given StringM a star on your behalf. It's quite painless Let me know if this helps
________________________________________________________________
If you want to get the best response to a question, please check out FAQ222-2244 first

'There are 10 kinds of people in the world: those who understand binary, and those who don't.'
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top