zoukankan      html  css  js  c++  java
  • [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();
    }
  • 相关阅读:
    了解windows下的npm
    “jupyter notebook 不能导入python库但是终端上可以实现”的问题的解决
    本地创建的jupyter notebook 无法连接本地环境(即不能运行代码)
    win10 + ubuntu 下右键新建md文件(转载)
    LeetCode刷题2
    【入门】离散化
    【10.5NOIP普及模拟】sum
    【10.5NOIP普及模拟】sort
    【2020.02.01NOIP普及模拟4】怪兽
    [图论]最小花费
  • 原文地址:https://www.cnblogs.com/hcbin/p/1750042.html
Copyright © 2011-2022 走看看