您的位置:首页 > 编程语言 > VB

为MSHFlexGrid添加表格编辑功能

2009-05-15 12:12 309 查看
     虽然MSHFlexGrid的功能比较强大,样式比较丰富,可惜它不支持数据编辑。我以前俺用TextBox实现了它的编辑功能,总算弥补了MSHFlexGrid的不足。

    首先,新建一个标准EXE工程,然后, 在工程部件里选择“Microsoft Hierarchical Flex Grid Control 6.0 (OLEDB)”,在Form里添加一个MSHFlexGrid控件,命名为msGrid,再添加一个TextBox控件,命名为txtCell,最后在Form的代码窗口里粘贴以下代码,运行一下工程,一个支持数据编辑的表格就算大功告成了。

Option Explicit

Private Sub Form_Load()
Dim i As Long

Me.ScaleMode = vbPixels
msGrid.Rows = 10
msGrid.Cols = 10
For i = 1 To 9
msGrid.TextMatrix(i, 0) = i
msGrid.TextMatrix(0, i) = Chr(i + 64)
Next
msGrid.HighLight = flexHighlightNever
msGrid.AllowBigSelection = False
msGrid.SelectionMode = flexSelectionFree
msGrid.FocusRect = flexFocusNone
txtCell.Move -100, -100
txtCell.BorderStyle = 0
End Sub

Private Sub msGrid_MouseDown(Button As Integer, Shift As Integer, x As Single, y As Single)
If Button <> 1 Then Exit Sub
msGrid.Redraw = False
End Sub

Private Sub msGrid_MouseUp(Button As Integer, Shift As Integer, x As Single, y As Single)
If Button <> 1 Then Exit Sub
msGrid.ColSel = msGrid.Col
msGrid.RowSel = msGrid.Row
msGrid.Redraw = True
If msGrid.Row > msGrid.FixedRows - 1 And msGrid.Col > msGrid.FixedCols - 1 And msGrid.CellWidth > 0 And msGrid.CellHeight > 0 Then
txtCell.Move msGrid.Left - 1 + msGrid.CellLeft / 15, msGrid.Top - 1 + msGrid.CellTop / 15, msGrid.CellWidth / 15, msGrid.CellHeight / 15
txtCell.Tag = "No"
txtCell.Text = msGrid.Text
txtCell.Tag = ""
txtCell.SetFocus
End If
End Sub

Private Sub msGrid_Scroll()
txtCell.Move -100, -100
End Sub

Private Sub txtCell_Change()
If txtCell.Tag <> "No" And msGrid.Col >= msGrid.FixedCols And msGrid.Row >= msGrid.FixedRows Then
msGrid.Text = txtCell.Text
End If
End Sub

Private Sub txtCell_KeyDown(KeyCode As Integer, Shift As Integer)
Select Case KeyCode
Case 37
If msGrid.Col > msGrid.FixedCols And txtCell.SelStart = 0 Then
msGrid.Col = msGrid.Col - 1
KeyCode = 0
End If
Case 38
If msGrid.Row > msGrid.FixedRows And txtCell.SelStart = 0 Then
msGrid.Row = msGrid.Row - 1
KeyCode = 0
End If
Case 39
If msGrid.Col < msGrid.Cols - 1 And txtCell.SelStart = Len(txtCell.Text) Then
msGrid.Col = msGrid.Col + 1
KeyCode = 0
End If
Case 40
If msGrid.Row < msGrid.Rows - 1 And txtCell.SelStart = Len(txtCell.Text) Then
msGrid.Row = msGrid.Row + 1
KeyCode = 0
End If
Case 13
If msGrid.Col < msGrid.Cols - 1 Then
msGrid.Col = msGrid.Col + 1
Else
If msGrid.Row < msGrid.Rows - 1 Then msGrid.Row = msGrid.Row + 1
msGrid.Col = msGrid.FixedCols
End If
KeyCode = 0
Case 33
msGrid.SetFocus
SendKeys Chr(33)
Case 34
msGrid.SetFocus
SendKeys Chr(34)
End Select
If KeyCode = 0 And msGrid.CellWidth > 0 And msGrid.CellHeight > 0 Then
txtCell.Text = msGrid.Text
txtCell.Move msGrid.Left - 1 + msGrid.CellLeft / 15, msGrid.Top - 1 + msGrid.CellTop / 15, msGrid.CellWidth / 15, msGrid.CellHeight / 15
txtCell.SelStart = 0
txtCell.SelLength = Len(txtCell.Text)
End If
End Sub
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息