300字范文,内容丰富有趣,生活中的好帮手!
300字范文 > c# winform 用子窗体刷新父窗体 子窗体改变父窗体控件的值

c# winform 用子窗体刷新父窗体 子窗体改变父窗体控件的值

时间:2019-06-01 00:53:20

相关推荐

c# winform 用子窗体刷新父窗体 子窗体改变父窗体控件的值

第一种方法:

用委托,Form2和Form3是同一组

Form2

usingSystem;

usingSystem.Collections.Generic;

ponentModel;

usingSystem.Data;

usingSystem.Drawing;

usingSystem.Text;

usingSystem.Windows.Forms;

namespaceTestMouseMove

{

publicdelegatevoidSetVisiableHandler();

publicpartialclassForm2:Form

{

publicForm2()

{

InitializeComponent();

}

privatevoidbutton1_Click(objectsender,EventArgse)

{

Form3frm=newForm3(newSetVisiableHandler(SetVisiable));

frm.Show();

}

privatevoidSetVisiable()

{

SetVisiable(this.label1,!this.label1.Visible);

}

privatevoidSetVisiable(Controlcontrol,boolvisiable)

{

if(this.Controls.Contains(control))

{

control.Visible=visiable;

}

}

}

}

Form3

usingSystem;

usingSystem.Collections.Generic;

ponentModel;

usingSystem.Data;

usingSystem.Drawing;

usingSystem.Text;

usingSystem.Windows.Forms;

namespaceTestMouseMove

{

publicpartialclassForm3:Form

{

privateSetVisiableHandlerm_setVisible;

publicForm3(SetVisiableHandlersetvisible)

{

InitializeComponent();

this.m_setVisible=setvisible;

}

privatevoidbtnVisible_Click(objectsender,EventArgse)

{

if(this.m_setVisible!=null)

{

this.m_setVisible();

}

}

}

}

第二种方法:

用变量,Form4和Form5是同一组

Form4

usingSystem;

usingSystem.Collections.Generic;

ponentModel;

usingSystem.Data;

usingSystem.Drawing;

usingSystem.Text;

usingSystem.Windows.Forms;

namespaceTestMouseMove

{

publicpartialclassForm4:Form

{

publicForm4()

{

InitializeComponent();

}

#region子窗口刷新父窗口的值

privatestringstrLabel1="";

publicstringStrLabel1

{

get

{

returnstrLabel1;

}

set

{

strLabel1=value;

this.label1.Text=strLabel1;

}

}

#endregion

privatevoidbutton1_Click(objectsender,EventArgse)

{

Form5form5=newForm5(this);//这里注意传个this

form5.Show();

}

}

}

usingSystem;

usingSystem.Collections.Generic;

ponentModel;

usingSystem.Data;

usingSystem.Drawing;

usingSystem.Text;

usingSystem.Windows.Forms;

namespaceTestMouseMove

{

publicpartialclassForm4:Form

{

publicForm4()

{

InitializeComponent();

}

#region子窗口刷新父窗口的值

privatestringstrLabel1="";

publicstringStrLabel1

{

get

{

returnstrLabel1;

}

set

{

strLabel1=value;

this.label1.Text=strLabel1;

}

}

#endregion

privatevoidbutton1_Click(objectsender,EventArgse)

{

Form5form5=newForm5(this);//这里注意传个this

form5.Show();

}

}

}

Form5

usingSystem;

usingSystem.Collections.Generic;

ponentModel;

usingSystem.Data;

usingSystem.Drawing;

usingSystem.Text;

usingSystem.Windows.Forms;

namespaceTestMouseMove

{

publicpartialclassForm5:Form

{

Form4form4=newForm4();

publicForm5(Form4formFrm)//这个构造方法里有参数

{

form4=formFrm;//这个必须要有

InitializeComponent();

}

privatevoidbutton1_Click(objectsender,EventArgse)

{

form4.StrLabel1=this.textBox1.Text;

}

}

}

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。