2014-02-27 134 views
0

假设我有两种形式,即Form1Form2。 Form1中包含动态创建DataGridView控制和Form2包含两个Textbox控件(Textbox1Textbox2)Button控制。如何将值从一种形式传递给另一种形式的动态创建的控件

当我DoubleClickDataGridView的细胞它将打开Form2和从当前选择的单元中的数据传递到Form2Textbox1

下面是代码:

我添加了一个handlear到我的动态创建的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 

当我点击从窗体2的按钮,当前选择的单元格的值将改变像TextBox2中从窗体2具有。但问题是我不能使用从From2 button1的代码,如

 

    Form1.dgView.CurrentCell.Value = TextBox2.Text 

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

回答

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 

在窗口2

 

    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 

0

您可以通过Form2的构造函数将Form1的datagridview传递给Form2。然后,您可以像访问最后一段代码片段一样访问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 

当您创建的Form2Form1的情况下,通过dgView到窗体2,如:

Public Class Form1 
    Dim Form2 As New Form2(dgView) 

End Class 

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

dgView.CurrentCell.Value = TextBox2.Text 
+0

你能解释一些代码吗? –

+0

我已经更新了我的答案。谢谢! –

0

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

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

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

我认为这可以是一个解决方案。但是,点击Button form2后隐藏,但如何从剪贴板中获取文本并设置为当前单元格? –

+0

一旦数据在剪贴板中,然后像'Form1.dgView.CurrentCell.Value = System.Windows.Forms.Clipboard.GetText(System.Windows.Forms.TextDataFormat.Text)' – TylerDurden

+1

检索它一个糟糕的解决方案国际海事组织 - 有无需为此使用剪贴板 –

1

当您创建的DataGridView,存储对它的引用:

private _myDgv as DataGridView 

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

然后添加一个只读属性,从其他地方得到它的引用:

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

然后你可以在Form2中做到这一点:

Form1.DynamicDgv.CurrentCell.Value = TextBox2.Text 
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 

Form 2上以文本框的任何变化将反映在Form1中的文本框。

+0

该问题与默认表单实例无关(并且实际上,您不能解释为什么您会推荐使用这些表单) –

+0

如果您使用表单的默认实例,那么对该表单的任何更改都会将其更改为应用程序。假设他的表单有10个字段作为文本框,并且用户将其填入。下一次显示表单时,这些文本框将具有相同的数据而不是空白。我不会使用默认实例,因为其他许多原因,比它值得的更麻烦。 – dbasnett

+0

这是“正确”方式的一个很好的例子:http://jmcilhinney.blogspot.com.au/2013/10/managing-data-among-multiple-forms-part.html – dbasnett

相关问题