本文介绍了WinForms从另一个窗体引发事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个主窗体命名:MainForm和一个名为:ChildForm的子窗体我想填充ChildForm的文本框,在MainForm_ButtonClick我想要启动ChildForm_ButtonClick事件。
ChildForm:
public partial class ChildForm:Form
{
public delegate void delPassData文本);
private void button1_Click(object sender,EventArgs e)
{
string depart =;
MainForm mfrm = new MainForm();
delPassData del = new delPassData(frm.funData);
del(this.Item_CodeTextEdit);
}
}
MainForm:
public partial class MainForm:Form
{
public void funData(TextEdit txtForm1)
{
string ss =;
ss = txtForm1.Text;
MessageBox.Show(ss);
}
private void NavigationPanelBtns_ButtonClick(object sender,ButtonEventArgs e)
{
switch(e.Button.Properties.Caption)
{
caseSave:
//我想在这里调用funData(),但是我收到一个空的messageBox
break;
}
}
}
解决方案
子表单
public partial class ChildForm:Form
{
public ChildForm()
{
InitializeComponent();
MainForm.OnChildTextChanged + = MainForm_OnChildTextChanged;
MainForm.OnButtonClick + = MainForm_OnButtonClick;
bttn1.Visible = false;
}
void MainForm_OnButtonClick(object sender,EventArgs e)
{
this.bttn1.PerformClick();
}
void MainForm_OnChildTextChanged(string value)
{
this.textBox1.Text = value;
}
private void bttn1_Click(object sender,EventArgs e)
{
MessageBox.Show(我是隐藏但显示消息);
}
}
public class Bttn:Button
{
public new void PerformClick()
{
this.OnClick(EventArgs.Empty);
}
}
创建家长表单
public partial class MainForm:Form
{
public delegate void OnMyTextChanged(string value);
public delegate void ButtonClicked(object sender,EventArgs e);
public static event OnMyTextChanged OnChildTextChanged;
public static event ButtonClicked OnButtonClick;
ChildForm frm = new ChildForm();
public MainForm()
{
InitializeComponent();
frm.Show();
}
public void button1_Click(object sender,EventArgs e)
{
OnChildTextChanged(this is new value);
OnButtonClick(sender,e);
}
}
I have a main form named: MainForm and a child form named: ChildFormI want to fill ChildForm's textboxes and in MainForm_ButtonClick i want to fire ChildForm_ButtonClick event.
ChildForm:
public partial class ChildForm :Form
{
public delegate void delPassData(TextEdit text);
private void button1_Click(object sender, EventArgs e)
{
string depart = "";
MainForm mfrm = new MainForm();
delPassData del = new delPassData(frm.funData);
del(this.Item_CodeTextEdit);
}
}
MainForm:
public partial class MainForm : Form
{
public void funData(TextEdit txtForm1)
{
string ss = "";
ss = txtForm1.Text;
MessageBox.Show(ss);
}
private void NavigationPanelBtns_ButtonClick(object sender, ButtonEventArgs e)
{
switch (e.Button.Properties.Caption)
{
case "Save":
// i want to call funData() here but i get an empty messageBox
break;
}
}
}
解决方案
Child form
public partial class ChildForm : Form
{
public ChildForm()
{
InitializeComponent();
MainForm.OnChildTextChanged += MainForm_OnChildTextChanged;
MainForm.OnButtonClick += MainForm_OnButtonClick;
bttn1.Visible = false;
}
void MainForm_OnButtonClick(object sender, EventArgs e)
{
this.bttn1.PerformClick();
}
void MainForm_OnChildTextChanged(string value)
{
this.textBox1.Text = value;
}
private void bttn1_Click(object sender, EventArgs e)
{
MessageBox.Show("I am hide. But shows message");
}
}
public class Bttn : Button
{
public new void PerformClick()
{
this.OnClick(EventArgs.Empty);
}
}
Create a Parent Form
public partial class MainForm : Form
{
public delegate void OnMyTextChanged(string value);
public delegate void ButtonClicked(object sender, EventArgs e);
public static event OnMyTextChanged OnChildTextChanged;
public static event ButtonClicked OnButtonClick;
ChildForm frm = new ChildForm();
public MainForm()
{
InitializeComponent();
frm.Show();
}
public void button1_Click(object sender, EventArgs e)
{
OnChildTextChanged("this is new value");
OnButtonClick(sender, e);
}
}
这篇关于WinForms从另一个窗体引发事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!