国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

C#窗體傳值

2019-11-17 02:47:08
字體:
來源:轉載
供稿:網友

C#窗體傳值

整理一下:

1.靜態變量傳值,非常簡單適合簡單的非實例的

public calss form1:Form{    public static int A;}public class form2:Form{   form1.A=1; }

2.委托傳值

public class form1:Form{  public int A{get;set;}  public static Action<int> setIntADelForClass;//類的委托  public Action<int>setIntADel   //實例的委托  public void setintA(int a){    A=a;  }  public void form_load(object sender, EventArgs e){  setIntADelForClass=setIntA;  setIntADel   =setIntA;  }}public class form2:Form{  public void setFom1IntA{   form1.setIntADelForClass(10);//通過類的委托將form1 的A變量設置為10  form1 fm1=new form1();  fm1.setIntADel(12);//通過實例的委托將新實例fm1的A變量設置為12  }}

3.使用onwer屬性,適合對話框之間的傳值

public class Form1:Form{  public int A{get;set;}     PRivate void button1_click(object sender, EventArgs e){    A=10;    Form2 fm2=new Form2();    fm2.ShowDialog(this);   }}public class Form2:Form{    private void button1_Click(object sender, EventArgs e)        {            Form1 fm = (Form1)this.Owner;            MessageBox.Show(fm.A);//讀Form1的A            fm.A=11;//寫Form1的A                                        }}

  當然也可以使用委托繼續傳值

4.重構窗體構造函數,初始化的時候傳值,只適合初始化的適合,不夠方便

5.委托+事件的方法

下面代碼是一個點擊Form1 button 使Form2的button顯示Form1.textbox內容可以一次性傳很多值,步驟是在窗體A中聲明一個事件,B窗體中實現相同方法簽名的方法為事件賦值,B中回調該方法Form1的代碼://--------------------------------------->>--------------------Form1--------------- public partial class Form1 : Form{     public string B //獲取textbox1的text        {            get { return textBox1.Text; }            set            {                textBox1.Text = value;            }        }        public delegate void EventArgsaccept(object sender, acceptEventArgs e);//聲明一個事件簽名的委托        public static event EventArgsaccept accept;//相當于實例化一個事件        private void button1_Click(object sender, EventArgs e)        {            acceptEventArgs ae = new acceptEventArgs();            ae.b = B;                        if (accept != null) {                accept(this,ae);            }        }    }  }  public class acceptEventArgs : EventArgs {//封裝EventArgs類,添加可傳遞的屬性        public  string b { get; set; }    }//------------------->>----------------------------------end code of form1-----------form2的代碼,實現一個相同簽名的方法,如我們的accept的簽名是 方法名(object a,acceptEventArgs b);//-------------------------------------->>---------------------------------------code of form2--- public partial class Form2 : Form{  private void Form2_Load(object sender, EventArgs e)        {            Form1.accept += Form1_accept;//為form1的事件賦值,當form1執行該事件的時候會執行該方法        }  void Form1_accept(object sender,acceptEventArgs e) {//實現一個相同方法簽名的方法            this.button1.Text = e.b;                 }}//------------------------>>---------------

  

具體的原理,我想因為委托是函數指針所以可以通過傳值能保存函數指針的位置?所以可以標記相應的實例的,執行其他實例的方法?還沒看編譯原理,發表一下自己看法,不要誤導大眾

6.通過全局數據讀寫,適合登陸驗證

 AppDomain.CurrentDomain.SetData("user", "小明"); AppDomain.CurrentDomain.GetData("user");

  


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 屏南县| 黄大仙区| 天门市| 措勤县| 蓝田县| 连平县| 左云县| 普兰县| 红原县| 宜春市| 湘潭市| 长丰县| 兰州市| 开远市| 邛崃市| 平顶山市| 海晏县| 安宁市| 淮阳县| 文成县| 扬州市| 永德县| 鄂尔多斯市| 永丰县| 耿马| 乐清市| 德江县| 邳州市| 淮滨县| 江孜县| 河北区| 鄄城县| 永顺县| 九龙坡区| 宜宾市| 佛山市| 六安市| 湾仔区| 临桂县| 五台县| 喜德县|