XL5: List in Dialog Box Does Not Reflect Changes Made by Macro |
In Microsoft Excel, when you use a Visual Basic macro to clear (deselect)
an item in a list box, if the list box has a selection type of Multi or
Extend, the item may appear to remain selected.
Note that Microsoft Excel DOES recognize that the item is no longer
selected; this problem has to do with the way that the dialog box is
displayed.
If you use a Visual Basic macro to clear an item in a list box while the dialog box that contains that list box is active, the screen may not be properly redrawn (to show that the item is no longer selected).
Microsoft has confirmed this to be a problem in the Microsoft products listed at the beginning of this article. This problem was corrected in Microsoft Excel for Windows 95, version 7.0.
In custom dialog boxes, if a list box has the Selection option set to
Multi, you can choose any number of items from the list. For example, if a
list contains Alpha, Bravo, and Charlie, you can select any, none, or all
of those items.
You can use a Visual Basic macro to select and clear items in a multiple
selection list box by changing the Selected property of a single list item.
For example, if the active dialog box contains a list box (for example,
List1) that contains three items (for example, Alpha, Bravo, Charlie), you
can select the first item by using this line of code:
ActiveDialog.ListBoxes("List1").Selected(1) = True
To clear (deselect) the third item, use this code:
ActiveDialog.ListBoxes("List1").Selected(3) = False A1: AlphaThe dialog sheet contains an OK button that is set to dismiss the dialog box, another button (Button1), and a Multi Selection list box (List1) that is linked to Sheet1!$A$1:$A$4.
A2: Bravo
A3: Charlie
A4: Delta
'------------------------------------------------------------------
Option Explicit
Sub ShowDialog()
'Dimension some variables.
Dim CurList As Variant, LTemp As Variant, LItem As Variant
Dim Counter As Integer
'Show the dialog box.
DialogSheets("Dialog1").Show
'Set an object name for easy referencing of the list box.
Set CurList = DialogSheets("Dialog1").ListBoxes("List1")
'Put the Selected array into the variable LTemp.
LTemp = CurList.Selected
'Initialize the Counter variable.
Counter = 1
'Iterate through the loop once for each item in the array (which is
'the same as iterating once for each item in the list box).
For Each LItem In LTemp
'If the value of the current item is True...
If LItem = True Then
'...show a message box indicating the item is selected.
'CurList.List(Counter) gets us the value of the selected item
'("Alpha", "Bravo", etc.).
MsgBox CurList.List(Counter) & " is selected."
'Otherwise...
Else
'...indicate that it isn't selected.
MsgBox CurList.List(Counter) & " is NOT selected."
End If
'Increment the Counter so we can get the value of the next
'selected item.
Counter = Counter + 1
Next 'repeat until all done
End Sub
Sub ClearItem()
'Clear the second item in the list box.
ActiveDialog.ListBoxes("List1").Selected(2) = False
End Sub
'--------------------------------------------------------------------
To test the subroutine, position the insertion point in the line that
contains Sub ShowDialog(), and either press F5 or choose Start from the Run
menu, and do the following:Additional query words:
Keywords :
Version : 5.00 5.00c
Platform : WINDOWS
Issue type :
Technology :
|
Last Reviewed: April 12, 2000 © 2000 Microsoft Corporation. All rights reserved. Terms of Use. |