问题描述
假设我有两个表单,即 Form1
和 Form2
.Form1 包含动态创建的 DataGridView
控件,Form2
包含两个 Textbox
控件(Textbox1
和 Textbox2)
和一个 Button
控件.
当我 DoubleClick
在 DataGridView
的单元格上时,它将打开 Form2
并且当前选定单元格的数据传递到 Form2
的 Textbox1
代码如下:
我向动态创建的 DatagridView 添加了一个句柄,如下所示:
AddHandler dg.CellMouseDoubleClick, AddressOf dg_DataGridEditPrivate 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
这篇关于如何将值从一个表单传递到另一个表单的动态创建的控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!