我有一个VB6应用程序,它包含一个flexgrid,两个命令按钮和一个文本框。我有代码允许用户按向上或向下箭头键来切换网格中的行。按下向下箭头键时,光标位于下一行文本的末尾,但按下向上箭头键时,光标位于上一行文本的中间。有人对此有任何解释吗?
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
答案 0 :(得分:3)
因为在移动文本框后仍然正在处理按键本身。您应该注意到向左按下时会发生同样的事情,因为文本框中的向上和向左都会备份一个字符。完成后,请尝试将KeyCode设置为0,以防止其进一步处理。