The following items cover topics related to Access ListBox &
ComboBox controls and their
use.
ListBoxColumnResize now supports an
Autosizing method for the ColumnWidths property.
ListBoxRowNumbers lets you add Row
Numbers to your ListBox or Combo control.
NEW Ver 2.0 Justification
- Right or Center Alignment
Functions for data in your List or Combo Controls.
FieldList is a database containing the FieldList
control Access uses in its Table Analyzer and Performance Analyzer Wizards. This
control is created directly from the ACCWIZ.DLL so no ActiveX registration
problems are incurred. I have created a VB like ListBox with 1) CheckBoxes for
each row. 2) True Drag and Drop. 3) Conditional Formatting for each Row. 4)
AddItem method
New Version ListBoxEnhanced - Simulate Callback with
Custom Function to fill ListBox.
ListBoxAuto1 - MouseOver a ListBox - sort of
:-)
CustomListBoxver3 - Use TextBox controls
to simulate ListBox
Raw NewsGroup Postings
Scroll a ListBox to a specific row
Arrow keys to navigate a ListBox without the
Focus
Scroll a ListBox to a specific row. Emulates the VB
ListBox TopIndex property. You can alter the code to easily have the selected
row display as the first or last row as well. The example code is placed behind
a Command Button.
' *** CODE START
Private Sub cmdListIndex_Click()
On Error GoTo Err_cmdListIndex_Click
' Always make NumRows an odd number
' if you want selected Row to be in the
' middle of the ListBox.
' NumRows is the number of completely visible rows in the ListBox Const NumRows
= 7
' Row we want displayed in middle of ListBox.
Dim intDesiredRow As Integer
' Arbitrarily select the 24th row.
intDesiredRow = 24
' ListBox must have the Focus
Me.List2.SetFocus
' Force ListBox to start from the top
Me.List2.ListIndex = 1
' Force the Scroll offset we desire
Me.List2.ListIndex = intDesiredRow + (NumRows / 2)
' Now select the row without further scrolling
Me.List2.ListIndex = intDesiredRow
Exit_cmdListIndex_Click:
Exit Sub
Err_cmdListIndex_Click:
MsgBox Err.Description
Resume Exit_cmdListIndex_Click
End Sub
' ***CODE END
Method #2
Here's the code to force a ListBox to Scroll
to a specific row. I put it behind a Command Button Named Customer, you can
obviously do
whatever you want. Really should be a Class Wrapper for a ListBox to
expose a TopIndex property like VB ListBoxes.
' ***CODE START
'Place this code in the General Declarations of your Form
Private Declare Function SendMessage Lib "user32" Alias "SendMessageA"
_ (ByVal hWnd As Long, ByVal wMsg As Long, _
ByVal wParam As Long, lParam As Any) As Long
Private Declare Function GetFocus Lib "user32" () As Long
' Windows Message Constant
Private Const WM_VSCROLL = &H115
' Scroll Bar Commands
Private Const SB_THUMBPOSITION = 4
' Code end for General Declarations
' Code for Control's Click Event
Private Sub Customer_Click()
Dim hWndSB As Long
Dim lngRet As Long
Dim lngIndex As Long
Dim LngThumb As Long
' You will get lngIndex value from the user or whatever.
' For now I'm just setting it to arbitrary Number
lngIndex = 45
' SetFocus to our listBox so that we can
' get its hWnd
Me.List2.SetFocus
hWndSB = GetFocus
' Set the window's ScrollBar position
LngThumb = MakeDWord(SB_THUMBPOSITION, CInt(LngIndex))
lngRet = SendMessage(hWndSB, WM_VSCROLL, LngThumb, 0&)
End Sub
' Here's the MakeDWord function from the MS KB
Function MakeDWord(loword As Integer, hiword As Integer) As Long MakeDWord = (hiword
* &H10000) Or (loword And &HFFFF&) End Function '***END CODE
HTH
If you'd like to use the Arrow Keys to navigate through a ListBox even if the
ListBox does not have the focus then read on.
Go to Form properties and set the Key Preview to Yes.
Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
Select Case KeyCode
Case vbKeyDown
Me.List2 = Me.List2.ItemData(Me.List2.ListIndex + 1)
KeyCode = 0
Case vbKeyUp
Me.List2 = Me.List2.ItemData(Me.List2.ListIndex - 1)
KeyCode = 0
Case Else
End Select
End Sub
Tthe above code is for a ListBox named List2. Change the name to reflect
your ListBox. Also this is set to work with a ListBox WITHOUT
Column Headers turned on. You'll have to adjust it if you use Column Headers.
Finally the Arrow Keys are sent to oblivion with the Line KeyCode =0.
Your mileage may vary. :-)
|