微软的MSFlexGrid控件可以显示文本,图片,更可以和数据库控件绑定,具有强大的灵活性,但是在运行时不能对网格内的数据进行修改的确是一件憾事,但是通过代码这个问题同样可以解决。
思路是在编辑网格内文本时,先将一文本框定位到网格内,在文本框中输入内容,编辑完成后,再将文本框中内容赋值给网格,这样就实现了,网格控件的编辑输入。
代码如下:
Option Explicit
Private Sub Combo1_KeyPress(KeyAscii As Integer)
Dim i As Integer, bSame As Boolean
If KeyAscii = vbKeyEscape Then
Combo1.Visible = False
MSFlexGrid1.SetFocus
Exit Sub
End If
If KeyAscii = vbKeyReturn Then
MSFlexGrid1.Text = Combo1.Text
Combo1.Visible = False
MSFlexGrid1.SetFocus
With Combo1
bSame = False
For i = 0 To .ListCount
If .Text = .List(i) Then bSame = True
Next i
If Not bSame Then .AddItem .Text
End With
End If
End Sub
Private Sub Combo1_LostFocus()
Combo1.Visible = False
MSFlexGrid1.SetFocus
End Sub
Private Sub Form_Load()
Dim i As Integer
With MSFlexGrid1
.Cols = 4
.Rows = 5
For i = 0 To 4
.RowHeight(i) = 300
Next i
End With
For i = 1 To 10
Combo1.AddItem i
Next i
Label1.Caption = "在第一、二列中,双击左键,会出现一文字框(TextBox)..." & vbCr & _
"而第三、四列,会出现选择类表单(ComboBox)..." & vbCr & _
"输入完毕后按下Enter键,资料即可保留于MSFlexGrid中," & vbCr & _
"而按下Esc键则取消输入..."
End Sub
Private Sub MSFlexGrid1_DblClick()
Dim c As Integer, r As Integer
With MSFlexGrid1
c = .Col: r = .Row
If c <= 1 Then
Text1.Left = .Left + .ColPos(c)
Text1.Top = .Top + .RowPos(r)
Text1.Width = .ColWidth(c) - 10
Text1.Height = .RowHeight(r) - 10
Text1 = .Text
Text1.Visible = True
Text1.SetFocus
Else
Combo1.Left = .Left + .ColPos(c)
Combo1.Top = .Top + .RowPos(r)
Combo1.Width = .ColWidth(c)
Combo1.Text = .Text
Combo1.Visible = True
Combo1.SetFocus
End If
End With
End Sub
Private Sub MSFlexGrid1_KeyPress(KeyAscii As Integer)
If KeyAscii = vbKeyReturn Then
Call MSFlexGrid1_DblClick
End If
End Sub
Private Sub Text1_KeyPress(KeyAscii As Integer)
If KeyAscii = vbKeyEscape Then
Text1.Visible = False
MSFlexGrid1.SetFocus
Exit Sub
End If
If KeyAscii = vbKeyReturn Then
MSFlexGrid1.Text = Text1.Text
Text1.Visible = False
MSFlexGrid1.SetFocus
End If
End Sub
Private Sub Text1_LostFocus()
Text1.Visible = False
MSFlexGrid1.SetFocus
End Sub
本程序在VB6.0+Windows2000下测试通过。
本文介绍了一种在MSFlexGrid控件中实现单元格编辑的方法,通过使用文本框和组合框来修改网格内的数据,并提供了完整的VB代码示例。
6424

被折叠的 条评论
为什么被折叠?



