读书人

C#不同窗体传值有关问题求答案

发布时间: 2012-08-01 17:53:41 作者: rapoo

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 dataReader = command.ExecuteReader();





if (dataReader.Read())


{

string password = dataReader["pass"].ToString();
int bb = int.Parse(dataReader["errortimes"].ToString());
if (bb <= 3)
{
if (password == aa)
{
MessageBox.Show("登录成功");

Form2 m = new Form2();
m.ShowDialog();
this.X = 2;



}

else
{

MessageBox.Show("密码错误");
error(name);
}
}
else { MessageBox.Show("登录次数过多"); }
}
else
{


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

}
}


}







private void button2_Click(object sender, EventArgs e)
{
Form3 a = new Form3();
a.ShowDialog();
}
}

}


、、、、、、、、、、、、、、、、、、、、、、、、、、、、、form2




private void Form2_Load(object sender, EventArgs e)
{
Form1 tt = new Form1();
if (tt.X == 2)
{

this.用户管理ToolStripMenuItem.HideDropDown();
this.用户管理ToolStripMenuItem.Enabled = false;


。。。。。。*****部分为主要问题所在。求指导

[解决办法]
你做的那个属性确实不好传值。。
一般这样做在子窗口可以传给父窗口
不如你定义一个静态变量
public static int X;
然后把值传给他。。调用的时候 类名.X就好了

读书人网 >C#

热点推荐