自定义文本框缩放 - 将文本返回到活动的datagridview单元格?

时间:2016-09-21 09:24:50

标签: vb.net datagridview

我有2个相同格式的Datagridview控件。每个Datagrid都有一些用户将编写长文本的列,因此我使用RichTextBox设计了表单,当用户双击这些列以放大文本条目时,该表格会打开。代码有效,但我想为两个Datagrids使用相同的表单,所以我应该以某种方式将文本返回到活动的datagridview单元格。这是我的代码(对于Datagridview1):

TextZoomForm:

Public Class TextZoomForm

   Public OpenedForm1 As New Form1
   Private Sub RichTextBox1_DoubleClick(sender As Object, e As EventArgs) Handles RichTextBox1.DoubleClick

        OpenedForm1.DataGridView1.CurrentCell.Value = RichTextBox1.Text
        OpenedForm1.Label24.Focus()
        Me.Close()

   End Sub

   Private Sub TextZoom_Load(sender As Object, e As EventArgs) Handles Me.Load
               RichTextBox1.Text = OpenedForm1.DataGridView1.CurrentCell.Value

   End Sub

End Class

Form1中的DataGridView1_CellMouseDoubleClick:

  Private Sub DataGridView1_CellMouseDoubleClick(sender As Object, e As DataGridViewCellMouseEventArgs) Handles DataGridView1.CellMouseDoubleClick

        If e.ColumnIndex = 1 Then

            Dim cp = Cursor.Position
            cp.Y += CInt(Cursor.Size.Height * (-0.5))
            cp.X += CInt(Cursor.Size.Width * 0.8)

            Dim f As New TextZoomForm()
            f.OpenedForm1 = Me
            f.Show()
            f.Location = New Point(cp)

        End If

    End Sub

关于如何将文本返回到活动数据网格视图单元的任何想法?

1 个答案:

答案 0 :(得分:1)

更改缩放的表单,以便它不知道数据的来源。相反,使用它的控件将传递数据。

Public Class TextZoomForm

    Public Property ZoomedText As String
        Get
            Return RichTextBox1.Text
        End Get
        Set(value As String)
            RichTextBox1.Text = value
        End Set
    End Property

    Private Sub RichTextBox1_DoubleClick(sender As Object, e As EventArgs) Handles RichTextBox1.DoubleClick
        Me.Close()
    End Sub

End Class

要调用表单,请将代码更改为以下内容:

Private Sub DataGridView1_CellMouseDoubleClick(sender As Object, e As DataGridViewCellMouseEventArgs) Handles DataGridView1.CellMouseDoubleClick

...

            Dim f As New TextZoomForm()
            f.ZoomedText = DataGridView1.CurrentCell.Value
            f.ShowDialog()
            'Great breakpoint location.
            DataGridView1.CurrentCell.Value = f.ZoomedText
            Label24.Focus()
....

    End Sub

使用ShowDialog可防止用户在通话过程中更改当前单元格。

如果您需要它无模式,那么您应该:

  • 存储用户已选择的单元格
  • 处理FormClosing事件。
  • 测试DialogResult以确保用户按下确定
  • 将数据写回存储的单元格。