我想在新线程上打开一个现有表单,其中包含自己的值(在文本字段中等)
这是我的代码:
Dim NewForm As New Form2
NewForm.Textbox1.text = "This is a test"
NewForm.Textbox2.text = "This is the other field"
NewForm.Show()
如何在单独的帖子中打开NewForm
?
答案 0 :(得分:0)
这只是一个示例代码,如果您已经运行了一个表单,该如何操作
在主窗体构造函数
中 // Mark existing thread
Thread.CurrentThread.Name = "First";
this.Click += delegate(object a, EventArgs b) { MessageBox.Show(Thread.CurrentThread.Name); };
// Start new thread
ThreadStart ts = new ThreadStart(NewThread);
Thread t = new Thread(ts);
t.Name = "Second";
t.Start();
VB
' Mark existing thread
Thread.CurrentThread.Name = "First"
AddHandler me.Click, Sub(a As Object, b As EventArgs) MessageBox.Show(Thread.CurrentThread.Name)
' Start new thread
Dim ts As New ThreadStart(NewThread)
Dim t As New Thread(ts)
t.Name = "Second"
t.Start()
现在,打开表单的方法
private void NewThresd()
{
Form f = new Form();
f.Text = "dfsdfsdfsdfsd";
f.Click += delegate(object a, EventArgs b) { MessageBox.Show(Thread.CurrentThread.Name); };
f.ShowDialog();
}
VB:
Private Sub NewThresd()
Dim f As New Form()
f.Text = "dfsdfsdfsdfsd"
AddHandler f.Click, Sub(a As Object, b As EventArgs) MessageBox.Show(Thread.CurrentThread.Name)
f.ShowDialog()
End Sub
这将是什么,它将启动一个线程并在其中打开一个表单。当您单击表单时,它将显示该线程的名称,以证明您在不同的线程上运行表单。
请记住,这不是生产就绪的代码。标记线程并连接点击事件以显示概念。另外,例如,这不好,因为这里会有内存泄漏
f.Click += delegate(object a, EventArgs b) { MessageBox.Show(Thread.CurrentThread.Name); };
你应该在班级
private EventHandler _hndlr = delegate(object a, EventArgs b) { MessageBox.Show(Thread.CurrentThread.Name); };
VB
Private _h As EventHandler = Sub(a As Object, b As EventArgs) MessageBox.Show(Thread.CurrentThread.Name)
然后你连接处理程序
f.Click += _hndlr;
VB
AddHandler f.Click, _hndlr
然后,关闭表单时 - unwire
f.Click -= _hndlr;
VB
RemoveHandler f.Click, _hndlr
答案 1 :(得分:0)
这可以使用Application.run()完成,例如:
Dim NewForm As Form2 = New Form2()
NewForm.Textbox1.text = "This is a test"
NewForm.Textbox2.text = "This is the other field"
Application.Run(NewForm)