试试这个: Option ExplicitPrivate Sub MSHFlexGrid1_Click() Call SizeText Text1.Visible = True Text1.SetFocus End SubPrivate Sub MSHFlexGrid1_GotFocus() MSHFlexGrid1_RowColChange End SubPrivate Sub MSHFlexGrid1_LeaveCell() MSHFlexGrid1.Text = Text1.Text End SubPrivate Sub MSHFlexGrid1_RowColChange() Static OldRow As Integer, OldCol As Integer, Change As Boolean
If Change Then Exit Sub Change = True
With MSHFlexGrid1 If .Col <> OldCol Or .Row <> OldRow Then OldRow = .Row OldCol = .Col
Call SizeText Text1.Visible = True Text1.SetFocus End If End With Change = False End SubPrivate Sub Text1_KeyDown(KeyCode As Integer, Shift As Integer) With MSHFlexGrid1 Select Case KeyCode
Case vbKeyRight If .Col + 2 > .Cols And .Row + 1 < .Rows Then .Col = 1 .Row = .Row + 1 ElseIf .Col + 1 < .Cols And .Row < .Rows Then .Col = .Col + 1 End If
Case vbKeyUp If .Row - 1 > 0 Then .Row = .Row - 1
Case vbKeyDown, vbKeyReturn If .Row + 1 < .Rows Then .Row = .Row + 1
Case vbKeyLeft If .Col - 1 = 0 And .Row - 1 <> 0 Then .Col = .Cols - 1 .Row = .Row - 1 ElseIf .Col - 1 <> 0 Then .Col = .Col - 1 End If End Select End With MSHFlexGrid1_RowColChange End SubPrivate Sub Text1_KeyPress(KeyAscii As Integer) If KeyAscii = vbKeyReturn Then KeyAscii = 0 If KeyAscii = vbKeyTab Then Call Text1_KeyDown(vbKeyRight, 0) KeyAscii = 0 End If End SubPrivate Sub Text1_LostFocus() Text1.Visible = False Call MSHFlexGrid1_RowColChange End SubPrivate Sub SizeText() With MSHFlexGrid1 Text1.Text = .Text Text1.FontSize = .Font.Size Text1.Height = .CellHeight
If .CellLeft + .CellWidth > .Width Then Text1.Width = .Width - .CellLeft Else Text1.Width = .CellWidth End If
Text1.Left = .CellLeft + .Left Text1.Top = .CellTop + .Top End With End Sub
好象以前看到一个贴子,一个据说还是mvp的莫XX竟然说要用timer控件,这不是开玩笑吗?真不知这个mvp是如何当上的。
Option ExplicitPrivate Sub MSHFlexGrid1_Click()
Call SizeText
Text1.Visible = True
Text1.SetFocus
End SubPrivate Sub MSHFlexGrid1_GotFocus()
MSHFlexGrid1_RowColChange
End SubPrivate Sub MSHFlexGrid1_LeaveCell()
MSHFlexGrid1.Text = Text1.Text
End SubPrivate Sub MSHFlexGrid1_RowColChange()
Static OldRow As Integer, OldCol As Integer, Change As Boolean
If Change Then Exit Sub
Change = True
With MSHFlexGrid1
If .Col <> OldCol Or .Row <> OldRow Then
OldRow = .Row
OldCol = .Col
Call SizeText
Text1.Visible = True
Text1.SetFocus
End If
End With
Change = False
End SubPrivate Sub Text1_KeyDown(KeyCode As Integer, Shift As Integer)
With MSHFlexGrid1
Select Case KeyCode
Case vbKeyRight
If .Col + 2 > .Cols And .Row + 1 < .Rows Then
.Col = 1
.Row = .Row + 1
ElseIf .Col + 1 < .Cols And .Row < .Rows Then
.Col = .Col + 1
End If
Case vbKeyUp
If .Row - 1 > 0 Then .Row = .Row - 1
Case vbKeyDown, vbKeyReturn
If .Row + 1 < .Rows Then .Row = .Row + 1
Case vbKeyLeft
If .Col - 1 = 0 And .Row - 1 <> 0 Then
.Col = .Cols - 1
.Row = .Row - 1
ElseIf .Col - 1 <> 0 Then
.Col = .Col - 1
End If
End Select
End With
MSHFlexGrid1_RowColChange
End SubPrivate Sub Text1_KeyPress(KeyAscii As Integer)
If KeyAscii = vbKeyReturn Then KeyAscii = 0
If KeyAscii = vbKeyTab Then
Call Text1_KeyDown(vbKeyRight, 0)
KeyAscii = 0
End If
End SubPrivate Sub Text1_LostFocus()
Text1.Visible = False
Call MSHFlexGrid1_RowColChange
End SubPrivate Sub SizeText()
With MSHFlexGrid1
Text1.Text = .Text
Text1.FontSize = .Font.Size
Text1.Height = .CellHeight
If .CellLeft + .CellWidth > .Width Then
Text1.Width = .Width - .CellLeft
Else
Text1.Width = .CellWidth
End If
Text1.Left = .CellLeft + .Left
Text1.Top = .CellTop + .Top
End With
End Sub
Text1.Visible = False 起作用了。
把这一句去了,就会露出马脚了。
不过,觉得这样也是可以的,只要文本框失去焦点就让其不可见也是一种解决办法。
后来弄到一个开源的表格控件,他就有这个事件。交流MSN:[email protected]