Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 309
Default Listbox questions

If I add like 20 items to my userform's listbox control, how can I
determine if a user selects or double clicks on item 10?? I'm
just trying to figure out which event handles when an item is
selected or double clicked.

Thank you


  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1,565
Default Listbox questions

Assuming your listbox is ListBox1, if you use:

Private Sub ListBox1_Click()
MsgBox "You Selected" & Me.ListBox1.Value
End Sub

The above code would display a message box that tells you what selection you
made, if the ListBox1 is not a multiselect type.

"Robert Crandal" wrote in message
...
If I add like 20 items to my userform's listbox control, how can I
determine if a user selects or double clicks on item 10?? I'm
just trying to figure out which event handles when an item is
selected or double clicked.

Thank you




  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 35,218
Default Listbox questions

I'm not quite sure what you're looking for, but you could use the .value and/or
the .listindex and the .list properties and the _click event--if you only allow
a single selection.

If you allow multiselection, you could use the _change event. (Actually, you
could use the _change event in both single/multi selection cases.

Option Explicit
Private Sub ListBox1_Click()
With Me.ListBox1
MsgBox .Value & vbLf & .ListIndex _
& vbLf & .List(.ListIndex)
End With
End Sub

or
Option Explicit
Private Sub ListBox1_Change()
With Me.ListBox1
If .ListIndex -1 Then
MsgBox .ListIndex _
& vbLf & .Selected(.ListIndex) _
& vbLf & .List(.ListIndex)
End If
End With
End Sub

========
But I'm not sure I'd use either of these events. I find it more usual (as a
user) to allow me to pick and choose the selection(s) I want. I can change my
mind lots of times before I commit my choice.

And I usually have to hit an ok commandbutton to tell the program that I'm done.

With a single selection:
Option Explicit
Private Sub CommandButton1_Click()

With Me.ListBox1
If .ListIndex < 0 Then
MsgBox "nothing chosen"
Else
MsgBox .ListIndex & vbLf & .Value & vbLf & .List(.ListIndex)
End If
End With
End Sub
Private Sub UserForm_Initialize()
Dim iCtr As Long

With Me.ListBox1
.Clear
.MultiSelect = fmMultiSelectSingle
For iCtr = 1 To 20
.AddItem "A" & iCtr
Next iCtr
End With

Me.CommandButton1.Caption = "Ok"
End Sub

With multiselect:

Option Explicit
Private Sub CommandButton1_Click()

Dim iCtr As Long


With Me.ListBox1
For iCtr = 0 To .ListCount - 1
If .Selected(iCtr) Then
MsgBox iCtr & vbLf & .List(iCtr)
End If
Next iCtr
End With
End Sub
Private Sub UserForm_Initialize()
Dim iCtr As Long

With Me.ListBox1
.Clear
.MultiSelect = fmMultiSelectMulti
For iCtr = 1 To 20
.AddItem "A" & iCtr
Next iCtr
End With

Me.CommandButton1.Caption = "Ok"
End Sub

Again, you can actually use the loop with the single selection, but why bother.


Robert Crandal wrote:

If I add like 20 items to my userform's listbox control, how can I
determine if a user selects or double clicks on item 10?? I'm
just trying to figure out which event handles when an item is
selected or double clicked.

Thank you


--

Dave Peterson
  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 309
Default Listbox questions

Thank you thank you..... that was doubly helpful!



"Dave Peterson" wrote in message
...
I'm not quite sure what you're looking for, but you could use the .value
and/or
the .listindex and the .list properties and the _click event--if you only
allow
a single selection.

If you allow multiselection, you could use the _change event. (Actually,
you
could use the _change event in both single/multi selection cases.

Option Explicit
Private Sub ListBox1_Click()
With Me.ListBox1
MsgBox .Value & vbLf & .ListIndex _
& vbLf & .List(.ListIndex)
End With
End Sub

or
Option Explicit
Private Sub ListBox1_Change()
With Me.ListBox1
If .ListIndex -1 Then
MsgBox .ListIndex _
& vbLf & .Selected(.ListIndex) _
& vbLf & .List(.ListIndex)
End If
End With
End Sub

========
But I'm not sure I'd use either of these events. I find it more usual (as
a
user) to allow me to pick and choose the selection(s) I want. I can
change my
mind lots of times before I commit my choice.

And I usually have to hit an ok commandbutton to tell the program that I'm
done.

With a single selection:
Option Explicit
Private Sub CommandButton1_Click()

With Me.ListBox1
If .ListIndex < 0 Then
MsgBox "nothing chosen"
Else
MsgBox .ListIndex & vbLf & .Value & vbLf & .List(.ListIndex)
End If
End With
End Sub
Private Sub UserForm_Initialize()
Dim iCtr As Long

With Me.ListBox1
.Clear
.MultiSelect = fmMultiSelectSingle
For iCtr = 1 To 20
.AddItem "A" & iCtr
Next iCtr
End With

Me.CommandButton1.Caption = "Ok"
End Sub

With multiselect:

Option Explicit
Private Sub CommandButton1_Click()

Dim iCtr As Long


With Me.ListBox1
For iCtr = 0 To .ListCount - 1
If .Selected(iCtr) Then
MsgBox iCtr & vbLf & .List(iCtr)
End If
Next iCtr
End With
End Sub
Private Sub UserForm_Initialize()
Dim iCtr As Long

With Me.ListBox1
.Clear
.MultiSelect = fmMultiSelectMulti
For iCtr = 1 To 20
.AddItem "A" & iCtr
Next iCtr
End With

Me.CommandButton1.Caption = "Ok"
End Sub

Again, you can actually use the loop with the single selection, but why
bother.


Robert Crandal wrote:

If I add like 20 items to my userform's listbox control, how can I
determine if a user selects or double clicks on item 10?? I'm
just trying to figure out which event handles when an item is
selected or double clicked.

Thank you


--

Dave Peterson


Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
View Questions and Answer to questions I created Roibn Taylor Excel Discussion (Misc queries) 4 July 24th 08 12:05 AM
ListBox Questions Josh Sale Excel Programming 3 January 18th 06 10:24 PM
Listbox questions Shatin Excel Programming 2 February 22nd 04 05:16 PM
listbox.value not equal to listbox.list(listbox.listindex,0) ARB Excel Programming 0 October 22nd 03 12:46 AM
Listbox and Array questions Stuart[_5_] Excel Programming 4 September 23rd 03 11:04 PM


All times are GMT +1. The time now is 08:34 AM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"