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


0
Robert
3/6/2010 1:33:55 AM
excel.programming 6508 articles. 2 followers. Follow

3 Replies
914 Views

Similar Articles

[PageSpeed] 1

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" <nobody@gmail.com> wrote in message 
news:biikn.5694$QL4.3110@newsfe24.iad...
> 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
>
> 


0
JLGWhiz
3/6/2010 2:14:20 AM
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
0
Dave
3/6/2010 2:20:34 AM
Thank you thank you..... that was doubly helpful!



"Dave Peterson" <petersod@verizonXSPAM.net> wrote in message 
news:4B91BBF2.47912305@verizonXSPAM.net...
> 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 

0
Robert
3/6/2010 6:55:31 AM
Reply:

Similar Artilces:

Licensing Questions
I'm building an iSCSI host for a client of mine, and was wondering what the license requirements would be. Here is he question, If I install my iSCSI host software (in this case StarWind) on a windows 2003 Platform, what kind of license do I need, ie do I only need the OS license or do I also need CLIs. Since no actual "User" will be accessing this Server Directly (they might be using a VMDK stored on the host LUN, but that would be via my VMware servers... basiclly, I'm using the iSCSI host, to host disk space for 2 or 3 VMWare ESXi hosts, hosting 2 or 3 VMs which...

GP9 to 10 question
I've run a successful upgrade, but I can't find where to give my users access to the personalized lists, specifically for Field Service Modules. I've given them roles as Svc Manager stake holders etc, kept thier old imported roles... All I get is You don't have security to open this window. But I don't see what 'window' to give them access to. Where do I find that? ...

Filter by form using a multiselect listbox
Hi All, I have been able to build a query that is filtered by a selection in a Listbox, but now i want to use a multi select list box so that two or more items can be included in the query. I constructed the query pointing to the Listbox but all I get is an empty dataset. How can this be accomplished? As always your help is appreciated. Regards, John Dumay Hi John, For a multiselect listbox to work as a filter you need to create another query based on the listbox. Put this code for execution between the selection of the listbox and running your main query ****Start of code***** ...

A hypertext link to an Excel documents (questions)
I have a client who has setup an Excel (Excel 2002) document to have the A column empty. When I link to that document from a webpage and view the page in a browser (IE 6.0.28), some users have the Excel page open up to veiw column A and others (most) have the document open to NOT showing column A, but rather starting at showing column B. What causes this? Is there anything I can do in the HTHL to ensure tha file opens with column A showing? Thank you very much. Hi Robert, What does the link look like in the HTML. Where does the Excel file exist, I guess you are opening it to see th...

Using Outlook with Hotmail
Hello. I've just started using Outlook 2003, after having owned the disc for a while (I only used office for Word prior to this). I use two hotmail accounts, both of which are installed fine. 1) How can I get an audio alert when new hotmail arrives? Surely if Outlook Express provides one, then the more advanced program should too??? This is a mjor thing for me, as I don't want to have to keep checking constantly to see if I have mail. 2) How do I configure my sent items to stay in the right place - e.g. the hotmail sent items with their relevant accounts, rather than in the un...

Basic Graphic Questions
I have a charting program I created some time back. It works well -- but is effectively the old legacy way of doing things. That is: Coordinate system is (0,0) in upper left corner. I Paint everything. -- AutoRedraw = False. If the form is resized, then a new scale is determined (via code) and the graph is repainted. -------------------------------------- I'm wondering if I missing something and that using a more OPP approach might give me better control. Questions 1) Is there a benefit to using the object (picturebox) scale methods instead of calculating your own an...

