Re: Greying out (disabling) items in a dropdown (or list) box
- From: "Rick Rothstein \(MVP - VB\)" <rickNOSPAMnews@xxxxxxxxxxxxxxxxx>
- Date: Sat, 22 Dec 2007 16:10:35 -0500
Is it possible to disable (i.e. so they cannot be selcted - and are 'greyed out') - items in a dropdown box or a listbox?
From a previous post of mine...
Here is some code that will make a MSFlexGrid act like a ListBox, but where you can disable one or more items in it. Give the code a try and post back with any questions you may have about how any part of it works. Start a new project and add an MSFlexGrid to the form and paste the code below into the form's code window... then run the project. Note: The code is rough and rather quickly constructed... so it may contains small flaws that might surface when you test it out. If so, let me know and I'll see if I can patch it for you. Even if you figure out how to patch it yourself, please post any fixes back to this thread so that the archives are complete. Thanks.
NOTE: No one came back to me with comments on this code, and I never really looked at it again, so my same ending comments above still hold.
Rick
' These 3 declares are needed for functionality
Dim PreviousRow As Long
Dim DisabledColor As Long
Dim SkipEnterCellCheck As Boolean
' The following is for control grid display stuff
Dim RowsToDisplay As Long
Const NumberOfLinesOfText = 30
Const VisibleRows As Long = 12
Private Sub Form_Load()
Dim Index As Long
Const WidthOfGrid As Long = 3000
DisabledColor = RGB(190, 190, 190)
With MSFlexGrid1
.Font = "Arial"
.Font.Size = 10
.Cols = 1
.Rows = NumberOfLinesOfText
.FixedCols = 0
.FixedRows = 0
If NumberOfLinesOfText > VisibleRows Then
RowsToDisplay = VisibleRows
.ScrollBars = flexScrollBarVertical
Else
RowsToDisplay = NumberOfLinesOfText
End If
.Height = RowsToDisplay * (.RowHeight(0) + .GridLineWidth)
.Width = WidthOfGrid
.ColWidth(0) = .Width
.Appearance = flexFlat
.FocusRect = flexFocusNone
.BackColor = vbWhite
.ForeColor = vbBlack
.BackColorSel = vbBlack
.ForeColorSel = vbWhite
.ScrollTrack = True
' Color grid lines if shown
.GridColor = &HC0C0C0
' Hide the grid lines, remove this line to show them
.GridLines = flexGridNone
' Fill the grid with something to start with
For Index = 0 To .Rows - 1
.TextMatrix(Index, 0) = "Text for Line #" & CStr(Index)
Next
' Just to make sure you can see the grid for this example
.Move 120, 120
' Mark all rows as enabled
For Index = 0 To .Rows - 1
.RowData(Index) = 0
Next
' NOW, let us disable some items in the list
DisableRow 0
DisableRow 1
DisableRow 6
DisableRow 7
DisableRow 8
DisableRow 27
DisableRow 28
DisableRow 29
' Attempt to set Row #0 as the current row; if it is
' diabled, the EnterCell event will force it to find
' the first non-disabled row automatically
.Row = 0
' For initialization purposes, we set the PreviousRow
' variable to whatever non-disabled row becomes the
' default
PreviousRow = .Row
End With
End Sub
Private Sub Command1_Click()
Static ItemStatus As Boolean
ItemStatus = Not ItemStatus
DisableRow 6, ItemStatus
MSFlexGrid1.SetFocus
End Sub
Private Sub MSFlexGrid1_Click()
With MSFlexGrid1
Debug.Print "Row #" & .Row & " was clicked"
End With
End Sub
Private Sub MSFlexGrid1_EnterCell()
If SkipEnterCellCheck Then Exit Sub
With MSFlexGrid1
If .RowData(.Row) = True Then FindNextNonDisabledRow
End With
End Sub
Private Sub MSFlexGrid1_LeaveCell()
With MSFlexGrid1
PreviousRow = .Row
End With
End Sub
Private Sub MSFlexGrid1_MouseDown(Button As Integer, _
Shift As Integer, x As Single, y As Single)
With MSFlexGrid1
If .RowData(.Row) = True Then FindNextNonDisabledRow
' Needed to stop contiguous row selections
.Redraw = False
PreviousRow = .Row
End With
End Sub
Private Sub MSFlexGrid1_MouseUp(Button As Integer, _
Shift As Integer, x As Single, y As Single)
With MSFlexGrid1
If .Rows - .TopRow < VisibleRows Then
.TopRow = .Rows - VisibleRows
End If
' Needed to stop contiguous row selections
.RowSel = MSFlexGrid1.Row
.Redraw = True
End With
End Sub
Private Sub MSFlexGrid1_Scroll()
With MSFlexGrid1
If .TopRow > NumberOfLinesOfText - RowsToDisplay Then
.TopRow = NumberOfLinesOfText - RowsToDisplay
End If
End With
End Sub
' Use this Sub to disable an item (DisableItem = True, the default)
' and to reenable an item again (pass DisableItem = False) to the Sub
Sub DisableRow(RowNum As Long, Optional DisableItem As Boolean = True)
Dim CurrentRow As Long
SkipEnterCellCheck = True
With MSFlexGrid1
CurrentRow = .Row
.RowData(RowNum) = True
.Col = 0
.Row = RowNum
If DisableItem Then
.CellForeColor = DisabledColor
Else
.CellForeColor = .ForeColor
.RowData(RowNum) = False
End If
If CurrentRow = RowNum Then
FindNextNonDisabledRow
Else
.Row = CurrentRow
End If
End With
SkipEnterCellCheck = False
End Sub
Sub FindNextNonDisabledRow()
Dim Index As Long
With MSFlexGrid1
If PreviousRow < .Row Then
For Index = .Row + 1 To .Rows - 1
If .RowData(Index) = False Then
.Row = Index
Exit Sub
End If
Next
For Index = .Row - 1 To 0 Step -1
If .RowData(Index) = False Then
.Row = Index
Exit Sub
End If
Next
Else
For Index = .Row - 1 To 0 Step -1
If .RowData(Index) = False Then
.Row = Index
Exit Sub
End If
Next
For Index = .Row + 1 To .Rows - 1
If .RowData(Index) = False Then
.Row = Index
Exit Sub
End If
Next
End If
End With
End Sub
.
- Follow-Ups:
- Re: Greying out (disabling) items in a dropdown (or list) box
- From: Rick Rothstein \(MVP - VB\)
- Re: Greying out (disabling) items in a dropdown (or list) box
- References:
- Greying out (disabling) items in a dropdown (or list) box
- From: Frank Moyles
- Greying out (disabling) items in a dropdown (or list) box
- Prev by Date: Re: Charts and Indicator Windows
- Next by Date: Re: Greying out (disabling) items in a dropdown (or list) box
- Previous by thread: Re: Greying out (disabling) items in a dropdown (or list) box
- Next by thread: Re: Greying out (disabling) items in a dropdown (or list) box
- Index(es):