mfgGrid_EnterCell End SubPrivate Sub MoveTextBox() With mfgGrid If .Rows > 0 Then Dim lh As Long lh = .RowHeight(0)
Dim lt As Long lt = (.Row - .TopRow) * lh
If .Col = 0 Then Dim ll As Long ll = 0
Dim lw As Long lw = .ColWidth(0) Else ll = .ColWidth(0) lw = .ColWidth(1) End If
txtEdit.Move ll, lt, lw - glngX, lh - glngY End If End With
End SubPrivate Sub mfgGrid_EnterCell() On Error GoTo ErrorHandle With mfgGrid txtEdit = .Text blnChanged = False MoveTextBox End With Exit Sub
ErrorHandle: ShowErr End SubPrivate Sub mfgGrid_KeyPress(KeyAscii As Integer)
On Error GoTo ErrorHandle
With mfgGrid Dim l As Long l = .Row If l >= 0 Then Select Case KeyAscii Case 0 To &H1F Case Else
Dim s As String s = .Text
blnInEdit = True
With txtEdit
If KeyAscii = vbKeySpace Then .Text = s blnChanged = False
TextSelectAll txtEdit
.SetFocus Else .Text = vbNullString .SetFocus
SendKeys Chr$(KeyAscii), True End If End With End Select End If End With Exit Sub
ErrorHandle: If Err <> 381 Then ShowErr End SubPrivate Sub mfgGrid_LeaveCell() On Error GoTo ErrorHandle txtEdit_LostFocus Exit Sub
ErrorHandle: ShowErr End Sub Private Sub mfgGrid_Scroll() On Error GoTo ErrorHandle MoveTextBox Exit Sub
ErrorHandle: ShowErr End SubPrivate Sub txtEdit_Change() blnChanged = True End SubPrivate Sub txtEdit_Click() If Not blnInEdit Then On Error GoTo ErrorHandle mfgGrid_KeyPress vbKeySpace End If Exit Sub
ErrorHandle: ShowErr End Sub Private Sub txtEdit_KeyPress(KeyAscii As Integer) On Error GoTo ErrorHandle Select Case KeyAscii Case vbKeyEscape txtEdit = mfgGrid.Text blnChanged = False GoTo SetFocusToGrid Case vbKeyReturn
SetFocusToGrid: KeyAscii = 0 mfgGrid.SetFocus End Select Exit Sub
ErrorHandle: ShowErr End Sub Private Sub txtEdit_LostFocus() blnInEdit = False
On Error GoTo ErrorHandle
If blnChanged Then With txtEdit If .Visible Then .SelStart = 0
Dim s As String mfgGrid.Text = .Text End If End With blnChanged = False End If Exit Sub
ErrorHandle: txtEdit = mfgGrid.Text blnChanged = False ShowErr End Sub
'Form1.frm 文件VERSION 5.00
Object = "{5E9E78A0-531B-11CF-91F6-C2863C385E30}#1.0#0"; "MSFLXGRD.OCX"
Begin VB.Form Form1
Caption = "Form1"
ClientHeight = 5355
ClientLeft = 1545
ClientTop = 1920
ClientWidth = 6420
LinkTopic = "Form1"
ScaleHeight = 5355
ScaleWidth = 6420
Begin VB.PictureBox Picture1
Height = 3495
Left = 1230
ScaleHeight = 3435
ScaleWidth = 3495
TabIndex = 0
Top = 990
Width = 3555
Begin VB.TextBox txtEdit
Height = 405
Left = 1260
TabIndex = 1
Top = 1440
Width = 1665
End
Begin MSFlexGridLib.MSFlexGrid mfgGrid
Height = 3585
Left = 0
TabIndex = 2
Top = 0
Width = 3795
_ExtentX = 6694
_ExtentY = 6324
_Version = 393216
Rows = 20
FixedRows = 0
FixedCols = 0
End
End
End
Attribute VB_Name = "Form1"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = True
Attribute VB_Exposed = False
Option ExplicitPrivate Declare Function SetParent Lib "user32" (ByVal hWndChild As Long, ByVal hWndNewParent As Long) As LongPrivate glngX As Long
Private glngY As Long
Private blnChanged As Boolean
Private blnInEdit As BooleanPrivate lngRowHeight As LongPrivate Sub TextSelectAll(Tb As TextBox)
With Tb
.SelStart = 0
.SelStart = 32767
End With
End Sub
Private Sub ShowErr()
MsgBox Err.Description, vbCritical
End SubPrivate Sub Form_Load() With Screen
glngX = .TwipsPerPixelX
glngY = .TwipsPerPixelY
End With With txtEdit
'.Visible = False
.Appearance = 0
.BorderStyle = vbBSNone
Dim l As Long
l = .hWnd
.Text = vbNullString
blnChanged = False
End With With mfgGrid
SetParent l, .hWnd
.Appearance = flexFlat
.AllowBigSelection = False
.BackColorBkg = &H80000005
.BorderStyle = flexBorderNone
.FixedRows = 0
.FixedCols = 0
.FocusRect = flexFocusNone
lngRowHeight = .RowHeight(0)
End With
mfgGrid_EnterCell
End SubPrivate Sub MoveTextBox()
With mfgGrid
If .Rows > 0 Then
Dim lh As Long
lh = .RowHeight(0)
Dim lt As Long
lt = (.Row - .TopRow) * lh
If .Col = 0 Then
Dim ll As Long
ll = 0
Dim lw As Long
lw = .ColWidth(0)
Else
ll = .ColWidth(0)
lw = .ColWidth(1)
End If
txtEdit.Move ll, lt, lw - glngX, lh - glngY
End If
End With
End SubPrivate Sub mfgGrid_EnterCell()
On Error GoTo ErrorHandle
With mfgGrid
txtEdit = .Text
blnChanged = False
MoveTextBox
End With
Exit Sub
ErrorHandle:
ShowErr
End SubPrivate Sub mfgGrid_KeyPress(KeyAscii As Integer)
On Error GoTo ErrorHandle
With mfgGrid
Dim l As Long
l = .Row
If l >= 0 Then
Select Case KeyAscii
Case 0 To &H1F
Case Else
Dim s As String
s = .Text
blnInEdit = True
With txtEdit
If KeyAscii = vbKeySpace Then
.Text = s
blnChanged = False
TextSelectAll txtEdit
.SetFocus
Else
.Text = vbNullString
.SetFocus
SendKeys Chr$(KeyAscii), True
End If
End With
End Select
End If
End With
Exit Sub
ErrorHandle:
If Err <> 381 Then ShowErr
End SubPrivate Sub mfgGrid_LeaveCell()
On Error GoTo ErrorHandle
txtEdit_LostFocus
Exit Sub
ErrorHandle:
ShowErr
End Sub
Private Sub mfgGrid_Scroll()
On Error GoTo ErrorHandle
MoveTextBox
Exit Sub
ErrorHandle:
ShowErr
End SubPrivate Sub txtEdit_Change()
blnChanged = True
End SubPrivate Sub txtEdit_Click()
If Not blnInEdit Then
On Error GoTo ErrorHandle
mfgGrid_KeyPress vbKeySpace
End If
Exit Sub
ErrorHandle:
ShowErr
End Sub
Private Sub txtEdit_KeyPress(KeyAscii As Integer)
On Error GoTo ErrorHandle
Select Case KeyAscii
Case vbKeyEscape
txtEdit = mfgGrid.Text
blnChanged = False
GoTo SetFocusToGrid
Case vbKeyReturn
SetFocusToGrid:
KeyAscii = 0
mfgGrid.SetFocus
End Select
Exit Sub
ErrorHandle:
ShowErr
End Sub
Private Sub txtEdit_LostFocus()
blnInEdit = False
On Error GoTo ErrorHandle
If blnChanged Then
With txtEdit
If .Visible Then
.SelStart = 0
Dim s As String
mfgGrid.Text = .Text
End If
End With
blnChanged = False
End If
Exit Sub
ErrorHandle:
txtEdit = mfgGrid.Text
blnChanged = False
ShowErr
End Sub
在FORM 中有一个文本框
With grid
.RowSel = .Row
.ColSel = .Col
Text1.Move .Left + .CellLeft, .Top + .CellTop, .CellWidth - ScaleX(1, vbPixels, vbTwips), .CellHeight - ScaleY(1, vbPixels, vbTwips)
Text1.Text = .Text
Text1.Visible = True
Text1.ZOrder
Text1.SetFocus
End With
编辑完后将文本框的值赋给GRID即可。
当然还是用当同的原理将其HIDE。