请问 如何能直接编辑MSHFlexGrid 的内容。

解决方案 »

  1.   

    '------------------------------------------------
    '本实例要在窗体上放一个MSHFlexGrid1,一个Text1
    '------------------------------------------------Const ASC_ENTER = 13 '回车
    Dim gRow As Integer
    Dim gCol As Integer
    Private Sub Form_Load()
        MSHFlexGrid1.Cols = 10
        MSHFlexGrid1.Rows = 10
        Text1.BackColor = vbBlue
        Text1.ForeColor = vbRed
        Text1.Visible = False
    End SubPrivate Sub MSHFlexGrid1_KeyPress(KeyAscii As Integer)
        ' Move the text box to the current grid cell:
        Text1.Top = MSHFlexGrid1.CellTop + MSHFlexGrid1.Top
        Text1.Left = MSHFlexGrid1.CellLeft + MSHFlexGrid1.Left
        ' Save the position of the grids Row and Col for later:
        gRow = MSHFlexGrid1.Row
        gCol = MSHFlexGrid1.Col
        ' Make text box same size as current grid cell:
        Text1.Width = MSHFlexGrid1.CellWidth - 2 * Screen.TwipsPerPixelX
        Text1.Height = MSHFlexGrid1.CellHeight - 2 * Screen.TwipsPerPixelY
        ' Transfer the grid cell text:
        Text1.Text = MSHFlexGrid1.Text
        ' Show the text box:
        Text1.Visible = True
        Text1.ZOrder 0 ' 把 Text1 放到最前面!
        Text1.SetFocus
        ' Redirect this KeyPress event to the text box:
        If KeyAscii <> ASC_ENTER Then
            SendKeys Chr$(KeyAscii)
        End If
    End SubPrivate Sub Text1_KeyPress(KeyAscii As Integer)
        If KeyAscii = ASC_ENTER Then
            MSHFlexGrid1.SetFocus
            KeyAscii = 0
        End If
    End Sub
    Private Sub Text1_LostFocus()
        Dim tmpRow As Integer
        Dim tmpCol As Integer
        tmpRow = MSHFlexGrid1.Row
        tmpCol = MSHFlexGrid1.Col
        MSHFlexGrid1.Row = gRow
        MSHFlexGrid1.Col = gCol
        MSHFlexGrid1.Text = Text1.Text ' Transfer text back to grid.
        Text1.SelStart = 0 ' Return caret to beginning.
        Text1.Visible = False ' Disable text box.
        MSHFlexGrid1.Row = tmpRow
        MSHFlexGrid1.Col = tmpCol
    End Sub