set the multiselect property of a listbox at runtime

Posted on 2002-05-29
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
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.
What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

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 200 total points
ID: 7856673
>I need to set it at runtime.
This code does exactly what the property 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

Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Restricting text box entry from \/:?<>"| 9 87
Specify ip address in my connection string for sas 2 60
Using "ScreenUpdating" 6 65
Protecting vb6 & .Net code Obfuscation 18 125
The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

809 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