属性

来源:互联网 发布:php oa系统原理 编辑:程序博客网 时间:2024/06/10 21:04
public class MyUser
    
{
        
private string name;
        
private string pwd;

        
public string Name
        
{
            
get
            
{
                
return this.name;
            }

            
set
            
{
                
this.name = value;
            }

        }


        
public string Pwd
        
{
            
get
            
{
                
return this.pwd;
            }

            
set
            
{
                
this.pwd = value;
            }

        }

        
public MyUser(string name, string pwd)
        
{
            
this.name = name;
            
this.pwd = pwd;
        }

    }

在命名空间里添加一个类

在需要的窗体里再添加

 

public partial class Form4 : Form
    
{
        
private MyUser user;
        
public MyUser LoginUser
        
{
            
get
            
{
                
return this.user;
            }

            
set
            
{
                
this.user = value;
                
if (this.user != null)
                
{
                    
this.textBox1.Text = this.user.Name;
                    
this.textBox2.Text = this.user.Pwd;
                }

            }

        }

        
public Form4()
        
{
            InitializeComponent();
        }


        
private void Form4_Load(object sender, EventArgs e)
        
{
            
if (this.user != null)
            
{
                
this.textBox1.Text = this.user.Name;
                
this.textBox2.Text = this.user.Pwd;
            }

        }

    }

调用语句:

 

        private void button3_Click(object sender, EventArgs e)
        
{
            Form4 fm2 
= FormCache.GetForm("WinFormTest.Form4"as Form4;
            fm2.LoginUser 
= null;
            fm2.Show();

        }


        
private void button4_Click(object sender, EventArgs e)
        
{
            Form4 fm2 
= FormCache.GetForm("WinFormTest.Form4"as Form4;
            fm2.LoginUser 
= new MyUser("这是用户名""这里是密码");
            fm2.Show();
        }

 

原创粉丝点击