WinForm页面之间(父页面传递参数给子页面)传递参数

方法一通过构造函数:

父页面(frmMain)点击btnQuery按钮进入子页面(frmListInfo),将数据库名(pdtDB)传递给子页面

父页面代码:

private void btnQuery_Click(object sender, EventArgs e)
{
            string pdtDB = FISTools.TAttributeCollection.ProductInfo["DatabaseName"].ToString();//数据库名
            this.TopMost = false;//隐藏frmMain
            frmListInfo frmListInfo = new frmListInfo(pdtDB);//创建子页面frmListInfo对象,new的时候传递参数pdtDB
            frmListInfo.ShowDialog();
            this.TopMost = true;
}
View Code

子页面frmListInfo中在构造函数中初始此参数

C#代码:

public partial class frmListInfo : Form
{
string db = "";//定义一个全局变量
        public frmListInfo()
        {
            InitializeComponent();
            this.dTPickerFrom.Format = DateTimePickerFormat.Custom;
            dTPickerFrom.CustomFormat = "yyyy-MM-dd HH:mm";
            this.dTPickerTo.Format = DateTimePickerFormat.Custom;
            dTPickerTo.CustomFormat = "yyyy-MM-dd HH:mm";
        }
        public frmListInfo(string a)
        {
            InitializeComponent();
            db = a;            
        }
    //下面的程序就可以用db了
}
View Code

当有两个构造函数的时候,程序会先执行不带参数的构造函数,然后再执行带参数的构造函数。执行带参数的构造函数,在构造函数中将参数的值付给指定的变量,在子页面中就可以使用这个传递过来的值了。 

也可以这样写:

C#代码:

public partial class frmListInfo : Form
{
string db = "";//定义一个全局变量
        public frmListInfo(string a)
        {
            InitializeComponent();
            db = a;   
            this.dTPickerFrom.Format = DateTimePickerFormat.Custom;
            dTPickerFrom.CustomFormat = "yyyy-MM-dd HH:mm";
            this.dTPickerTo.Format = DateTimePickerFormat.Custom;
            dTPickerTo.CustomFormat = "yyyy-MM-dd HH:mm";
        }
        //public frmListInfo(string a)
        //{
            //InitializeComponent();
            //db = a;            
        //}
    //下面的程序就可以用db了
}
View Code

 

方法二通过子类属性来获取父类参数(winform页面可以看做是一个类):

父页面(frmMain)点击btnQuery按钮进入子页面(frmListInfo),将数据库名(pdtDB)传递给子页面

父页面代码:

private void btnQuery_Click(object sender, EventArgs e)
{
            string pdtDB = FISTools.TAttributeCollection.ProductInfo["DatabaseName"].ToString();//数据库名
            this.TopMost = false; //隐藏frmMain
            frmListInfo frmListInfo = new frmListInfo();//创建子页面对象frmListInfo
            frmListInfo.getDBName = pdtDB; //给子页面类属性赋值pdtDB
            frmListInfo.ShowDialog();
            this.TopMost = true;
}
View Code

子页面(frmListInfo)程序,当父页面执行frmListInfo.getDBName = pdtDB;时子页面中getDBName属性的set { pdtDB = value; }将pdtDB的value给了子页面属性getDBName

在子页面使用的时候,只需要直接获取属性getDBName的值即可,

即string aDBName = getDBName.ToString();

子页面代码:

public partial class frmListInfo : Form
{ 
        public frmListInfo()
        {
            InitializeComponent(); 
            this.dTPickerFrom.Format = DateTimePickerFormat.Custom;
            dTPickerFrom.CustomFormat = "yyyy-MM-dd HH:mm";
            this.dTPickerTo.Format = DateTimePickerFormat.Custom;
            dTPickerTo.CustomFormat = "yyyy-MM-dd HH:mm";
        }

        #region GetpdtDBName 子页面getDBName属性
        private string pdtDB;
        public string getDBName
        {
            get { return pdtDB; }
            set { pdtDB = value; }
        }
        #endregion
      string aDBName = getDBName.ToString();//aDBName就是获得了从父页面传递过来的pdtDB
}
View Code

 

posted @ 2015-12-11 13:46  坏坏的阳光仔  阅读(952)  评论(0编辑  收藏  举报