如何将值从一个表单传递到另一个表单的动态创建控件

时间:2014-02-27 10:33:22

标签: .net vb.net winforms datagridview

假设我有两种形式,即Form1Form2。 Form1包含动态创建的DataGridView控件,Form2包含两个Textbox控件(Textbox1Textbox2)以及Button控件。

当我在DoubleClick的小区DataGridView时,它会打开Form2,当前所选小区的数据会传递到Form2的{​​{1}} < / p>

以下是代码:

我在动态创建的DatagridView中添加了一个句柄,如下所示:


    AddHandler dg.CellMouseDoubleClick, AddressOf dg_DataGridEdit


    Private Sub dg_DataGridEdit(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellMouseEventArgs)
                Dim dgView As DataGridView = DirectCast(sender, DataGridView)
        Form2.TextBox1.Text = dgView.CurrentCell.Value.ToString()
                Form2.ShowDialog()
    End Sub

当我单击Form2中的按钮时,当前所选单元格的值将像Form2中的TextBox2一样改变。但问题是我无法使用From2中的button1代码


    Form1.dgView.CurrentCell.Value = TextBox2.Text

如何将值从textbox2传递到当前选定的单元格?

5 个答案:

答案 0 :(得分:1)

创建DataGridView时,请存储对它的引用:

private _myDgv as DataGridView

Sub Form_load
    _myDgv = New dataGridView
    Me.Controls.Add(_myDgv)
    'etc.
End Sub

然后添加一个ReadOnly属性以从其他地方获取对它的引用:

Public ReadOnly Property DynamicDgv As DataGridView
    Get
        Return _myDgv
    End Get
End Property

然后你可以在Form2中执行此操作:

Form1.DynamicDgv.CurrentCell.Value = TextBox2.Text

答案 1 :(得分:1)

我不会使用默认实例。以下是在每个表单上使用文本框的示例:

Form1中

Public Class Form1

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Dim f As New Form2(TextBox1F1) 'pass ref to form2
        f.ShowDialog()
    End Sub
End Class

窗口2

Public Class Form2

    Dim txtbox As TextBox

    Public Sub New(tb As TextBox)

        ' This call is required by the designer.
        InitializeComponent()

        ' Add any initialization after the InitializeComponent() call.
        txtbox = tb 'get ref from calling form
    End Sub

    Private Sub Form2_Shown(sender As Object, e As EventArgs) Handles Me.Shown
        TextBox1F2.Text = txtbox.Text
    End Sub

    Private Sub TextBox1_TextChanged(sender As Object, e As EventArgs) Handles TextBox1F2.TextChanged
        txtbox.Text = TextBox1F2.Text
    End Sub
End Class

对form2上文本框的任何更改都将反映在form1上的文本框中。

答案 2 :(得分:0)

您可以将Form1的数据网格视图传递给Form2Form2的构造函数。然后,您就可以像访问上一个代码段一样访问CurrentCell的数据网格视图的Form1

你可以在Form2中有一个构造函数,它将datagridview作为参数。

Public Class Form2

    Private dgView As New DataGridView
    Public Sub New(ByRef _dgView As DataGridView)
        dgView = _dgView
    End Sub

End Class

Form2中创建Form1的实例时,将dgView传递给Form2,如:

Public Class Form1
    Dim Form2 As New Form2(dgView)

End Class

现在,当您单击Form2中的按钮时,只需在按钮事件处理程序中设置dgView的CurrentCell,如:

dgView.CurrentCell.Value = TextBox2.Text

答案 3 :(得分:0)

尝试复制到剪贴板作为解决方法,因此您不必将文本从一个方法传递到下一个方法

System.Windows.Forms.Clipboard.SetText(...)

System.Windows.Forms.Clipboard.GetText(...)

答案 4 :(得分:0)

好的我明白了。 起初我创建了一个名为

的公共变量

    Public UpdateData As String = ""

然后更改代码 -

    Private Sub dg_DataGridEdit(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellMouseEventArgs)

                Dim dgView As DataGridView = DirectCast(sender, DataGridView)
                Form2.TextBox1.Text = dgView.CurrentCell.Value.ToString()
                Form2.ShowDialog()
                dgView.CurrentCell.Value =UpdateData
                UpdateData=""
    End Sub

在form2中


    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        form1.UpdateData = TextBox2.Text
        Me.Hide()
    End Sub