2016-12-14 120 views
-2

我有2个窗体一个用于登录,另一个用于调用依赖于登录的功能。从另一种形式访问变量

FORM1

public partial class Form1 : Form 
    { 
     static public string userId, userPassword; 
     public Form1() 
     { 
      InitializeComponent(); 
     } 

     private void label1_Click(object sender, EventArgs e) 
     { 

     } 

     private void button1_Click(object sender, EventArgs e) 
     { 

      if (Program.AllStudents.ContainsKey(userId)) 
      { 
       Program.studentdata student = Program.LoginStudent(userId); 
       if (student.password == userPassword) 
       { 
        Form3 studentcommands = new Form3(); 
        studentcommands.ShowDialog(); 
       } 
       else 
       { 
        MessageBox.Show("Wrong username or password", "Error", 
    MessageBoxButtons.OK, MessageBoxIcon.Error); 
        this.Close(); 
       } 
      } 
      else 
      { 
       MessageBox.Show("Wrong username or password", "Error", 
    MessageBoxButtons.OK, MessageBoxIcon.Error); 
       this.Close(); 
      } 

     } 

如果我想要访问的另一种形式被称为学生的对象? 我该怎么办?

+1

搜索这个完全相同问题的数百万重复中的任何一个。 –

+1

这是我甚至问过自己的问题,并且经常问这个问题。去检查另一个答案,但为了方便,有两个选项AFAIK。首先,让你的对象公开,通过Form1.objectName访问它。或者你的第二个选择是将对象作为参数传递给第二个表单。 –

+0

为了我的好奇心,这是一个班级任务吗?几个小时前提出了同样的问题。 – Trey

回答

2

你可以在这个表格申报类型学生的Public property和使用Form1.Propertyname

从别人访问了解更多关于性能在这个MSDN topic

或者只是声明你的对象作为Public