MSFlexGrid的編輯輸入

微軟的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下測試通過。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章