日期:2014-05-18  浏览次数:20827 次

C#不同窗体传值问题,在线等,求答案
C#做一登陆界面,分管理员和普通用户。普通用户只能有一部分功能,我需要从登陆判断,然后确实隐藏一部分功能。但在窗口传递值问题上,有点为难。。




  public Form1()
  {
  InitializeComponent();
  }


  public int x; ************
  public int X {

  get {return x;} 此为我为form1,增加的公共属性。
  set { x = value; }
   
   
  }*********

  private void error(string name) {

  SqlConnection conn = new SqlConnection(@"Data Source=.\SqlExpress;AttachDBFilename=C:\Users\lw\Documents\Visual Studio 2010\Projects\WindowsFormsApplication1\WindowsFormsApplication1\Database1.mdf;Integrated Security=true ;User Instance=True;uid=sa;pwd=6288656");

  conn.Open();
  string sql = "update T_user set errortimes=errortimes+1 where [User]='" + name + "' ";
  SqlCommand command = new SqlCommand(sql, conn);
  SqlDataReader dataReader = command.ExecuteReader();
   
  }

  private void button1_Click(object sender, EventArgs e)
  {
  SqlConnection conn = new SqlConnection(@"Data Source=.\SqlExpress;AttachDBFilename=C:\Users\lw\Documents\Visual Studio 2010\Projects\WindowsFormsApplication1\WindowsFormsApplication1\Database1.mdf;Integrated Security=true ;User Instance=True;uid=sa;pwd=6288656");

  conn.Open();

  if (radioButton1.Checked == true) {

  string na = textBox1.Text;
  string pa = textBox2.Text;



  string ab = "select * from T_student where [name]='" + na + "'";
  SqlCommand ss = new SqlCommand(ab, conn);
  SqlDataReader cc = ss.ExecuteReader();



  if (cc.Read())
  {

  string password = cc["pass"].ToString();

  if (password == pa)
  {
  MessageBox.Show("学生用户登录成功");
  this.X = 1; *********************此为给公共属性赋值,然后传
  Form2 tt = new Form2(); 递到form2中判断隐藏
  tt.ShowDialog();
   
   








  }
  else { MessageBox.Show("密码错误"); }







  }
  else {

  MessageBox.Show("用户不存在");
   
   
   
  }
   
   
   
   
   
  }




  //管理员验证

  else if (radioButton2.Checked == true)
  {

  string name = textBox1.Text;
  string aa = textBox2.Text;



  string sql = "select * from T_user where [User]='" + name + "'";
  SqlCommand command = new SqlCommand(sql, conn);
  SqlDataReader dataR