Why does vbkeyup produce different results than vbkeydown does in this Code.

Posted by Joshua Rhoads on Stack Overflow See other posts from Stack Overflow or by Joshua Rhoads
Published on 2010-05-10T18:00:33Z Indexed on 2010/05/10 18:04 UTC
Read the original article Hit count: 343

Filed under:
|
|

I have a VB6 app. It consists of a flexgrid. I have code to allow the user to press the up or down arrow key to switch rows in the grid. When the down arrow key is pressed the cursor is placed at the end of the text in the next row, but when the Up arrow key is pressed the cursor is placed in the middle of the text of the previous row. Anybody have any explantion for this.

Private Sub Command1_Click()

    With MSFlexGrid1
        .Cols = 4
        .Rows = 5

        .FixedCols = 1
        .FixedRows = 1

        MSFlexGrid1.TextMatrix(0, 1) = "FROM"
        MSFlexGrid1.TextMatrix(0, 2) = "THRU"
        MSFlexGrid1.TextMatrix(0, 3) = "PAGE"

        MSFlexGrid1.TextMatrix(1, 1) = "Aa"
        MSFlexGrid1.TextMatrix(1, 2) = "Az"
        MSFlexGrid1.TextMatrix(1, 3) = "-"

        MSFlexGrid1.TextMatrix(2, 1) = "Ba"
        MSFlexGrid1.TextMatrix(2, 2) = "Bz"
        MSFlexGrid1.TextMatrix(2, 3) = "-"

        MSFlexGrid1.TextMatrix(3, 1) = "Ca"
        MSFlexGrid1.TextMatrix(3, 2) = "Cz"
        MSFlexGrid1.TextMatrix(3, 3) = "-"

        MSFlexGrid1.TextMatrix(4, 1) = "Da"
        MSFlexGrid1.TextMatrix(4, 2) = "Dz"
        MSFlexGrid1.TextMatrix(4, 3) = "-"

    End With

End Sub

Private Sub Command2_Click()
    End
End Sub

Private Sub Form_Load()

    Text1.Visible = False

End Sub

Private Sub MSFlexGrid1_DblClick()

    FlexGridEdit Asc(" ")
    Exit Sub

End Sub

Private Sub FlexGridEdit(KeyAscii As Integer)

    Text1.Left = MSFlexGrid1.CellLeft + MSFlexGrid1.Left
    Text1.Top = MSFlexGrid1.CellTop + MSFlexGrid1.Top
    Text1.Width = MSFlexGrid1.ColWidth(MSFlexGrid1.Col) - 2 * (MSFlexGrid1.ColWidth   (MSFlexGrid1.Col) - MSFlexGrid1.CellWidth)
    Text1.Height = MSFlexGrid1.RowHeight(MSFlexGrid1.Row) - 2 * (MSFlexGrid1.RowHeight(MSFlexGrid1.Row) - MSFlexGrid1.CellHeight)
    Text1.MaxLength = 2
    Text1.Visible = True
    Text1.SetFocus

    Select Case KeyAscii
    Case 0 To Asc(" ")
        Text1.Text = MSFlexGrid1.Text
        Text1.SelStart = Len(Text1.Text)
    Case Else
        Text1.Text = Chr$(KeyAscii)
        Text1.SelStart = 1
    End Select

    Exit Sub

End Sub

Function ValidateFlexGrid1() As Boolean

    Dim llCntrRow       As Integer
    Dim llCntrCol       As Integer
    Dim max_len         As Single
    Dim new_len         As Single
    Dim liCntr          As Integer
    Dim lsCheck         As String

    With MSFlexGrid1
        If Text1.Visible Then .Text = Text1.Text
        If .Rows = .FixedRows Then
            ValidateFlexGrid1 = False
        End If
        For llCntrCol = .FixedCols To MSFlexGrid1.Cols - 1
            For llCntrRow = .FixedRows To MSFlexGrid1.Rows - 1
                If .TextMatrix(llCntrRow, llCntrCol) = "" Then
                    ValidateFlexGrid1 = False
                    Exit Function
                End If
            Next llCntrRow
        Next llCntrCol
    End With

    ValidateFlexGrid1 = True

    Exit Function

End Function

Private Sub Text1_Keydown(KeyCode As Integer, Shift As Integer)

Select Case KeyCode

    Case vbKeyRight, vbKeyLeft, vbKeyReturn

        If Text1.Visible = True Then
            If Text1.Text = "/" Then
                If MSFlexGrid1.Row > 1 Then
                    Text1.Text = MSFlexGrid1.TextMatrix(MSFlexGrid1.Row - 1, MSFlexGrid1.Col)
                    Text1.SelStart = Len(Text1.Text)
                End If
            End If
            MSFlexGrid1.Text = Text1.Text
            If KeyCode = vbKeyRight Or KeyCode = vbKeyReturn Then
                If Text1.SelStart = Len(Text1.Text) Then
                    FlexGridChkPos KeyCode
                    FlexGridEdit Asc(" ")
                End If
            Else
                If Text1.SelStart = 0 Then
                    FlexGridChkPos KeyCode
                    FlexGridEdit Asc(" ")
                End If
            End If
        End If

    Case vbKeyDown, vbKeyUp
        If Text1.Visible = True Then
            MSFlexGrid1.Text = Text1.Text
            FlexGridChkPos KeyCode
            FlexGridEdit Asc(" ")
        End If
End Select
Exit Sub

End Sub

Function FlexGridChkPos(KeyCode As Integer) As Boolean
    Dim llNextRow   As Long
    Dim llNextCol   As Long
    Dim llCurrCol   As Long
    Dim llCurrRow   As Long
    Dim llTotCols   As Long
    Dim llTotRows   As Long
    Dim llBegRow    As Long
    Dim llBegCol    As Long
    Dim llCntrCol   As Long
    Dim lsText      As String


    With MSFlexGrid1

        llCurrRow = .Row + 1
        llCurrCol = .Col + 1
        llTotRows = .Rows
        llTotCols = .Cols
        llBegRow = .FixedRows
        llBegCol = .FixedCols

        If KeyCode = vbKeyRight Or KeyCode = vbKeyReturn Then
            llNextCol = llCurrCol + 1
            If llNextCol > llTotCols Then
                llNextRow = llCurrRow + 1
                If llNextRow > llTotRows Then
                    .Rows = .Rows + 1
                    llCurrRow = llCurrRow + 1
                    llCurrCol = 1 + llBegCol
                Else
                    llCurrRow = llNextRow
                    llCurrCol = 1 + llBegCol
                End If
            Else
                llCurrCol = llNextCol
            End If
        End If

        If KeyCode = vbKeyLeft Then
            llNextCol = llCurrCol - 1
            If llNextCol = llBegCol Then
                llNextRow = llCurrRow - 1
                If llNextRow = llBegRow Then
                    llCurrRow = llTotRows
                Else
                    llCurrRow = llNextRow
                End If
                llCurrCol = llTotCols
            Else
                llCurrCol = llNextCol
            End If
        End If

        If KeyCode = vbKeyUp Then
            llNextRow = llCurrRow - 1
            If llNextRow = llBegRow Then
                llCurrRow = llTotRows
            Else
                llCurrRow = llNextRow
            End If
        End If

        If KeyCode = vbKeyDown Then
            llNextRow = llCurrRow + 1
            If llNextRow > llTotRows Then
                llCurrRow = llBegRow + 1
            Else
                llCurrRow = llNextRow
            End If
        End If

        .Col = llCurrCol - 1
        .Row = llCurrRow - 1

    End With
Exit Function

End Function

© Stack Overflow or respective owner

Related posts about vb6

Related posts about msflexgrid