C#/WINFORM上下级窗口值变量传递

单个参数、少量参数使用。多行多列多参数,还是使用表方法进行传递!

父传子,子传父
//Form1
public void but_Click(object sender, EventArgs e){
    From2 A= new Form2();
    A.TextBox2Values = textBox1.Text;        //Form2的TextBox2Values方法

    if(A.ShowDialog()==DialgResult.OK){
        textBox1.Text = A.textBox2Values;
    }
    
}


//Form2
//将外部不可访问的textBox2.Text封装成属性TextBox1Value
public void TextBox2Values{

    set { textBox2.Text = values}
    get { return textBox2.Text;}
}

private void but2_Click(object sender,EventArgs e){
    this.DialogResult = DialogResult.OK;        //这里的DialogResult是Form2类对象的属性
}

1、实例化窗口时传值。(父窗口=》子窗口)

//Form1
private void but_Click(object sender,EventArgs e){
    //Form1窗口的textBox1.Text作为入参
    Form2 A = new Form2(textBox1.Text);
    A.ShowDialog();
}

//Form2
public Form2(string str)        //对应Form1的入参类型,str=textBox1.Text
{
    InitializeComponent();
    textBox2.Text=str;
}


2、设置公共窗口变量,static 也可以认为全局变量.(父窗口=》子窗口)
//Form1


private void but_Click(object sender,EventArgs e){
    Form2 A = new Form2();        //实例化新窗口,可以使用新窗口的变量
    A.str = textBox1.Text;        //str 在Form2中声明全局变量
    A.ShowDialog();
}

//Form2

public string str;            //Form2中设置变量,不需要 static 关键词

//窗口初始化加载
private void Form2_Load(object sender,EventArgs e){
    textBox2.Text = str;
}


3、Owner方法设置窗口(子窗口=》父窗口)
//Form1

public void but_Click(object sender,EventArgs e){
    Form2 A = new From2();
    A.ShowDialog(this);        //this 不可缺少,(将窗体显示为具有指定所有者:窗体f2的所有者是Form1类当前的对象)
}

//Form2

public void but2_Click(object sender,EventArgs e){
    Form1 B = (Form1)this.Owner;            将本窗体的拥有者强制设为Form1类的实例B
    B.Controls["textBox1"].Text = textBox2.Text;    //找到B窗口的控件赋值
}


4、将3优化为方法赋值
//Form1

public void but_Click(object sender,EventArgs e){
    Form2 A = new From2();
    A.ShowDialog(this);
}

//新增一个方法,赋值数据
public void ChangeText(string str){
    textBox1.Text = str;
}

//Form2

public void but2_Click(object sender,EventArgs e){
    Form1 B = (Form1)this.Owner;
    B.ChangeText(textBox2.Text);        //调用Form1的ChangeText 方法
}

5、get set方法
//Form1

public void but_Click(object sender,EventArgs e){
    Form2 A = new From2();
    A.ShowDialog(this);
}

public string TextBox1Value{
    set {textBox1.Text = value;}
    get {return textBox1.Text;}
}


//Form2

public void but2_Click(object sender,EventArgs e){
    Form1 B = (Form1)this.Owner;
    B.TextBox1Values = textBox2.Text;        
}

6、通过委托事件传值,实现不同控件数据交互
//Form1
private void button1_Click(object sender, EventArgs e)
         {
            Form2 f2 = new Form2();
            f2.ChangeText += new ChangeTextHandler(Change_Text);//将事件和处理方法绑在一起,这句话必须放在f2.ShowDialog();前面
            f2.ShowDialog();            
        }
 
        public void Change_Text(string str)
         {
             textBox1.Text = str;
        }  


//Form2
public delegate void ChangeTextHandler(string str);  //定义委托

{

public event ChangeTextHandler ChangeText;  //定义事件
 
         private void button2_Click(object sender, EventArgs e)
         {
             if (ChangeText != null)//判断事件是否为空
             {
                 ChangeText(textBox2.Text);//执行委托实例  
                 this.Close();
             }           
         }    

}

7、Action<>泛型委托和lambda表达式简化
//Form1
private void button1_Click(object sender, EventArgs e)
         {
             Form2 f2 = new Form2();
             f2.ChangeText = (str) => textBox1.Text = str;//用lambda表达式实现,这句话必须放在f2.ShowDialog();前面
             f2.ShowDialog();            
         }

//Form2
public Action<string> ChangeText;//之前的定义委托和定义事件由这一句话代替
 
         private void button2_Click(object sender, EventArgs e)
         {
             if (ChangeText != null)//判断事件是否为空
             {
                 ChangeText(textBox2.Text);//执行委托实例  
                 this.Close();
             }           
         }    

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值