现用的方法是在控件上面加了个TEXT控件.将他的值复制到MS中.但是当程序运行在单元格输入时TEXT的位置与底部格老有偏差.下面是原代码:希望高手指点下.
'*********************编辑使MSHFlexGrid控件可以进行输入
Private Function fgi(ByVal r As Integer, ByVal c As Integer) As Integer
fgi = c + MSHFlexGrid1.Cols * r
End Function
Private Sub MSHFlexGrid1_Click()
fgedit MSHFlexGrid1, TxtEdit, 32
End Sub
Private Sub MSHFlexGrid1_GotFocus()
'MSHFlexGrid1获得焦点
If TxtEdit.Visible = False Then
Exit Sub
End If
'复制数据隐藏文本框
MSHFlexGrid1 = TxtEdit
TxtEdit.Visible = False
End Sub
Private Sub MSHFlexGrid1_KeyPress(KeyAscii As Integer)
'处理MSHFlexGrid1接收按键,进行单元格编辑
fgedit MSHFlexGrid1, TxtEdit, KeyAscii
End Sub
Private Sub fgedit(fg As Control, Edt As Control, KeyAscii As Integer)
Select Case KeyAscii
'根据输入的字符进行相应操作
Case 0 To 32
'空格表示编辑当前文本
Edt = fg
Edt.SelStart = 0
Case Else
' 其他字符代替单元格的文本
Edt = Chr(KeyAscii)
Edt.SelStart = 0
End Select
'定位文本框到对应的单元格位置
Dim X As Integer, Y As Integer
X = 1070 '横向微调
Y = 10 '纵向微调
Edt.Move MSHFlexGrid1.CellLeft + X, MSHFlexGrid1.CellTop - Y, MSHFlexGrid1.CellWidth, MSHFlexGrid1.CellHeight
Edt.Visible = True
Edt.SetFocus
End Sub
Private Sub MSHFlexGrid1_LeaveCell()
'离开MSHFlexGrid1当前单元个前
If TxtEdit.Visible = False Then
Exit Sub
End If
'复制数据并隐藏文本框
MSHFlexGrid1 = TxtEdit
TxtEdit.Visible = False
End Sub
Private Sub TxtEdit_KeyDown(KeyCode As Integer, Shift As Integer)
editkeycode MSHFlexGrid1, TxtEdit, KeyCode, Shift
End Sub
Private Sub TxtEdit_KeyPress(KeyAscii As Integer)
If KeyAscii = 13 Then
KeyAscii = 0
End If
End Sub
'处理文本框接受字符
Private Sub editkeycode(fg As Control, Edt As Control, KeyAscii As Integer, Shift As Integer)
Select Case KeyAscii
Case 27
'ESC:隐藏文本框,返回MSHFlexGrid1
Edt.Visible = False
MSHFlexGrid1.SetFocus
Case 13
'返回MSHFlexGrid1
MSHFlexGrid1.SetFocus
Case 38
'向上
MSHFlexGrid1.SetFocus
DoEvents
If MSHFlexGrid1.Row > MSHFlexGrid1.FixedRows Then
MSHFlexGrid1.Row = MSHFlexGrid1.Row - 1
End If
Case 40
'向下
MSHFlexGrid1.SetFocus
DoEvents
If MSHFlexGrid1.Row < MSHFlexGrid1.Rows - 1 Then
MSHFlexGrid1.Row = MSHFlexGrid1.Row + 1
End If
End Select
End Sub