如何动态创建权限按钮?

时间:2017-03-30 01:23:48

标签: c# sql winforms windows-forms-designer

由于我可以根据登录的用户自动创建按钮,例如管理员用户属于管理员类型且具有所有权限,那么管理员用户在登录表单时应该生成按钮等权限,怎么能它会完成吗?

我的登录代码:

  private void button1_Click(object sender, EventArgs e)
            {
    if (txtUser.Text == "" || txtContrasenna.Text == "")
                {
                    MessageBox.Show("Please provide UserName and Password");
                    return;
                }
                try
                {
                    //Create SqlConnection
                    SqlConnection con = new SqlConnection(cs);
                    SqlCommand cmd = new SqlCommand("Select * from Users where NameUser=@nameuser and Password=@password", con);
                    cmd.Parameters.AddWithValue("@nameuser", txtUser.Text);
                    cmd.Parameters.AddWithValue("@password", txtContrasenna.Text);
                    con.Open();
                    SqlDataAdapter adapt = new SqlDataAdapter(cmd);
                    DataTable ds = new DataTable();
                    adapt.Fill(ds);
                    con.Close();
                   // int count = ds.Tables[0].Rows.Count;

                    //If count is equal to 1, than show frmMain form
                    if (ds.Rows.Count == 1)
                    {
                        switch (ds.Rows[0]["UserTypeId"] as int?)
                        {
                            case 1:
                                {
                                    this.Hide();
                                   Menus.MenuSuperUser ss = new Menus.MenuSuperUser();
                                    ss.Show();
                                   // MessageBox.Show("MAMALON");

                                    break;
                                }
                            case 2:
                                {
                                    this.Hide();
                                    PantallasUsers.DashboardVendedor ss = new PantallasUsers.DashboardVendedor();
                                    ss.Show();

                                    break;
                                }
                            default:
                                {
                                    MessageBox.Show("NOTBABY");
                                    break;
                                }
                        }
                    }
                    else
                    {
                        MessageBox.Show("Algo esta mal, revisa tus datos.");
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }

1 个答案:

答案 0 :(得分:0)

// Create desired control
CTRL ctrl = new CTRL();

// Set properties
ctrl.SomeProperty = value;

// Where to put the control
ctrl.Location = new Point(X, Y);

Controls.Add(ctrl);