简体   繁体   中英

Log in/log out/restrictions

I have built application in multiple winforms. First form is a login form. When user logs in, it opens him another form, let say form 2, and then from there I have menu strip which takes user further to form 3,4,5 and so on.

What I want is to put a button on a upper right corner and call it LOG OUT.This button will appear on all forms(only not on first one of course), so when user logs in, do what he needs to do, and then to have ability to log out,regardless on what form he is on. When he logs out the first form will pop out again! How can this be done? Is it possible to close form 1 (log in form, parent) and not shut down the whole application (children forms) after log in?

Next thing I need is to put restrictions... What I mean by that is that there will be different type of users, regular ones and admins, which will have more options available. I have done the login part, checked if there is user name and password from database that match eg textbox1 and textbox2 but I need some advice to implement what I just described above.

Thanks, Bane

 private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                SqlConnection con1 = getConnection();
                con1.Open();
                SqlCommand com1 = new SqlCommand();
                com1.Connection = con1;
                com1.CommandType = CommandType.Text;
                com1.CommandText = "select * from radnik WHERE username = '" + textBox2.Text + "' AND password = '" + textBox3.Text + "'";
                SqlDataReader reader = com1.ExecuteReader();

                if (reader.Read())
                {
                    MessageBox.Show("Uspesno ste se ulogovali!");
                    Form2 form2 = new Form2();
                    form2.Show();

                }
                else { MessageBox.Show("Doslo je do greske!"); }
            }

            catch (Exception ee)
            {
                MessageBox.Show(ee.StackTrace);

            }
            Refresh();

        }

this checks info for log in

 private void stoloviToolStripMenuItem1_Click(object sender, EventArgs e)
    {
        var form3 = new Form3();
        form3.Show();

    }

    private void stoloviToolStripMenuItem_Click(object sender, EventArgs e)
    {

    }

    private void sifrarnikToolStripMenuItem_Click(object sender, EventArgs e)
    {
        var form4 = new Form4();
        form4.Show();
    }

    private void rezervacijeToolStripMenuItem_Click(object sender, EventArgs e)
    {
        var form6 = new Form6();
        form6.Show();
    }

    private void porudzbineToolStripMenuItem_Click(object sender, EventArgs e)
    {
        var form7 = new Form7();
        form7.Show();   
    }

    private void magacinToolStripMenuItem_Click(object sender, EventArgs e)
    {
        var form9 = new Form9();
        form9.Show();
    }

    private void button1_Click(object sender, EventArgs e)
    {

        Form1 fr1 = new Form1();
            fr1.Show();

    }

this just lead user to different winforms

That's a typical MDI application. The Main-Form (the parent of the MDI Child) will have a button in the left upper corner which is activated when the user logs in. The login prompt's form will be a modal form. When the user clicks the logout button you should close all the children forms and then presents again the modal login form. Regarding the user's privileges is not difficult to do, just have a field in the database with that particular piece of information. It could be a byte an integer or whatever you like. Let's say 1=Normal user, 2=Admin user, etc.

I believe you would need 3 classes:

  1. a login form, where the user will authenticate by inserting the credentials and clicking the loginButton
  2. a main form, to hold the menu strip, where the other forms could be shown by pressing the menu buttons
  3. the child form, which will have a logoutButton and other custom controls (so basically you can derive other child forms from it)

In the login form, handle the login button click like this:

private void loginButton_Click(object sender, EventArgs e)
{
    if (Authenticated(username.Text, password.Text))
    {
        var mainForm = new MainForm();
        this.Visible = false;
        mainForm.Show(this);
    }
}

The main form, add a handler for the form closing event and some handlers for showing the children:

public MainForm()
{
    InitializeComponent();
    FormClosing += MainForm_FormClosing;
}

void MainForm_FormClosing(object sender, FormClosingEventArgs e)
{
    this.Owner.Visible = true;
}

private void form1ToolStripMenuItem_Click(object sender, EventArgs e)
{
    var childForm = new ChildForm();
    childForm.Show(this);
}

In the child form, when clicking on the logoutButton , get a reference to the owner of the child form (which will be the main form), close it's owned forms (all it's children) and close the main form itself:

private void logoutButton_Click(object sender, EventArgs e)
{
    var owner = ((MainForm)this.Owner);
    foreach (var childForm in owner.OwnedForms)
    {
        childForm.Close();
    }
    owner.Close();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM