2010-05-10 85 views
1

我有一個由flexgrid,兩個命令按鈕和一個文本框組成的VB6應用程序。我有代碼允許用戶按下向上或向下箭頭鍵來切換網格中的行。當按下向下箭頭鍵時,光標位於下一行文本的末尾,但當按下向上箭頭鍵時,光標位於前一行文本的中間。有人對此有任何解釋嗎?爲什麼vbkeyup產生與vbkeydown不同的結果

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 

回答

3

因爲移動文本框之後按鍵本身仍在處理中。你應該注意到,按下左鍵時會發生同樣的情況,因爲在文本框中向上和向左都備份了一個字符。嘗試將KeyCode設置爲0後,以防止它進一步處理。

+0

非常感謝你。這一直讓我瘋狂。將鍵碼設置爲0似乎已修復它。 – 2010-05-11 16:56:57

相關問題