?
Solved

set the multiselect property of a listbox at runtime

Posted on 2002-05-29
8
Medium Priority
?
889 Views
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.
0
Comment
Question by:beachbum714
[X]
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
8 Comments
 
LVL 22

Expert Comment

by:rspahitz
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.
0
 
LVL 49

Expert Comment

by:DanRollins
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
0
 

Author Comment

by:beachbum714
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.
0
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

by:rspahitz
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.
0
 

Author Comment

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

Accepted Solution

by:
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.
0
 
LVL 22

Expert Comment

by:rspahitz
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.
0
 

Expert Comment

by:SpideyMod
ID: 7912871
per recommendation

SpideyMod
Community Support Moderator @Experts Exchange
0

Featured Post

Industry Leaders: 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!

Question has a verified solution.

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

Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
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
Course of the Month11 days, 20 hours left to enroll

752 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