如何将值从一个表单传递到另一个表单的动态创建的控件 [英] How to pass value from one form to another form's dynamically created control

查看:23
本文介绍了如何将值从一个表单传递到另一个表单的动态创建的控件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有两个表单,即 Form1Form2.Form1 包含动态创建的 DataGridView 控件,Form2 包含两个 Textbox 控件(Textbox1Textbox2) 和一个 Button 控件.

当我 DoubleClickDataGridView 的单元格上时,它将打开 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()结束子

当我单击 Form2 中的按钮时,当前选定单元格的值会像 Form2 中的 TextBox2 一样发生变化.但问题是我不能使用来自 From2 的 button1 中的代码,例如

<前>Form1.dgView.CurrentCell.Value = TextBox2.Text

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

解决方案

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

的公共变量<前>公共 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 = 更新数据更新数据=""结束子

在form2中

<前>Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) 处理Button1.Clickform1.UpdateData = TextBox2.Text我.隐藏()结束子

Suppose I have two forms i.e Form1 and Form2. Form1 contains dynamically created DataGridView control and Form2 contains two Textbox controls (Textbox1 and Textbox2) and a Button control.

When I DoubleClick on the DataGridView's cell it will open Form2 and the data from current selected cell passes to Form2's Textbox1

Here is the code:

I Added a handlear to my dynamically created DatagridView like this:


    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

When i click on the button from Form2, the value of current selected cell will change like TextBox2 from Form2 has. But the problem is i can't use the code from button1 from From2 like


    Form1.dgView.CurrentCell.Value = TextBox2.Text

How can i pass value from textbox2 to current selected cell?

解决方案

Ok I got it. At first i have created a public variable called


    Public UpdateData As String = ""

Then change the code-

    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

In 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

这篇关于如何将值从一个表单传递到另一个表单的动态创建的控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