Tag属性,可以在传递数据的时候保存数据。具体用法如下:

建一个主窗体FrmMain和一个frmLogin窗体;

新建一个类用于数据传递:

 

using System;
using System.Collections.Generic;
using System.Text;

namespace frmMain
{
   public  class User
    {
        private string username;
        private string userpwd;
        public string userName
        {
            get { return username; }
            set
            {
                username = value;
            }

        }
        public string userPwd
        {
            get {
                return userpwd;
            }
            set {
                userpwd = value;
            }
        }   

    }
}

在frmMain窗体代码中:

 

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace frmMain
{
    public partial class frmMain : Form
    {
        public frmMain()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
              forLogin frm = new forLogin();
            if (frm.ShowDialog() == DialogResult.OK)//显示对话框的结果是否返回一个Ok;
            {
                User user = frm.Tag as User;
              
                this.Text +=string .Format ("欢迎你:"+ user.userName);
            }
            else
            { this.Close(); }

        }
    }
}

在frmlogin窗体代码中:

 

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace frmMain
{
    public partial class forLogin : Form
    {
        public forLogin()
        {
            InitializeComponent();
        }

        private void btnLogin_Click(object sender, EventArgs e)
        {
            User user = new User();
            user.userName = txtUsername.Text.Trim();
            user.userPwd = txtPassword.Text;
            this.Tag = user;//获取或者设置包含有关控件数据的对象。可以保存数据
            this.DialogResult = DialogResult.OK;//设置对话框结果,返回一个Ok结果
        }
    }
}


 

 

posted on 2009-06-08 13:19  sinbad_li  阅读(211)  评论(0编辑  收藏  举报