C#子窗體給父窗體傳值的幾種方法
一癞谒、我們舉個(gè)小例子啊掏,分別用幾種不同的方法來(lái)實(shí)現(xiàn)梗搅。比如:我們現(xiàn)在有教師禾唁、學(xué)生兩個(gè)類(lèi),教師類(lèi)是學(xué)生類(lèi)的父類(lèi)无切,也就是教師類(lèi)中可以實(shí)例化出來(lái)多個(gè)不同的學(xué)生類(lèi)〉炊蹋現(xiàn)在,學(xué)生有兩個(gè)功能哆键, ①掘托、在班級(jí)向大家自我介紹,讓其它同學(xué)們認(rèn)識(shí)他籍嘹, ②闪盔、向老師索要自己的學(xué)習(xí)成績(jī)和排名。教師的職責(zé)有三個(gè):①辱士、可以實(shí)例化出不同的學(xué)生來(lái) ②泪掀、可以命令學(xué)生在班級(jí)進(jìn)行自我介紹 ③、對(duì)于學(xué)生提出查看學(xué)習(xí)成績(jī)的需求颂碘,給出答復(fù)异赫。
1、第一種方法:利用委托和事件
對(duì)于大型的應(yīng)用程序头岔,都會(huì)利用面向?qū)ο蟮倪壿媮?lái)實(shí)現(xiàn)的塔拳,對(duì)于定義的委托和事件,我們可以寫(xiě)到一個(gè)專用的類(lèi)中切油,后面新類(lèi)可以繼承這個(gè)專用類(lèi)蝙斜。方便的利用定義好的委托和事件。
①澎胡、定義好專門(mén)的類(lèi)(AllMethodsClass)用來(lái)盛放窗體通信的方法孕荠。
namespace ConsoleApp1.Methods
{
? ? public class AllMethodsClass
? ? {
? ? ? ? public delegate int GetMyScore(string studentName);//定義好的委托
? ? }
}
②、定義教師類(lèi)
namespace ConsoleApp1.Methods
{
? ? public class TeacherClass: AllMethodsClass
? ? {
? ? ? ? public Dictionary<string, int> studentInfos = new Dictionary<string, int>();//存放學(xué)生的成績(jī)信息
? ??????StudentClass student1;
? ? ? ? StudentClass student2;
? ? ? ? public TeacherClass()
? ? ? ? {
? ? ? ? ? ? //構(gòu)造函數(shù)初始化成績(jī)表
? ? ? ? ? ? studentInfos.Add("LiMing", 98);
? ? ? ? ? ? studentInfos.Add("XiaoHong", 97);
? ? ? ? }
? ??????//實(shí)例化新學(xué)生的方法
? ? ? ? private void CreateNewStudent()
? ? ? ? {
? ? ? ? ? ? student1 = new StudentClass("LiMing");//實(shí)例化名字為L(zhǎng)iMing的學(xué)生
? ? ? ? ? ? student1.getMyScore += GetStudentScore;//對(duì)學(xué)生類(lèi)中的事件getMyScore綁定相應(yīng)的方法
? ? ? ? ? ? student2 = new StudentClass("XiaoHong");
? ? ? ? ? ? student2.getMyScore += GetStudentScore;
? ? ? ? }
? ??????//讓學(xué)生介紹自己
? ??????private void IntroduceStudent()
? ? ? ? {
? ? ? ? ? ? student1.Introduce();//直接通過(guò)相應(yīng)子類(lèi)調(diào)用即可
? ? ? ? }
? ??????//得到學(xué)生的成績(jī)并返回攻谁,如果成績(jī)表沒(méi)有則返回0
? ? ? ? private int GetStudentScore(string name)
? ? ? ? {
? ? ? ? ? ? if (studentInfos.ContainsKey(name))
? ? ? ? ? ? {
? ? ? ? ? ? ? ? return studentInfos[name];
? ? ? ? ? ? }
? ? ? ? ? ? return 0;
? ? ? ? }
? ? }
}
③稚伍、定義學(xué)生類(lèi)
namespace ConsoleApp1.Methods
{
? ? public class StudentClass: AllMethodsClass
? ? {
? ? ? ? public event GetMyScore getMyScore;//定義得到學(xué)生成績(jī)的事件
? ? ? ? private string _myName;//用來(lái)存儲(chǔ)個(gè)人姓名
? ? ? ? public StudentClass(string name)
? ? ? ? {
? ? ? ? ? ? _myName = name;
? ? ? ? }
? ??????//介紹自己的方法
? ??????public void Introduce()
? ? ? ? {
? ? ? ? ? ? string str = string.Format("我的名字叫:{0}", _myName);
? ? ? ? ? ? Console.WriteLine(str);
? ? ? ? }
? ??????//獲得成績(jī)方法
? ? ? ? private void GetScore()
? ? ? ? {
? ? ? ? ? ? int score = getMyScore.Invoke(_myName);//在此得到個(gè)人成績(jī),并保存在變量score中
? ? ? ? }
? ? }
}
2戚宦、父類(lèi)中定義靜態(tài)方法个曙,讓子類(lèi)直接調(diào)用
①、定義教師類(lèi)
namespace ConsoleApp1.Methods
{
? ? public class TeacherClass
? ? {
? ? ? ? public static Dictionary<string, int> studentInfos = new Dictionary<string, int>();//存放學(xué)生的成績(jī)信息
? ? ? ? StudentClass student1;
? ? ? ? StudentClass student2;
? ? ? ? public TeacherClass()
? ? ? ? {
? ? ? ? ? ? //構(gòu)造函數(shù)初始化成績(jī)表
? ? ? ? ? ? studentInfos.Add("LiMing", 98);
? ? ? ? ? ? studentInfos.Add("XiaoHong", 97);
? ? ? ? }
? ??????//實(shí)例化新學(xué)生的方法
? ? ? ? public void CreateNewStudent()
? ? ? ? {
? ? ? ? ? ? student1 = new StudentClass("LiMing");
? ? ? ? ? ? student2 = new StudentClass("XiaoHong");
? ? ? ? }
? ??????//讓學(xué)生介紹自己
? ? ? ? private void IntroduceStudent()
? ? ? ? {
? ? ? ? ? ? student1.Introduce();
? ? ? ? }
? ??????//得到學(xué)生的成績(jī)并返回,如果成績(jī)表沒(méi)有則返回0
? ? ? ? public static int GetStudentScore(string name)
? ? ? ? {
? ? ? ? ? ? if (studentInfos.ContainsKey(name))
? ? ? ? ? ? {
? ? ? ? ? ? ? ? return studentInfos[name];
? ? ? ? ? ? }
? ? ? ? ? ? return 0;
? ? ? ? }
? ? }
}
②垦搬、定義學(xué)生類(lèi)
namespace ConsoleApp1.Methods
{
? ? public class StudentClass
? ? {
? ? ? ? private string _myName;
? ? ? ? public StudentClass(string name)
? ? ? ? {
? ? ? ? ? ? _myName = name;
? ? ? ? }
? ??????//介紹自己的方法
? ? ? ? public void Introduce()
? ? ? ? {
? ? ? ? ? ? string str = string.Format("我的名字叫:{0}", _myName);
? ? ? ? ? ? Console.WriteLine(str);
? ? ? ? }
? ? ? ? private void GetScore()
? ? ? ? {
? ? ? ? ? ? int score = TeacherClass.GetStudentScore(_myName);//在此直接得到個(gè)人成績(jī)
? ? ? ? }
? ? }
}
3呼寸、還有一種利用事件子窗體調(diào)用父窗體方法,但是此方法不能有返回值猴贰。在我不知道第一種方法之前对雪,我一直使用的第二種方法,跟傳值的有關(guān)的地方都得寫(xiě)成靜態(tài)的米绕,即使是控件也是自己實(shí)例化為靜態(tài)的瑟捣,在實(shí)際應(yīng)用起來(lái)相當(dāng)?shù)穆闊胰菀壮鰡?wèn)題栅干。第一種方法是安全高效的迈套,非常完美,后來(lái)我一直使用第一種方法碱鳞。
第三種方法就不能有返回值了桑李,只能子窗體調(diào)用父窗體的方法。現(xiàn)在需要稍微改動(dòng)一下題目規(guī)則窿给,就是學(xué)生想要知道自己的成績(jī)的話芙扎,需要告訴老師,然后只能讓老師有說(shuō)出來(lái)學(xué)生成績(jī)的動(dòng)作填大。
①戒洼、定義教師類(lèi)
namespace ConsoleApp1.Methods
{
? ? public class TeacherClass
? ? {
? ? ? ? public Dictionary<string, int> studentInfos = new Dictionary<string, int>();//存放學(xué)生的成績(jī)信息
? ? ? ? StudentClass student1;
? ? ? ? StudentClass student2;
? ? ? ? public TeacherClass()
? ? ? ? {
? ? ? ? ? ? //構(gòu)造函數(shù)初始化成績(jī)表
? ? ? ? ? ? studentInfos.Add("LiMing", 98);
? ? ? ? ? ? studentInfos.Add("XiaoHong", 97);
? ? ? ? }
? ? ? ? public void CreateNewStudent()
? ? ? ? {
? ? ? ? ? ? student1 = new StudentClass("LiMing");
? ? ? ? ? ? student1.GetMyScore += GetStudentScore;
? ? ? ? }
? ? ? ? private void IntroduceStudent()
? ? ? ? {
? ? ? ? ? ? student1.Introduce();
? ? ? ? }
? ? ? ? public void GetStudentScore(object sender, string name)
? ? ? ? {
? ? ? ? ? ? string str = "";
? ? ? ? ? ? if (studentInfos.ContainsKey(name))
? ? ? ? ? ? {
? ? ? ? ? ? ? ? str = string.Format("{0}的成績(jī)?yōu)椋?{1}", name, studentInfos[name]);
? ? ? ? ? ? ? ? Console.WriteLine(str);
? ? ? ? ? ? }
? ? ? ? ? ? else
? ? ? ? ? ? {
? ? ? ? ? ? ? ? Console.WriteLine(str);
? ? ? ? ? ? }
? ? ? ? }
? ? }
}
②、定義學(xué)生類(lèi)
namespace ConsoleApp1.Methods
{
? ? public class StudentClass
? ? {
? ? ? ? public EventHandler<string> GetMyScore { get; set; }
? ? ? ? private string _myName;
? ? ? ? public StudentClass(string name)
? ? ? ? {
? ? ? ? ? ? _myName = name;
? ? ? ? }
? ? ? ? public void Introduce()
? ? ? ? {
? ? ? ? ? ? string str = string.Format("我的名字叫:{0}", _myName);
? ? ? ? ? ? Console.WriteLine(str);
? ? ? ? }
? ? ? ? private void GetScore()
? ? ? ? {
? ? ? ? ? ? GetMyScore.Invoke(null, _myName);//在此告訴老師需要知道自己的成績(jī)允华,沒(méi)有返回值
? ? ? ? }
? ? }
}