Results 1 to 5 of 5

Thread: Flex Grid

  1. #1

    Thread Starter
    New Member
    Join Date
    Aug 2001
    Location
    Australia
    Posts
    8

    Unhappy 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?

  2. #2
    -= B u g S l a y e r =- peet's Avatar
    Join Date
    Aug 2000
    Posts
    9,629
    Could you show us some of the code?
    -= a peet post =-

  3. #3
    I'm about to be a PowerPoster! Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,649
    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:
    1. Private Sub Text1_KeyDown(KeyCode As Integer, Shift As Integer)
    2.     If KeyCode = vbKeyTab Then
    3.         MsgBox "Tab was pressed", vbInformation
    4.     End If
    5. End Sub
    Best regards

  4. #4

    Thread Starter
    New Member
    Join Date
    Aug 2001
    Location
    Australia
    Posts
    8
    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

  5. #5
    Hyperactive Member
    Join Date
    Jan 2000
    Location
    Edinburgh, Scotland
    Posts
    272
    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:
    1. Private Sub RemoveTabStops()
    2. ' Prepare controls for tabbing only within grid
    3.  
    4. Dim obj As Control
    5.  
    6.     On Error Resume Next
    7.    
    8.     For Each obj In Me.Controls
    9.         obj.TabStop = False
    10.     Next
    11.     fg.TabStop = True
    12.    
    13. End Sub
    14.  
    15.  
    16. Private Sub RestoreTabStops()
    17. ' Restore tabbing to all controls
    18.  
    19. Dim obj As Control
    20.  
    21.     On Error Resume Next
    22.    
    23.     For Each obj In Me.Controls
    24.         obj.TabStop = True
    25.     Next
    26.    
    27. End Sub

    You then have to handle the tab press in the flexgrid keydown event:

    VB Code:
    1. Private Sub fg_KeyDown(KeyCode As Integer, Shift As Integer)
    2. ' The tabstop property for all controls on the form have to be disabled
    3. ' so that the grid keypress event is triggered by the tab key.
    4. ' Cursor key movement is automatically handled by the flexgrid control
    5. ' and does not need to be dealt with here.
    6. ' The need to determine whether SHIFT-TAB is being pressed means that the
    7. ' KeyDown event is used rather than the KeyPress event
    8.     ' Deal with normal keyboard navigation
    9.  
    10.     Select Case KeyCode
    11.         ' Other cases here
    12.         Case vbKeyTab             ' TAB (9)
    13.             TabPressed
    14.             Exit Sub
    15.         ' More stuff here
    16.     End Select

    The TabPressed routine is shown below:

    VB Code:
    1. Private Sub TabPressed()
    2. ' Deal with Tab, Shift-Tab, and cursor left and right keypresses.
    3. ' In the case of Tab and Shift-Tab, the active cell will
    4. ' cycle through all cells on a row-by-row basis and will
    5. ' loop when the end/beginning of the grid is reached.
    6. ' NB This is an extension to Excel default behaviour as
    7. ' the normal tab behaviour is to cycle through the cells
    8. ' but not to change row.  The cursor keys behave as for Excel.
    9.  
    10.     On Error Resume Next
    11.    
    12.     If Not mbCellSelectedByMouse Then
    13.         If Not mbShiftPressed Then
    14.             If fg.col + 1 <= fg.Cols - 1 Then
    15.                 fg.col = fg.col + 1
    16.             Else
    17.                 fg.col = 1
    18.                 If fg.row + 1 <= fg.Rows - 1 Then
    19.                     fg.row = fg.row + 1
    20.                 Else
    21.                     fg.row = 1
    22.                 End If
    23.             End If
    24.         Else
    25.             If fg.col > 1 Then
    26.                 fg.col = fg.col - 1
    27.             Else
    28.                 fg.col = fg.Cols - 1
    29.                 If fg.row > 1 Then
    30.                     fg.row = fg.row - 1
    31.                 Else
    32.                     fg.row = fg.Rows - 1
    33.                 End If
    34.             End If
    35.         End If
    36.        
    37.         ' The above code does the revelant grid positioning, now make sure
    38.         ' that the cell to be moved to is an allowed cell
    39.         GridAllowedCells
    40.        
    41.         ' Check the new column is visible and scroll grid if needed
    42.         If Not fg.ColIsVisible(fg.col) Then
    43.             fg.LeftCol = fg.col
    44.         End If
    45.        
    46.         ' Check the new row is visible and scroll grid if needed
    47.         If Not fg.RowIsVisible(fg.row) Then
    48.             fg.TopRow = fg.row
    49.         End If
    50.     End If
    51.     mbCellSelectedByMouse = False
    52. 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
  •  



Click Here to Expand Forum to Full Width