C#中容器控件GroupBox、Panel、TabControl的应用

 
  

实现如图所示登录界面,当输入正确用户名及密码时,转到主界面,否则给出错误提示。要求不同身份的登陆者拥有不同的操作权限。

          

源代码:

1.初始化三个string类型的变量分别用来存储用户名、密码和身份。

string yhm, mm, sf;

private void Form1_Load(object sender, EventArgs e)
        {
            yhm=mm="";
            sf=radioButton1.Text;
        }

2.为三个单选按钮添加CheckedChanged事件处理程序

1>学生:

private void radioButton1_CheckedChanged(object sender, EventArgs e)
        {
            if(radioButton1.Checked)
            {
                sf=radioButton1.Text;
                checkBox1.Checked = true;
                checkBox2.Checked=checkBox3.Checked=checkBox4.Checked=checkBox5.Checked=false;
            }
        
        }

2>教师:

 private void radioButton2_CheckedChanged(object sender, EventArgs e)
        {
            if (radioButton2.Checked)
            {
                sf = radioButton2.Text;
                checkBox1.Checked=checkBox2.Checked = true;
                checkBox3.Checked = checkBox4.Checked = checkBox5.Checked = false;
            }
        
        }


3>管理员:

private void radioButton3_CheckedChanged(object sender, EventArgs e)
        {
            if (radioButton3.Checked)
            {
                sf = radioButton3.Text;
                checkBox1.Checked = checkBox2.Checked = checkBox3.Checked = checkBox4.Checked = checkBox5.Checked = true;
            }
        
        }

3.为四个Button按钮添加Click事件:

1>确定:

 private void button1_Click(object sender, EventArgs e)
        {
            yhm = textBox1.Text.Trim();
            mm = textBox2.Text.Trim();
            if (yhm == "xs" && mm == "xs" && sf == "学生")
            {
                tabControl1.SelectedTab = tabPage2;
                panel2.Visible = true;
                button4.Enabled = button5.Enabled = button6.Enabled = button7.Enabled = false;
                panel1.Visible = false;
            }


            else if (yhm == "js" && mm == "js" && sf == "教师")
            {
                tabControl1.SelectedTab = tabPage2;
                panel2.Visible = true;
                button3.Enabled=button4.Enabled = true;
                button5.Enabled = button6.Enabled = button7.Enabled = false;
                panel1.Visible = false;
            }

            else if (yhm == "gly" && mm == "gly" && sf == "管理员")
            {
                tabControl1.SelectedTab = tabPage2;
                panel2.Visible = true;
                button3.Enabled=button4.Enabled = button5.Enabled = button6.Enabled = button7.Enabled = true;
                panel1.Visible = false;
            }

            else
                MessageBox.Show("用户名或密码错误","登录失败");
        }


2>取消:

private void button2_Click(object sender, EventArgs e)
        {
            this.Close();
        }


3>注销登录:

private void button8_Click(object sender, EventArgs e)
        {
            panel1.Visible = true;
            panel2.Visible = false;
            textBox1.Text = textBox2.Text = "";
            radioButton1.Checked = true;
            tabControl1.SelectedTab = tabPage1;
        }


4>退出系统:

private void button9_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }    

运行结果展示图:





你可能感兴趣的:(C#中容器控件GroupBox、Panel、TabControl的应用)