Const ASC_ENTER = 13 '回车
Dim gRow As Integer
Dim gCol As Integer
5 增加代码到 Grid_KeyPress 过程:
Private Sub Grid1_KeyPress(KeyAscii As Integer)
' Move the text box to the current grid cell:
Text1.Top = Grid1.CellTop + Grid1.Top
Text1.Left = Grid1.CellLeft + Grid1.Left
' Save the position of the grids Row and Col for later:
gRow = Grid1.Row
gCol = Grid1.Col
' Make text box same size as current grid cell:
Text1.Width = Grid1.CellWidth - 2 * Screen.TwipsPerPixelX
Text1.Height = Grid1.CellHeight - 2 * Screen.TwipsPerPixelY
' Transfer the grid cell text:
Text1.Text = Grid1.Text
' Show the text box:
Text1.Visible = True
Text1.ZOrder 0 ' 把 Text1 放到最前面!
Text1.SetFocus
' Redirect this KeyPress event to the text box:
If KeyAscii <> ASC_ENTER Then
SendKeys Chr$(KeyAscii)
End If
End Sub
6 增加代码到 Text1_KeyPress 过程:
Private Sub Text1_KeyPress(KeyAscii As Integer)
If KeyAscii = ASC_ENTER Then
Grid1.SetFocus ' Set focus back to grid, see Text_LostFocus.
KeyAscii = 0 ' Ignore this KeyPress.
End If
End Sub
7 增加代码到 Text1_LostFocus 过程:
Private Sub Text1_LostFocus()
Dim tmpRow As Integer
Dim tmpCol As Integer
' Save current settings of Grid Row and col. This is needed only if
' the focus is set somewhere else in the Grid.
tmpRow = Grid1.Row
tmpCol = Grid1.Col
' Set Row and Col back to what they were before Text1_LostFocus:
Grid1.Row = gRow
Grid1.Col = gCol
Grid1.Text = Text1.Text ' Transfer text back to grid.
Text1.SelStart = 0 ' Return caret to beginning.
Text1.Visible = False ' Disable text box.
' Return row and Col contents:
Grid1.Row = tmpRow
Grid1.Col = tmpCol
End Sub
Private Sub GetText(ByVal msfGrid As MSFlexGrid, ByVal txt As TextBox, ByVal bln As Boolean)
With msfGrid
If MSFGridCol >= 1 And txt.Text <> "" Then
.TextMatrix(MSFGridRow, MSFGridCol) = txt.Text
txt.Text = ""
txt.Visible = False
End If
If Not bln Then Exit Sub
txt.Move .Left + .CellLeft, .Top + .CellTop, .CellWidth, .CellHeight
txt.Visible = True
txt.SetFocus
txt.Text = .TextMatrix(.Row, .Col)
.TextMatrix(.Row, .Col) = ""
MSFGridRow = .Row
MSFGridCol = .Col
End With
End Sub
然后在msflexgrid的click或dbclick中调用。
在窗体上加一个文本框Text1,并设置它的visible属性为false,假定你的网格控件为Grid1
Sub Grid1_KeyPress(KeyAscii as integer)
Text1.text=text1.text & chr(KeyAscii)
Text1.SelStart=1
Text1.Move Grid1.CellLeft+Grid1.Left,_
Grid1.CellTop+Grid1.Top,_
Grid1.CellWidth,Grid1.CellHeight
Text1.Visible=True
Text1.SetFous
End Sub
Sub Grid1_LeaveCell()
If Text1.visible=False then
Exit sub
End if
Grid1.Text=Text1
Text1.Visible=False
Text1.Text=""
End sub