|
-
Sep 5th, 2001, 01:28 AM
#1
Thread Starter
New Member
Flex Grid
I am using the MS flex grid for some data entry and I need to know if it is at all possible to use the tab key to move across coloumns??
Currently the data is being entered into an invisible text box and the tabbing will work on the keydown event of the grid but will not regestier in the text box does anyone know why?
-
Sep 5th, 2001, 01:38 AM
#2
-= B u g S l a y e r =-
Could you show us some of the code?
-
Sep 5th, 2001, 04:35 AM
#3
I'm not sure if I understand you. How can anyone enter any data in an invisible TextBox?
You must mean that the textbox gets visible when you want to enter data in the grid. You probably move the textbox and size it to the active cell size in the grid. I'm I right so far?
The textbox will get a KeyDown event if the textbox has the focus and you press the tab key.
VB Code:
Private Sub Text1_KeyDown(KeyCode As Integer, Shift As Integer)
If KeyCode = vbKeyTab Then
MsgBox "Tab was pressed", vbInformation
End If
End Sub
Best regards
-
Sep 9th, 2001, 09:11 PM
#4
Thread Starter
New Member
Yes you are correct with the textbox entry.
And the code i have been trying is similar to what you supplied however i have found that the tab key would not fire the keydown or keypress event for the textbox, it will work for the corresponding events on the grid...
This is some code i have been using do you have any ideas ?
Code:
Private Sub grdItems_KeyDown(KeyCode As Integer, Shift As Integer)
'MsgBox "grdItems_keydown"
Select Case KeyCode
Case vbKeyDelete
grdItems = ""
Case vbKeyTab
If grdItems.Col < grdItems.Cols - 2 Then
grdItems.Col = grdItems.Col + 1
ElseIf grdItems.Col = grdItems.Cols - 2 Then
If grdItems.Row = grdItems.Rows - 1 Then
grdItems.Row = 1
grdItems.Col = grdItems.Col - 1
Else
grdItems.Row = grdItems.Row + 1
grdItems.Col = grdItems.Col - 1
End If
End If
Case Else
f1GridEdit KeyCode
End Select
End Sub
Private Sub Text1_KeyDown(KeyCode As Integer, Shift As Integer)
MsgBox "text1_keydown"
Select Case KeyCode
Case vbKeyTab
grdItems.SetFocus
If grdItems.Col < grdItems.Cols - 2 Then
grdItems.Col = grdItems.Col + 1
ElseIf grdItems.Col = grdItems.Cols - 2 Then
If grdItems.Row = grdItems.Rows - 1 Then
grdItems.Row = 1
grdItems.Col = grdItems.Col - 1
Else
grdItems.Row = grdItems.Row + 1
grdItems.Col = grdItems.Col - 1
End If
End If
Case vbKeyEscape
Text1.Visible = False
grdItems.SetFocus
Case vbKeyReturn
DoEvents
grdItems.SetFocus
If grdItems.Row < grdItems.Rows - 1 Then
grdItems.Row = grdItems.Row + 1
ElseIf grdItems.Row = grdItems.Rows - 1 Then
grdItems.Row = grdItems.Row - 1
End If
Case vbKeyDown
DoEvents
grdItems.SetFocus
If grdItems.Row < grdItems.Rows - 1 Then
grdItems.Row = grdItems.Row + 1
End If
Case vbKeyUp
DoEvents
grdItems.SetFocus
If grdItems.Row > grdItems.FixedRows Then
grdItems.Row = grdItems.Row - 1
End If
Case vbKeyTab
DoEvents
grdItems.SetFocus
If grdItems.Row > grdItems.FixedRows Then
grdItems.Row = grdItems.Row - 1
End If
Case Else
'f1GridEdit KeyCode
End Select
End Sub
Private Sub grdItems_KeyPress(KeyAscii As Integer)
'MsgBox "grditems_keypress"
f1GridEdit KeyAscii
End Sub
Sub f1GridEdit(KeyAscii As Integer)
Dim cr As Integer
Dim CC As Integer
cr = grdItems.Row
CC = grdItems.Col
Text1.FontName = grdItems.FontName
Text1.FontSize = grdItems.FontSize
Select Case KeyAscii
Case 0 To Asc(" ")
Text1 = grdItems
Text1.SelStart = 1000
Case Else
Text1 = Chr(KeyAscii)
'Text1.SelStart = 0
End Select
'position the edit box
With Me
.Text1.Left = .grdItems.CellLeft + .grdItems.Left
.Text1.Top = .grdItems.CellTop + .grdItems.Top '
.Text1.Width = .grdItems.CellWidth
.Text1.Height = .grdItems.CellHeight
.Text1.Visible = True
.Text1.SetFocus
End With
End Sub
Private Sub grditems_LeaveCell()
If Text1.Visible Then
grdItems = Text1
Text1.Visible = False
End If
End Sub
Private Sub grditems_GotFocus()
Dim iCol As Integer
iCol = grdItems.Col
If Text1.Visible Then
grdItems = Text1
Text1.Visible = False
End If
End Sub
Private Sub Text1_KeyPress(KeyAscii As Integer)
MsgBox "text1_keypress"
End Sub
-
Sep 10th, 2001, 03:30 AM
#5
Hyperactive Member
The way to use tab within a flexgrid is to disable the tabstop property of other controls on the form. I use the following routines to do this:
VB Code:
Private Sub RemoveTabStops()
' Prepare controls for tabbing only within grid
Dim obj As Control
On Error Resume Next
For Each obj In Me.Controls
obj.TabStop = False
Next
fg.TabStop = True
End Sub
Private Sub RestoreTabStops()
' Restore tabbing to all controls
Dim obj As Control
On Error Resume Next
For Each obj In Me.Controls
obj.TabStop = True
Next
End Sub
You then have to handle the tab press in the flexgrid keydown event:
VB Code:
Private Sub fg_KeyDown(KeyCode As Integer, Shift As Integer)
' The tabstop property for all controls on the form have to be disabled
' so that the grid keypress event is triggered by the tab key.
' Cursor key movement is automatically handled by the flexgrid control
' and does not need to be dealt with here.
' The need to determine whether SHIFT-TAB is being pressed means that the
' KeyDown event is used rather than the KeyPress event
' Deal with normal keyboard navigation
Select Case KeyCode
' Other cases here
Case vbKeyTab ' TAB (9)
TabPressed
Exit Sub
' More stuff here
End Select
The TabPressed routine is shown below:
VB Code:
Private Sub TabPressed()
' Deal with Tab, Shift-Tab, and cursor left and right keypresses.
' In the case of Tab and Shift-Tab, the active cell will
' cycle through all cells on a row-by-row basis and will
' loop when the end/beginning of the grid is reached.
' NB This is an extension to Excel default behaviour as
' the normal tab behaviour is to cycle through the cells
' but not to change row. The cursor keys behave as for Excel.
On Error Resume Next
If Not mbCellSelectedByMouse Then
If Not mbShiftPressed Then
If fg.col + 1 <= fg.Cols - 1 Then
fg.col = fg.col + 1
Else
fg.col = 1
If fg.row + 1 <= fg.Rows - 1 Then
fg.row = fg.row + 1
Else
fg.row = 1
End If
End If
Else
If fg.col > 1 Then
fg.col = fg.col - 1
Else
fg.col = fg.Cols - 1
If fg.row > 1 Then
fg.row = fg.row - 1
Else
fg.row = fg.Rows - 1
End If
End If
End If
' The above code does the revelant grid positioning, now make sure
' that the cell to be moved to is an allowed cell
GridAllowedCells
' Check the new column is visible and scroll grid if needed
If Not fg.ColIsVisible(fg.col) Then
fg.LeftCol = fg.col
End If
' Check the new row is visible and scroll grid if needed
If Not fg.RowIsVisible(fg.row) Then
fg.TopRow = fg.row
End If
End If
mbCellSelectedByMouse = False
End Sub
The TabPressed routine above has some extra coding for my program as the user wanted to cycle through the cells and move to the next row if tabbed from the previous row on the last column. There is also a call to GridAllowedCells as again my program has to prevent access to certain cells.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|