在Windows窗体之间传递 [英] Passing Between windows forms
本文介绍了在Windows窗体之间传递的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在form1上有一个文本框和一个按钮.
加载表单并单击按钮form2时,该表单上有另一个文本框和按钮.
当我在文本框中键入一些文本并单击按钮时,文本不会显示在form1文本框中.
我正在使用属性方法.
请帮助
Hi,
I have a textbox and a button on a form1.
When the form loads and you click the button form2 loads which has another textbox and button on that form.
When I type some text in the textbox and click the button the text does not display on the form1 textbox.
I am using property approach.
Please help
推荐答案
您需要在处理form2之前检索form2 TextBox控件的内容.为此,您必须处理form1内form2的Closing事件.在该事件处理程序中,您只需使用属性即可检索Form2.TextBox的内容,并对其进行所需的任何操作.
You need to retrieve the contents of the form2 TextBox control before form2 is disposed. To do that, you have to handle the Closing event for form2 inside form1. In that event handler, you simply use your property to retrieve the contents of the Form2.TextBox, and do whatever you need to with it.
也许这会有所帮助
Maybe this would help a little
<pre lang="vb">
Public Class Form1
Dim frm2 As Form2
Private Sub btnShowForm2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnShowForm2.Click
InitForm()
End Sub
Private Sub InitForm()
If frm2 Is Nothing OrElse frm2.IsDisposed = True Then
frm2 = New Form2
End If
If frm2.Visible = False Then
frm2.Show()
End If
End Sub
Private Sub btnSayHello_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSayHello.Click
InitForm()
frm2.SayHello(txtName.Text)
End Sub
Private Sub btnSetValue_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSetValue.Click
InitForm()
frm2.SampelValue = txtValue.Text
End Sub
Private Sub btnGetValue_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnGetValue.Click
InitForm()
txtValue.Text = frm2.SampelValue
End Sub
End Class
<pre lang="vb">Public Class Form2
Public Sub SayHello(ByVal name As String)
lblHallo.Text = "Hello " & name
End Sub
Public Property SampelValue() As String
Get
Return txtValue.Text
End Get
Set(ByVal value As String)
txtValue.Text = value
End Set
End Property
End Class
这是我前一段时间拿到的东西.希望对您有帮助
问候Zeldacat
This is something i picked up a while ago. I hope it helps
regards Zeldacat
我的代码是这样的:
Hi my code is this:
Public Class Form1
Public _form2 As Form2 = New Form2
Private Sub btnList_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnList.Click
_form2.Show()
End Sub
Public WriteOnly Property _textBox() As String
Set(ByVal Value As String)
tbxVal.Text = Value
End Set
End Property
结束类
End Class
Public Class Form2
Public _form1 As Form1
Private Sub btnOK_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOK.Click
Dim frm As Form1 = New Form1
'frm._textBox = _textBox1
Value = _textBox1
'frm.Close()
Me.Close()
'frm.Show()
'_form1 = New Form1
'With _form1
' .tbxVal.Text = _textBox1
' Me.Close()
'End With
End Sub
Public ReadOnly Property _textBox1() As String
Get
Return tbxValue1.Text
End Get
End Property
Private Sub Form2_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
Dim fm As Form1 = New Form1
fm.tbxVal.Text = Value
End Sub
End Class
这篇关于在Windows窗体之间传递的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文