'标记行和列。
For i = Fg2.FixedRows To Fg2.Rows - 1
Fg2.TextArray(fgi(i, 0)) = i
Next
For i = Fg2.FixedCols To Fg2.Cols - 1
Fg2.TextArray(fgi(0, i)) = i
Next
'初始化编辑框(于是,现在可加载它)。
txtEdit = ""
End Sub
2.如下创建计算 TextArray 属性索引值的函数:
Function Fgi (r As Integer, c As Integer) As Integer
Fgi = c + Fg2.Cols * r
End Function
将下列代码添加到 Hierarchical FlexGrid 的 KeyPress 和 DblClick 事件:
Sub Fg2_KeyPress (KeyAscii As Integer)
MSHFlexGridEdit Fg2, txtEdit, KeyAscii
End Sub
Sub Fg2_DblClick ()
MSHFlexGridEdit Fg2, txtEdit, 32 '模拟一个空格。
End Sub
Sub txtEdit_KeyPress (KeyAscii As Integer)
'删除回车符,以消除嘟嘟声。
If KeyAscii = Asc(vbCr) Then KeyAscii = 0
End Sub
Sub txtEdit_KeyDown (KeyCode As Integer, _
Shift As Integer)
EditKeyCode Fg2, txtEdit, KeyCode, Shift
End Sub
Sub EditKeyCode (MSHFlexGrid As Control, Edt As _
Control, KeyCode As Integer, Shift As Integer)
'标准编辑控件处理。
Select Case KeyCode
Case 27 'ESC:隐藏焦点并将其返回 MSFlexGrid。
Edt.Visible = False
MSHFlexGrid.SetFocus
Case 13 'ENTER 将焦点返回 MSFlexGrid。
MSHFlexGrid.SetFocus
Case 38 '向上。
MSHFlexGrid.SetFocus
DoEvents
If MSFlexGrid.Row > MSFlexGrid.FixedRows Then
MSFlexGrid.Row = MSFlexGrid.Row - 1
End If
Case 40 '向下。
MSFlexGrid.SetFocus
DoEvents
If MSHFlexGrid.Row > MSHFlexGrid.FixedRows Then
MSHFlexGrid.Row = MSHFlexGrid.Row - 1
End If
End Select
End Sub
Private Sub Form_KeyPress(KeyAscii As Integer)
If KeyAscii = 13 Then SendKeys "{tab}"
End Sub
Private Sub Form_Load()
initmg1
End Sub
Private Sub mg1_Click()
If mg1.CellAlignment < 3 Then txtins.Alignment = 0 Else txtins.Alignment = 1
End Sub
Private Sub mg1_DblClick()
On Error Resume Next
If mg1.Col >= 0 Then
txtins.Left = mg1.CellLeft + mg1.Left
txtins.Top = mg1.CellTop + mg1.Top
txtins.Height = mg1.CellHeight
txtins.Width = mg1.CellWidth
txtins.Visible = True
txtins.SelStart = 0
txtins.SelLength = Len(txtins.Text)
If IsNumeric(mg1.TextMatrix(mg1.Row, mg1.Col)) = True Then txtins.Alignment = 1 Else txtins.Alignment = 0
txtins.Text = mg1.TextMatrix(mg1.Row, mg1.Col)
mg1.TextMatrix(mg1.Row, mg1.Col) = ""
txtins.SetFocus
End If
myrow = mg1.Row
mycol = mg1.Col
End Sub
Private Sub mg1_MouseDown(Button As Integer, Shift As Integer, X As Single, y As Single)
mg1.TextMatrix(myrow, mycol) = txtins.Text
txtins.Visible = False
End Sub
Private Sub mg1_Scroll()
txtins.Text = ""
txtins.Visible = False
End Sub
Public Sub initmg1()
mg1.Clear
mg1.Rows = 10
mg1.Cols = 15
'Dim X As Integer
'For X = 0 To mg1.Cols - 1
' mg1.ColAlignment(X) = 0
'Next
mg1.FixedCols = 1
mg1.FixedRows = 1