vb.net在自制的窗体控件中弹出一个窗口再返回一个值
例如:正在自制一个控件A,在A中有一个按钮BUTTON1和变量X.按下BUTTON1就会弹出一个已经制作好的WINDOS窗体FROM2,在FROM2中有textbox1,在关闭FORM2时,将textbox1中的值赋给控件A中的变量X
先设计好Form1和Form2。
在Private Class Form2中输入 Public Event ChangeValue(ByVal value As String) '是string还是其它的看你需要了
在Form2的Closed事件:
Private Sub Form2_Closed() Handles Me.Closed RaiseEvent ChangeValue(TextBox1.Text)'如果是数值类型的话用下面这个: 'RaiseEvent ChangeValue(Val(TextBox1.Text)) End Sub
在Form中定义x:
Dim x As String '是String还是数值类型看你需要了,要和上面的统一
在Form1中写一个处理值的函数。值哪来?别管它
Private Sub ProcessValue(ByVal value As String) '记得统一 x = value End Sub
在Form1中Button1的Click事件中:
Private Sub Button1_Click() Handles Button1.Click Dim f2 As New Form2 AddHandler f2.ChangeValue,AddressOf ProcessValue f2.Show() End Sub
https://zhidao.baidu.com/question/553583369.html
ーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーー
WinForm构造函数传值
http://blog.csdn.net/zbssoft/article/details/5586306
在WinForms程序里实现窗体传值的最佳实践
http://bbs.csdn.net/topics/360140208
windows form (窗体) 之间传值小结
http://www.cnblogs.com/JoshuaDreaming/archive/2010/11/17/1880060.html
C# form 传参数
http://www.cnblogs.com/Asa-Zhu/archive/2012/12/06/2805074.html
========================================
'自己的代码
'我的例子
'弹出的子窗口 Public Class ChildForm Dim temp As String = "" Public Event returnValue(ByVal value As String) Public Sub New(ByVal Cd) InitializeComponent() temp = Cd End Sub Private Sub ChildForm_Load(sender As Object, e As EventArgs) Handles MyBase.Load txtAAA.Text = temp '父窗口传过来的值获取到 End Sub Private Sub F12_Click(sender As Object, e As EventArgs) Handles F12.Click RaiseEvent returnValue("传回父窗口的值") Me.Close() End Sub End Class
'父窗口 Public Class ParentForm Private Sub F2_Click(sender As Object, e As EventArgs) Handles F2.Click Dim childForm = New ChildForm("传给弹出子窗口的值") childForm.Show() AddHandler childForm.returnValue, AddressOf ProcessReturnValue End Sub Private Sub ProcessReturnValue(ByVal value As String) txtTest.Text = value '子窗口传回的值获取到 End Sub End Class