Solved

set the multiselect property of a listbox at runtime

Posted on 2002-05-29
8
843 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
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
 
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

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 200 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
DO Loop not working 4 72
VBA: Add rows to listbox based on criteria 7 73
MS Date Picker 64 bit 32 bit issue 12 49
Access Object Property from VBA Module in Excel 2010 2 27
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 …
If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
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…
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…

910 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

22 Experts available now in Live!

Get 1:1 Help Now