从第二个Windows窗体获取值
从主窗口窗体打开第二个窗体,我可以写入2个值。 我需要在主窗体中的这2个值。从第二个Windows窗体获取值
此刻,我打开使用
NuovoForm.show()
其中NuovoForm是第二种形式的名字第二种形式。第二种形式有两个文本字段和一个按钮,当按钮被按下时,我如何获得第一种形式写入2个字段内的文本?
假设第1形式的名称是“Form1
”和“NuovoForm
”上的控制是“TextBox1
”,“TextBox2
”和“Button1
”
您可以使用下面的代码:
Form 1代码:
Public Class Form1
Public Value1 As String, value2 As String
'
'
'
End Class
NuovoForm代码:
Public Class NuovoForm
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
Form1.Value1 = TextBox1.Text
Form1.value2 = TextBox2.Text
Me.Hide()
Form1.Show()
End Sub
End Class
现在,您可以使用值1和值2做任何你想在Form1
确实,很高兴有人救我发帖...... 这是OP寻找的唯一答案。 – Arazio
只需在NuovoForm窗体中创建两个属性并从NuovoForm的文本框中设置这些属性。通过这些新属性以主要形式获取这些值。
一个行为是将子窗体的所有者设置给调用者,在这种情况下是您的主窗体。因此,在子表单中,我们将使用类似这样的方式:在主窗体中更新主窗体的TextBox1和两个公共属性。
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
If Me.Owner IsNot Nothing Then
Dim MainForm As Form1 = CType(Me.Owner, Form1)
MainForm.TextBox1.Text = Me.TextBox1.Text
MainForm.Item1 = 1
MainForm.Item2 = "Hello, made this change in child form"
Else
Console.WriteLine("Owner not set")
End If
End Sub
In the main form we call the child form
mChildForm = New ChildForm
mChildForm.Owner = Me
mChildForm.Show()
I have a complete `demonstration project` on Microsoft OneDrive or see code below
Main form
Public Class Form1
Private mChildForm As ChildForm
Private mFirstTime As Boolean = True
Public Property Item1 As Integer
Public Property Item2 As String
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
DoFormWork()
End Sub
Private Sub DoFormWork()
If Not ((From f In My.Application.OpenForms.Cast(Of Form)() Where f.Name.Equals("ChildForm") Select f.Name).ToList.Count > 0) Then
mChildForm = New ChildForm
mChildForm.Owner = Me
mFirstTime = True
End If
mChildForm.Show()
If mFirstTime Then
mChildForm.Location = New Point(Me.Left + Me.Width, Top)
mFirstTime = False
End If
If chkPushText.Checked Then
mChildForm.TextBox1.Text = Me.TextBox1.Text
End If
End Sub
Private Sub Form1_FormClosing(sender As Object, e As FormClosingEventArgs) Handles Me.FormClosing
If mChildForm IsNot Nothing Then
mChildForm.Dispose()
Else
Console.WriteLine("mChildForm is Nothing")
End If
End Sub
Private Sub Form1_Shown(sender As Object, e As EventArgs) Handles Me.Shown
Controls.OfType(Of Control).ToList.ForEach(
Sub(x)
If TypeOf x Is TextBox Then
AddHandler x.Click,
Sub(s As System.Object, a As System.EventArgs)
Dim tb As TextBox = CType(x, TextBox)
If Not String.IsNullOrEmpty(tb.Text) Then
DoFormWork()
mChildForm.TextBox1.Text = tb.Text
End If
End Sub
End If
End Sub)
mChildForm = New ChildForm
mChildForm.Owner = Me
mChildForm.Show()
mChildForm.Location = New Point(Me.Left + Me.Width, Top)
mFirstTime = False
My.Application.OpenForms(0).Activate()
DoFormWork()
End Sub
Private Sub Form1_LocationChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.LocationChanged
If Not mFirstTime Then
mChildForm.Location = New Point(Me.Left + Me.Width, Top)
End If
End Sub
End Class
子窗体
Public Class ChildForm
Public Sub New()
InitializeComponent()
End Sub
Private Sub cmdClose_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdClose.Click
Close()
End Sub
Private Sub cmdHideMe_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdHideMe.Click
Hide()
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
If Me.Owner IsNot Nothing Then
Dim MainForm As Form1 = CType(Me.Owner, Form1)
MainForm.TextBox1.Text = Me.TextBox1.Text
MainForm.Item1 = 1
MainForm.Item2 = "Hello, made this change in child form"
Else
Console.WriteLine("Owner not set")
End If
End Sub
End Class
注意,这里的代码一个很好的协议,它其他的事情除了你问什么,因为它的东西,几年前我做了,但是,因为它表明相关如何与父母对孩子形式进行互动。现在,如果我们在C#中做了这些事情,还需要发生一些事情,所以现在这是一个vb.net的事情,而任何人都想在C#中使用它,我也可以这样做。
不管怎么说希望这有助于
另一种选择是,以显示你的孩子形式的对话,例如:
Dim frmDia As New NuovoForm
frmDia.TopMost = True
frmDia.StartPosition = FormStartPosition.CenterScreen
If frmDia.ShowDialog = Windows.Forms.DialogResult.OK Then
' Get data here
End If
在你的孩子的形式,通过将下面的行关闭表格“关闭”按钮:
DialogResult = Windows.Forms.DialogResult.OK
一旦形式与封闭的‘OK’对话的结果将进入循环。如果在那里你可以用公共属性提取数据。这也将使您对数据的检索时间有很多控制权,例如在表单过早关闭时不检索数据。
Srsly,有数以百计的教程在那里 - [这里](http://www.codeproject.com/Articles/12214 /传递值之间的表单在NET-X与C和),[这里](http:// *。COM /问题/ 4587952 /传递数据之间-形式)等。只是谷歌它! – Eminem