Questions about ESEUTIL
I have recently inmplemented mailbox limits and forced my users to start cleaning up their mailboxes and archiving mail from my Exchange 2000 server. My 100GB Information Store (that's the total size of the EDB and STM files) has now shrunk to 50GB of actual data and likely contains 50GB of blank pages. I do weekly online defrags that show that each database has lots of free space. I will never again be allowing users to freely use all the mailbox space they wish, so the actual amount of data will stay around 50GB and the free space in the IS will never get realocated. I believe a...

AllocSysString question
I created a COM interface. STDMETHODIMP CMyObj::GetName(BSTR* bstrName) { // TODO: Add your implementation code here CString strName = "John Master"; *bstrName = strName.AllocSysString(); return S_OK; } My question here is: 1. In the method, I did AllocSysString(). Does this cause a memory leak since I didn't free it ? Does COM runtime handle this ? 2. This method could be called from within VB or C++. Does the caller need to free the memory ? Thanks in advance. >1. In the method, I did AllocSysString(). Does this cause a memory leak >since I didn't fre...

Report Printing Question
Hi, I am working on a project for my company and my situation is essentially this, the access database that am I working on retrieves information from our payroll databse and inputs select fields into a two (printed) page length form. It does this for approximately 500 employees. Now this was great because I used to just be able to push a button and it would crank out the two page for every employee and then I could print it. Now however the form is a mandatory 5 pages too long for the standard form on access 2003. I had read some suggestions about creating a seperate form for each...

question on www.howto-outlook (duplicate emails)
Brian I have Outlook 7 Vista premium. I recently added a outlook profile (mine) and am getting the dup emails. In reading the directions on Outlook howto for 2007 version, I have followed the directions by conect to my original pst-file by foing to FILE>OPEN>OUTLOOK DATA FILE and am now looking at an [Accounts folder] & Outlook folder. Now I'm stumped by the direction "move all the newly received emails to that pst file". help? where's the pst file? -- Laura P "Laura Pearson" <LauraPearson@discussions.microsoft.com> wrote in message ...

GP Report Modifier Question (version 9.0)
On our invoice report I changed the format of a custom field to a custom format definition (that I named BB2 with 2 decimal places and show the currency symbol). Now our Inventory History Sales Summary Report prints all of the Total Quantities for the item with a currency symbol also, why is that? Is your Inventory History Sales Summary Report modified? Have you opened the field to check and make sure no format has been assigned to it? ...

Migration and rename question
I have an NT4 domain with Exchange 5.5. I need to upgrade the domain to Windows 2003 and Exchange to 2003 enterprise. Our current domain name is a name our company hasn't used in many years and I would like to change to our current name as well. I've done quite a bit of research and found very detailed instructions on doing each of these tasks individually, but not all together. Does anyone have any advice on the best approach to take in order to accomplish this? Thanks for any help. You will need to create a new Forest and do an Inter-org migration. This is much more compl...

MS Money 2203 D&B
I am creating a new budget in Money 2003 Deluxe & Business. I am in the "Enter Your Expenses" section. I want to add a category to a group, so I select "Add" then "Add a category to a budget group". Then I can select from a list of the categories. HOWEVER, the list only includes personal use categories. All the categories that are business related do not show up. How do I select a business category ???? How do I make a budget of business expsenses ???? ...

Quick VBA Worksheet Change Event or Selection Question:
Quick VBA Worksheet Change Event or Selection Question: I have a workbook that has a sheet named "Formatted Prices". I enter data starting from row 7 & from column A to S all the way down on a daily basis. This range is not completely filled on a daily basis=85, somedays I might just enter 2 or 3 data only filling 3 rows, but always from column A to S. In column Q, I have a formula that depends on column P=85, it basically a simple calculation formula that says if the data in say P7 is empty, then show nothing, but if the data in P7 is something, then subtract t...

question on "include" in schema files (.xsd). Trouble validating schema in .NET
Hi, I hope someone can please help me. I'm having a lot of trouble with schema files in .NET. I have produced a very simple example that uses "include" to include other schema files. It all works with SPY, but when I pick "Validate Schema" from the .NET 2003 menu, it fails with message: "Type XType is not declared." As I hinted at in my subject line, I suspect that it has to do with including another schema multiple times. Can someone tell me how this should be done? Possibly, SPY or .NET do not conform to the standard. Is there a reference somewhere t...

Query parent elements (newbe question)
Hi, I have a XML file where I want to query elements but id of the element is buried deep inside child elements for example: <XmlRoot> <XmlPosition> <PositionRecordInfo> <PositionId>1</PositionId> <PositionStatus>Active</PositionStatus> </PositionRecordInfo> <PositionTitle>.NET Developer</PositionTitle> <PositionDescription>Some sample text here</PositionDescription> </XmlPosition> <XmlPosition> <PositionRecordInfo> ...

Loaded Question
We are using Retail Management System (RMS), ACT 2000, Peachtree Premium, and now CRM. 1. How do we tie the CRM into the RMS product database? 2. We imported the customer data from Outlook and Act 2000. The app said it completed successfully but I can't find any of the records? ...

Question on Outlook Sychronization
All, I've been asked to write an application that will allow our remote users using Outlook XP to sychronize with our Exchange 2000 server. The idea is that the app will dial in (using DUN), sync and then disconnect. They wanted the absolute minimum involvement from the user. They just want the user to double-click on an application then walk away. Now....I'm just now in the researching stage, but from what I've gathered, it looks like Outlook itself can be set up to do this, right? From what I've seen, it looks like it might be possible, but I'm not at a place where I c...

Some questions re VBA in Excel
Hi, I'm relatively new to using VBA in Excel and hope that someone can help me with the following: 1. I have written some scripts that work in one workbook but open another for reading data. In order to make sure that this file is never corrupted I close the file at the end of my scripts and make sure that any changes are discarded: Workbooks(dsName).Close SaveChanges:=False Unfortunately I still get the message concerning a large amount of stuff being in the clipboard and do I want to save it, Yes/No/Cancel? I want to protect the user from this question (not least because the Canc...

Installation Office 2007 more info RE: my question.
I have Windows 7 64-bit. -- Barbara ...

Query-based Distribution Groups Attribute Question
A student asks: When a new attribute is added to a user class object in the schema, does that attribute automatically appear in the choices one has when configuring a query in the definition of a Query-based Distribution Group? If not, how would one get it to appear? Many thanks in advance! JB Fields jbfields@msn.com ...

ListBox
On my form I have a combobox with a value list. After I make my selection I would like to run a query (after_update) and have it populate a list box? Is this possible and how? The query is simple and will only select one field from my table with about 20 records. Then is it possible to select certian values in this list box (may CTRL Click the ones I want) and then have a report print out based on what I selected in the list box with other selection on my form? Thanks, Sean, In the combo's AfterUpdate event, you need to add code to set the listbox's RowSource property. ...

Contact list question
I now have my contacts listed online with a Windows Live ID. But, I notice the display in WLM is different ... depending on if I am logged into Windows Live ID or not. When I am not logged in, I see all my Groups (or Categories) but when I am logged in, I see none. The total contacts is the same. Is there any way to duplicate the Group/Categories when online? Tom You are looking at two independent contact lists. Any changes made while logged in will not be reflected when not logged in and vice versa. You would have to manually recreate the groups while logged in. ...

KB288807 Question
In the KB it says " A server short name that exists in more than one domain, for example, server.root.com and server.child.root.com. This incorrect configuration causes communication security problems." Does "server" refer to the Exchange server, the DNS server or the DC? I have the symptom that trying to browse on the Recipient Update Service (child) on the domain controller selection property results in a "the specified domain does not exist or could not be contacted" error. This was OK before SP2. Thanks for any light on this. -- Ray H. Gay Exchange ...

CCR disk requirements questions
Well this is throwing me off guys. We are planning a CCR environment. We have an EMC scan at our HQ that we plan to connect our primary mailbox server to. Our DR mailbox server is our concern. I was under the impression that the hardware didnt need to be "the same", but beyond that I was under the impression that they didnt even need to be "all that close" on performance. However, reading this doc: http://technet.microsoft.com/en-us/library/c5a9c0ed-e43e-4bc7-99fe-7d1a9cb967f8.aspx makes it seem like not only does the "passive node" (the server in the DR...