C#WINFORM窗体传参的最佳实践

入VS2005后,大家可以发现子窗体操作父窗体不能沿用2003下的方法:把父窗体的空间访问属性由private改为public.IDE已经把控件声明这部分代码隐藏了,所以只有采用更加对象一点的方法。

父窗体与子窗体间的参数传递我采用的步骤如下:
1 父窗体中声明一个静态的父窗体类型的临时对象
        public static frmFather frmFatherTemp;

2 父窗体构造函数中对该变量赋值

        public frmFather()
        {
            InitializeComponent();
            frmFatherTemp = this;
        }


3 把要传递的参数设置为父窗体的一个属性,并设置访问器。访问其的set方法中进行了参数与父窗体控件绑定的操作。

        private string testValue;
       
        public string TestValue
        {
            get
            {
                return testValue;   
            }
            set
            {
                this.testValue = value;
                this.txtFather.Text = value;
            }
        }


4 父窗体参数传递事件中对要传递的参数赋值,并打开子窗体。父窗体的工作到此结束。

            this.TestValue = this.txtFather.Text;
            frmSon frm = new frmSon();
            frm.ShowDialog();


5 子窗体构造函数中设置传递参数与子窗体控件的绑定操作

        public frmSon()
        {
            InitializeComponent();
            this.txtSon.Text = frmFather.frmFatherTemp.TestValue;
        }


6 子窗体回传事件中,对父窗体的临时对象的该参数属性赋值

        public frmSon()
        {
            InitializeComponent();
            this.txtSon.Text = frmFather.frmFatherTemp.TestValue;
        }

ok。一切搞定!

全部代码如下:

frmFather.cs

None.gif using  System;
None.gif
using  System.Collections.Generic;
None.gif
using  System.ComponentModel;
None.gif
using  System.Data;
None.gif
using  System.Drawing;
None.gif
using  System.Text;
None.gif
using  System.Windows.Forms;
None.gif
None.gif
namespace  winFormParameterPass
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    
public partial class frmFather : Form
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
public static frmFather frmFatherTemp;
InBlock.gif
InBlock.gif        
private string testValue;
InBlock.gif        
InBlock.gif        
public string TestValue
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
get 
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
return testValue;    
ExpandedSubBlockEnd.gif            }

InBlock.gif            
set 
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
this.testValue = value;
InBlock.gif                
this.txtFather.Text = value;
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
public frmFather()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            InitializeComponent();
InBlock.gif            frmFatherTemp 
= this;
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
private void btnFather_Click(object sender, EventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
this.TestValue = this.txtFather.Text;
InBlock.gif            frmSon frm 
= new frmSon();
InBlock.gif            frm.ShowDialog();
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

frmSon.cs
None.gif using  System;
None.gif
using  System.Collections.Generic;
None.gif
using  System.ComponentModel;
None.gif
using  System.Data;
None.gif
using  System.Drawing;
None.gif
using  System.Text;
None.gif
using  System.Windows.Forms;
None.gif
None.gif
namespace  winFormParameterPass
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    
public partial class frmSon : Form
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
public frmSon()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            InitializeComponent();
InBlock.gif            
this.txtSon.Text = frmFather.frmFatherTemp.TestValue;
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
private void btnSon_Click(object sender, EventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            frmFather.frmFatherTemp.TestValue 
= this.txtSon.Text;
InBlock.gif            
this.Close();
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

例子程序:
/Files/heekui/winFormParameterPass.rar

以上想法给新手们一点参考,请高手指教。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值