Solved

set the multiselect property of a listbox at runtime

Posted on 2002-05-29
8
825 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
8 Comments
 
LVL 22

Expert Comment

by:rspahitz
Comment Utility
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
Comment Utility
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
Comment Utility
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
 
LVL 22

Expert Comment

by:rspahitz
Comment Utility
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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 

Author Comment

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

Accepted Solution

by:
rspahitz earned 200 total points
Comment Utility
>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
Comment Utility
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
Comment Utility
per recommendation

SpideyMod
Community Support Moderator @Experts Exchange
0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
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.
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…

762 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now