为什么在此代码中 vbkeyup 产生的结果与 vbkeydown 产生的结果不同
我有一个 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
I have a VB6 app which consists of a flexgrid, two command buttons, and a text box. 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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
因为在移动文本框后,按键本身仍在处理中。您应该注意到,按向左键时会发生相同的情况,因为文本框中的向上和向左都会备份一个字符。完成后尝试将 KeyCode 设置为 0,以防止进一步处理。
Because the key press itself is still being processed after you move the text box. You should notice that the same thing happens when pressing left, because both up and left within the text box back up a character. Try setting KeyCode to 0 after you're done with it to keep it from being processed further.