[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now


set the multiselect property of a listbox at runtime

Posted on 2002-05-29
Medium Priority
Last Modified: 2012-05-04
Is there a way to set the multiselect property of a listbox at runtime?  Can it be done thru the WIN32 API?  I have a listbox class to build it on the fly.  VB6 only allows it to be changed at designtime.  I want to be able to set it on the fly as it loads.
Question by:beachbum714
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
LVL 22

Expert Comment

ID: 7043251
Although you can't change this at run time, you can set Multiselect=2-Extended at design time.

Then at run-time, use a module-level property to track whether it allows single or multiple selections and use this code:

Private Sub List1_Click()
  If List1.SelCount > 1 And MyMultiSelectProperty=0 Then
    List1.Selected(List1.ListIndex) = False
  End If
End Sub

This will deselect whatever the user just selected if another item is already selected.
LVL 49

Expert Comment

ID: 7851281
Hi beachbum714,
It appears that you have forgotten this question. I will ask Community Support to close it unless you finalize it within 7 days. I will ask a Community Support Moderator to:

    Accept rspahitz's comment(s) as an answer.

beachbum714, if you think your question was not answered at all or if you need help, just post a new comment here; Community Support will help you.  DO NOT accept this comment as an answer.
DanRollins -- EE database cleanup volunteer

Author Comment

ID: 7853151
How then do I close or delete a question?  I no longer see a means of doing so.  But, by all means close it.  THe only response is not accepted.
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

LVL 22

Expert Comment

ID: 7855195
Did you try my code?  It will do ask you requested by preventing users from multiselecting.  If you want to be able to toggle this, simply add a module-level flag and check the flag to see if you should run the code or not.

Author Comment

ID: 7855605
I need to set it at runtime.  That does not solve my problem.
LVL 22

Accepted Solution

rspahitz earned 800 total points
ID: 7856673
>I need to set it at runtime.
This code does exactly what the property does...at run-time.

Try this as a test:

Add a checkbox next to the listbox.  This represented the multi-select property for test purposes.

Add some items to the listbox:

Private Sub Form_Load()
  Dim i As Integer
  For i = 1 To 10
    List1.AddItem i & "is selected"
  Next i
End Sub

Add this code to allow/disallow new items when you toggle the "multiselect property" (controlled by the checkbox or your own boolean module-level value.)

Private Sub List1_Click()
  Dim bMyMultiSelectProperty As Boolean
  bMyMultiSelectProperty = (Check1.Value = vbChecked)
  If List1.SelCount > 1 And bMyMultiSelectProperty = False Then
    List1.Selected(List1.Tag) = False
  End If
  List1.Tag = List1.ListIndex
End Sub

When the checkbox is selected, you can multiselect.  When the checkbox is unselected, you can't multi-select.

The only other issue (which is probably why you'll never see the multiselect property available at run-time) is what to do if you have multiple items selected, then turn off multiselect...?  For my test, I left them selected, but maybe it's more appropriate to clear all selections.
LVL 22

Expert Comment

ID: 7856687
Finally, if you're looking for the list control to allow dynamic multiselection, it probably never will...but you can always create your own user control that does exactly the code I have above, after which you add a property to your control to toggle the flag.

Problem solved.

Expert Comment

ID: 7912871
per recommendation

Community Support Moderator @Experts Exchange

Featured Post


Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…
Suggested Courses

650 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question