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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Navigating MSHFlexgrid

Status
Not open for further replies.

vbjock

Programmer
Jul 13, 2007
16
US
I am trying to use MSHFlexgrid with textbox to update 1 column. There are 4 columns, namely, member ID, member Name, Interest, Date Joined. This also serves as the row heading. When I start the form, I want the cursor to be on the Date joined, 2nd row. Additionally, how can I prevent the user from going to row 1? Lastly, is it possible to use Control tab to move the cursor left or when it is on the first column to the above row/4th column. Thanks.
 
Private Sub Form_Load()
With MSFlexGrid1
.Cols = 4
.Rows = 10
.FixedRows = 1
.FixedCols = 0
.Col = 3
.Row = 1
End With
End Sub

Private Sub MSFlexGrid1_KeyDown(KeyCode As Integer, Shift As Integer)
If KeyCode = 9 Then
If (Shift And vbCtrlMask) > 0 Then
With MSFlexGrid1
If .Col = 0 Then
If .Row > 1 Then
.Row = .Row - 1
.Col = 3
End If
Else
.Col = .Col - 1
End If
End With
End If
End If
End Sub
 
Hugh, many thanks! It is working quite well. Right now, we use the left, right, up and down arrows to navigate to the grid. My error in the 2nd request. What changes are needed to use Tab and shift tab to move right and left also, respectively, instead of Control tab to move left or navigate 1 row up to column 3?
Regards
 
Put your cursor on the line 'If KeyCode = 9 Then' and press F9; it will be highlighted.
Run the code, put focus on the grid, and press one of the keys your want the KeyCode for; the run will halt on the highlighted line.
When halted pass the mouse cursor over 'KeyCode'; a ToolTip will be displayed revealing the required test value.

9 is the test value for the Tab key

F1 is your friend.
 
Hi HughLerwill,
Thanks for your help and suggestions. They were great!

This is the way I was able to get it done for Tab and Shift Tab to work. Since my form contains other command buttons like, Cancel, Exit, Save, I need to make sure these Tab stop properties are set to False, else, Tab control is hard to monitor whether it is for the Grid or for the form.

‘Declare a switch to monitor the Shift key
Dim svKey As Boolean

Private Sub Form_Load()
svKey = False
End sub

Private Sub fg2_KeyDown(KeyCode As Integer, Shift As Integer)
' code to allow Shift Tab or Tab to navigate left or 1 row up and column 3 or just the opposite

‘ User pressed Shift key, then keep track of it
If KeyCode = 16 Then
svKey = True
Exit Sub
End If

‘ Followed by a tab, then user wants to move the cursor left or to above row if applicable
If KeyCode = 9 And svKey Then
svKey = False
KeyCode = 0
With FG2
If .Col = 0 Then
If .Row > 1 Then
.Row = .Row - 1
.Col = 3
End If
Else
.Col = .Col - 1
End If
FG2.ColSel = .Col ‘ Highlight (not sure if needed)
End With
Exit Sub
End If

‘ User just pressed Tab, then move cursor right, etc.
If KeyCode = 9 And Not svKey Then
With FG2
If .Col < 3 Then
.Col = .Col + 1
Else
If .Row < .Rows Then
.Row = .Row + 1
.Col = 0
End If
End If

End With
End If

End Sub
 
Oops! when looking at the grid, rows count includes the header. So if rows = 6, then data is only for 5 rows, thus:

If KeyCode = 9 And Not svKey Then
With FG2
If .Col < 3 Then
.Col = .Col + 1
Else
If .Row < .Rows - 1 Then ' exclude header
.Row = .Row + 1
.Col = 0
End If
End If

End With
End If
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top