Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

MSFlexGrid, on KeyPress go to first row that starts with that letter?

Status
Not open for further replies.

JohnInDC

Programmer
Joined
Aug 26, 2005
Messages
20
Location
US
I would like to know the best way to go to the first row that starts with a certain letter when the corresponding letter is pressed on the keyboard.

I have a MSFlexGrid with many rows, in alphabetical order, and I would like to give the user a "short cut" instead of having to scroll down to find a row.

What is the best way to do this in VB 6.0?

Obviusly, the KeyPress event handler would have code that gets the key value and sets the .row property of the grid, but how do I find out which row to go to? I want it to go to the first row that has a value that starts with that letter.

Would I have to loop through the grid values, and examine the first letter of each row until I find a match? That seems like an expensive way to do it. Is there a more direct way?

Also, when the .row property is set will the display automatically scroll down the that row, or do I need to do something else?

Thanks,
John
 
Maybe using a collection???

For example, as you're looping through the recordset to populate the grid, use a counter to keep track of where each letter starts and store the letter and the value in the collection. Then when the user presses "M", use the collection to get the value stored in "M".

You can then use that to get to the correct row.

Does that make sense?? (Not sure it does, even to me!!)

Patrick
 
how do I find out which row to go to

if the flexgrid is bound to a recordset (rs, e.g.), use the absoluteposition property:

rs.absoluteposition = flexGrid.row

That kind of thing.

HTH

ciao for niao!

AMACycle

American Motorcyclist Association
 
Here's how I did it:

If NameFound(txtEntry.Text) then
' do something
else
' don't
end if

private Function NameFound(thisName as String) as Boolean
Dim r as integer ' row index

for r = 1 to flxGrid.Rows - 1
' assumes that column 3 is the column you're concerned about.
if left(flxGrid.TextMatrix(r, 3), 1) = thisName then
NameFound = True
rs.absoluteposition = (flxGrid.row - 1)
exit Function
end if
next r
NameFound = False
end Function



ciao for niao!

AMACycle

American Motorcyclist Association
 
You could also index (rs.fields("MyField").Properties("Optimize") = true) the recordset if you need speed and use Find or Filter, if the flexgrid is bound to the recordset. Basically, you can run through the rows of the flexgrid, as AMA does, and synchronize the recordset cursor with the row that you find, or you can run through the recordset and let the binder synchronize the flexgrid with the rows that you find. (Not that you have to use data binding, either.)

I can't say if AMA's solution is expensive or not. AMA, what's your experience?

Bob
 
Thanks guys!

I ended up with something about the same as AMA's solution...

Private Sub Form_KeyPress(intKeyAscii As Integer)
Dim intRowPointer As Integer, boolFound As Boolean
Dim intFirstLetterAscii As Integer, strFirstLetter As String
'if the backspace key is pressed
If intKeyAscii = vbKeyBack Then
'go back to the main menu
CmdMainMenu_Click
ElseIf (LCase(intKeyAscii) > 96) And (LCase(intKeyAscii) < 123) Then
'if the key pressed is an alphabet key...
'...then find the first row that starts with the letter of the key pressed
flxChartGrid.SetFocus
'loop through the rows
intRowPointer = 1
Do While (Not boolFound) And (intRowPointer < flxChartGrid.Rows)
'get the first letter of the current row's text
strFirstLetter = Left(flxChartGrid.TextMatrix(intRowPointer, 0), 1)
'get the ascii value of the lowercase of that letter
intFirstLetterAscii = Asc(LCase(strFirstLetter))
'if that value equals the ascii value of the key pressed
If intFirstLetterAscii = intKeyAscii Then
'go to that row
flxChartGrid.TopRow = intRowPointer
flxChartGrid.Row = intRowPointer
'end search
boolFound = True
End If
intRowPointer = intRowPointer + 1
Loop
End If
End Sub


I know I can combine a couple of those statements, but I decided not to for maintainability.

I was hoping there'd be some property or method... You never know. Microsoft gives you so many things you never need, then makes you write some otherwise common things yourself...

I do like redsmooth's solution. I might try that and see if there's a noticeable difference.

Thanks again.

JT
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top