string strmode;private void btnClose_Click(object sender, EventArgs e) { if (strmode == "strnew") { frmMain frm = new frmMain(); frm.PerformRefresh(); }} 推荐答案 你必须使用你的frmForm类的当前对象不要创建一个新的,所以为了在两个表单对象之间进行通信,你应该做下面的步骤: 1.你的主表单 fromMain 类必须像下一个一样实现接口: You have to use the current object of your frmForm class and not to create a new one, so in order to communicate between two forms objects you should do the next steps:1.Your main form fromMain class have to implement an interface like the next one:public interface IMyForm{ void PerformRefresh();}class frmMain : Form, IMyForm{//...} 2.然后在Form2类添加IMyForm类型的属性并在Close事件中使用它: 2.Then in the Form2 class add a property of type IMyForm and use it in Close event:class Form2 : Form{ public IMyForm MainForm{get;set;}//...private void btnClose_Click(object sender, EventArgs e){ if (strmode == "strnew") { this.MainForm.PerformRefresh(); }}//...} 3.在创建 Form2 对象时,最后在主窗体中,必须将其新属性设置为下面: 3.Finally in your main form when the Form2 object is created you must set its new property like below:private void btnNew_Click(object sender, EventArgs e){Form2 frm = new Form2();frm.MainForm = this; //This is line is new!frm.Show();} 将这些全局变量和属性添加到form1 add these global variable and property to form1string strmode = string.Empty; public string Strmode { get { return strmode; } set { strmode = value; } } 删除您在上面的帖子中发布的代码,然后点击关闭 remove code u posted in ur question above in close clickprivate void btnClose_Click(object sender, EventArgs e) { ///from ur previous code " if (strmode == "strnew")" i am assuming strmode has value "strnew" .no extra code needed here } 将这些全局变量和属性添加到form2 add these global variable and property to form2string strmode = string.Empty; public string Strmode { get { return strmode; } set { strmode = value; } } 替换btn新点击代码到这个 replace btnNew click code to thisprivate void btnNew_Click(object sender, EventArgs e) { Form2 frm = new Form2(); frm.Show(); strmode = frm.Strmode; if (strmode == "strnew") { PerformRefresh(); } } 这篇关于在表单中刷新用户控件:1表单后:winforms中的2个结束事件C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
09-17 09:15