• [C#(WinForm)] 窗体间传值方法


    来源:http://hackline.net/a/school/ymbc/C/2009/1221/2343.html

    // 方法一:所有权法
    // ************************************************************************
    MainForm (按钮名btnMethod1)

    publicvoid myMethod1() { this.Text ="方法一"; }
    privatevoid btnMethod1_Click(object sender, EventArgs e)
    {
    ChildForm child1
    =new ChildForm();
    child1.Owner
    =this; // 必须设置
    child1.ShowDialog();
    }

    ChildForm (按钮名btnMethod1)

    privatevoid btnMethod1_Click(object sender, EventArgs e)
    {
    MainForm main1
    = (MainForm)this.Owner;
    main1.myMethod1();
    main1.textBox1.Text
    =this.textBox1.Text;
    }

    // 方法二:自身传递法
    // ************************************************************************
    // MainForm (按钮名btnMethod2)

    publicvoid myMethod2() { this.Text ="方法二"; }
    privatevoid btnMethod2_Click(object sender, EventArgs e)
    {
    ChildForm child2
    =new ChildForm(this);
    child2.ShowDialog(
    this);
    }

    // ChildForm (按钮名btnMethod2)

    private MainForm main2;
    public ChildForm(MainForm mainform) // 重载ChildForm
    {
    InitializeComponent();
    main2
    = mainform;
    }
    privatevoid btnMethod2_Click(object sender, EventArgs e)
    {
    main2.myMethod2();
    main2.textBox2.Text
    =this.textBox2.Text;
    }

    // 方法三:属性法
    // ************************************************************************
    // MainForm (按钮名btnMethod3)

    publicvoid myMethod3() { this.Text ="方法三"; }
    privatevoid btnMethod3_Click(object sender, EventArgs e)
    {
    ChildForm child3
    =new ChildForm();
    child3.MAIN3
    =this;
    child3.ShowDialog();
    }

    // ChildForm (按钮名btnMethod3)

    private MainForm main3;
    public MainForm MAIN3
    {
    get { return main3; }
    set { main3 = value; }
    }
    privatevoid btnMethod3_Click(object sender, EventArgs e)
    {
    main3.myMethod3();
    main3.textBox3.Text
    =this.textBox3.Text;
    }

    // 方法四:委托法
    // ************************************************************************

    // MainForm (按钮名btnMethod4)
    publicdelegatevoid myDelegate4(); // 声明一个委托
    publicvoid myMethod4() { this.Text ="方法四"; }
    privatevoid btnMethod4_Click(object sender, EventArgs e)
    {
    ChildForm child4
    =new ChildForm();
    child4.myDelegate4Test
    +=new myDelegate4(myMethod4);
    child4.ShowDialog();
    }

    // ChildForm (按钮名btnMethod4)

    publicevent MainForm.myDelegate4 myDelegate4Test; // 声明事件
    privatevoid btnMethod4_Click(object sender, EventArgs e)
    {
    myDelegate4Test();
    }
  • 相关阅读:
    获取URL的name值 getUrl(url,name) 传入url和key 得到key对应的value
    封装GetQueryString()方法来获取URL的value值
    判断设备
    RecyclerView 加点击事件
    SparseArray,SparseBooleanArray和SparseIntArray
    内存泄漏监测-LeakCanary
    StrictMode
    数据结构-线性结构
    程序设计语言基础-知识点
    数据结构-概念
  • 原文地址:https://www.cnblogs.com/hcbin/p/1750042.html
Copyright © 2020-2023  润新知