物业公司退出小区:为MSHFlexGrid添加表格编辑功能

来源:百度文库 编辑:中财网 时间:2024/04/29 09:38:04
转载自:http://blog.csdn.net/lyserver/article/details/4188280
虽然MSHFlexGrid的功能比较强大,样式比较丰富,可惜它不支持数据编辑。我以前俺用TextBox实现了它的编辑功能,总算弥补了MSHFlexGrid的不足。

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

view plaincopy to clipboard?

  1. Option Explicit  
  2.   
  3. Private Sub Form_Load()  
  4.     Dim i As Long  
  5.       
  6.     Me.ScaleMode = vbPixels  
  7.     msGrid.Rows = 10  
  8.     msGrid.Cols = 10  
  9.     For i = 1 To 9  
  10.         msGrid.TextMatrix(i, 0) = i  
  11.         msGrid.TextMatrix(0, i) = Chr(i + 64)  
  12.     Next  
  13.     msGrid.HighLight = flexHighlightNever  
  14.     msGrid.AllowBigSelection = False  
  15.     msGrid.SelectionMode = flexSelectionFree  
  16.     msGrid.FocusRect = flexFocusNone  
  17.     txtCell.Move -100, -100  
  18.     txtCell.BorderStyle = 0  
  19. End Sub  
  20.   
  21. Private Sub msGrid_MouseDown(Button As Integer, Shift As Integer, x As Single, y As Single)  
  22.     If Button <> 1 Then Exit Sub  
  23.     msGrid.Redraw = False  
  24. End Sub  
  25.   
  26. Private Sub msGrid_MouseUp(Button As Integer, Shift As Integer, x As Single, y As Single)  
  27.     If Button <> 1 Then Exit Sub  
  28.     msGrid.ColSel = msGrid.Col  
  29.     msGrid.RowSel = msGrid.Row  
  30.     msGrid.Redraw = True  
  31.     If msGrid.Row > msGrid.FixedRows - 1 And msGrid.Col > msGrid.FixedCols - 1 And msGrid.CellWidth > 0 And msGrid.CellHeight > 0 Then  
  32.         txtCell.Move msGrid.Left - 1 + msGrid.CellLeft / 15, msGrid.Top - 1 + msGrid.CellTop / 15, msGrid.CellWidth / 15, msGrid.CellHeight / 15  
  33.         txtCell.Tag = "No"  
  34.         txtCell.Text = msGrid.Text  
  35.         txtCell.Tag = ""  
  36.         txtCell.SetFocus  
  37.     End If  
  38. End Sub  
  39.   
  40. Private Sub msGrid_Scroll()  
  41.     txtCell.Move -100, -100  
  42. End Sub  
  43.   
  44. Private Sub txtCell_Change()  
  45.     If txtCell.Tag <> "No" And msGrid.Col >= msGrid.FixedCols And msGrid.Row >= msGrid.FixedRows Then  
  46.         msGrid.Text = txtCell.Text  
  47.     End If  
  48. End Sub  
  49.   
  50. Private Sub txtCell_KeyDown(KeyCode As Integer, Shift As Integer)  
  51.     Select Case KeyCode  
  52.         Case 37  
  53.             If msGrid.Col > msGrid.FixedCols And txtCell.SelStart = 0 Then  
  54.                 msGrid.Col = msGrid.Col - 1  
  55.                 KeyCode = 0  
  56.             End If  
  57.         Case 38  
  58.             If msGrid.Row > msGrid.FixedRows And txtCell.SelStart = 0 Then  
  59.                 msGrid.Row = msGrid.Row - 1  
  60.                 KeyCode = 0  
  61.             End If  
  62.         Case 39  
  63.             If msGrid.Col < msGrid.Cols - 1 And txtCell.SelStart = Len(txtCell.Text) Then  
  64.                 msGrid.Col = msGrid.Col + 1  
  65.                 KeyCode = 0  
  66.             End If  
  67.         Case 40  
  68.             If msGrid.Row < msGrid.Rows - 1 And txtCell.SelStart = Len(txtCell.Text) Then  
  69.                 msGrid.Row = msGrid.Row + 1  
  70.                 KeyCode = 0  
  71.             End If  
  72.         Case 13  
  73.             If msGrid.Col < msGrid.Cols - 1 Then  
  74.                 msGrid.Col = msGrid.Col + 1  
  75.             Else  
  76.                 If msGrid.Row < msGrid.Rows - 1 Then msGrid.Row = msGrid.Row + 1  
  77.                 msGrid.Col = msGrid.FixedCols  
  78.             End If  
  79.             KeyCode = 0  
  80.         Case 33  
  81.             msGrid.SetFocus  
  82.             SendKeys Chr(33)  
  83.         Case 34  
  84.             msGrid.SetFocus  
  85.             SendKeys Chr(34)  
  86.     End Select  
  87.     If KeyCode = 0 And msGrid.CellWidth > 0 And msGrid.CellHeight > 0 Then  
  88.         txtCell.Text = msGrid.Text  
  89.         txtCell.Move msGrid.Left - 1 + msGrid.CellLeft / 15, msGrid.Top - 1 + msGrid.CellTop / 15, msGrid.CellWidth / 15, msGrid.CellHeight / 15  
  90.         txtCell.SelStart = 0  
  91.         txtCell.SelLength = Len(txtCell.Text)  
  92.     End If  
  93. End Sub