首页 文章

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

提问于
浏览
0

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

当我在 DataGridView 的单元格上 DoubleClick 时,它将打开 Form2 并且当前所选单元格中的数据传递给 Form2Textbox1

这是代码:

我为动态创建的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

    创建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
    
  • 0

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

    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上的文本框中 .

  • 0

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

    System.Windows.Forms.Clipboard.SetText(...)
    
    System.Windows.Forms.Clipboard.GetText(...)
    
  • 1

    好,我知道了 . 起初我创建了一个名为的公共变量

    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
    
  • 1

    您可以将 Form1 的datagridview传递给 Form2 ,通过 Form2 's constructor. Then you' ll能够访问 Form1 的datagridview的 CurrentCell ,就像您的上一个代码段一样 .

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

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

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

    Public Class Form1
        Dim Form2 As New Form2(dgView)
    
    End Class
    

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

    dgView.CurrentCell.Value = TextBox2.Text
    

相关问题