2

I have a Loginform which i coded into the mainform or Form1, then if all the inputed credentials are correct I will be directed to Form2 and then the Loginform will close, and the Form2 contains a logout button, and if I click the logout button I will go back to Loginform and input again the credentials.

I want to close or not to be shown in the taskbar icon the Loginform when I'm successfully login, I only want to see one icon not two icon in the taskbar.

Note: Loginform is in the Form1, and Form2 is another form.

void btnLogin_Click(object sender, EventArgs e)
    {
        bool error = true;
        if ((txtUserLog.Text.Trim() == "") || (txtPassLog.Text.Trim() == ""))
        {
            MessageBox.Show("Please fill all fields!");
        }
        cfgotcall.tether(settings);
        cfgotcall.engageQuery("SELECT * FROM tblUsers");
        unitbl = cfgotcall.tbl;
        cfgotcall.untether();
        for (int i = 0; i < unitbl.Rows.Count; i++)
        {
            if ((unitbl.Rows[i].ItemArray.ElementAt(2).ToString().Equals(txtUserLog.Text, StringComparison.OrdinalIgnoreCase)) && (unitbl.Rows[i].ItemArray.ElementAt(3).Equals(txtPassLog.Text)))
            {
                if (unitbl.Rows[i].ItemArray.ElementAt(4).ToString().Equals("Registrar", StringComparison.OrdinalIgnoreCase))
                {
                    error = false;
                    i = unitbl.Rows.Count;
                    cfgotcall.engageQuery("SELECT * FROM tblUsers");
                    frmInterface sfInterface = new frmInterface();
                    sfInterface.enable_mnuSIM(true);
                    sfInterface.ShowDialog();
                }
                else if (unitbl.Rows[i].ItemArray.ElementAt(4).ToString().Equals("Accounting", StringComparison.OrdinalIgnoreCase))
                {
                    error = false;
                    i = unitbl.Rows.Count;
                    cfgotcall.engageQuery("SELECT * FROM tblUsers");
                    frmInterface sfInterface = new frmInterface();
                    sfInterface.enable_mnuSAM(true);
                    sfInterface.ShowDialog();
                }
         }
Chappie
  • 31
  • 4

4 Answers4

0

You can simply create a new instance of the second form and show it, simultaneously hiding the current form somthing like this:

//when login successed
Form2 form2=new Form2();
form2.Show();
this.Hide()//closes the current form.

Goodluck.

Slashy
  • 1,841
  • 3
  • 23
  • 42
  • i tried but it terminates the whole program not the form – Chappie Oct 15 '15 at 07:53
  • I coded it,and now it shows multiple login form, whenever i click logout, it just loops – Chappie Oct 15 '15 at 07:58
  • @Chappie yea.. because you should add an `else` statement after the conidiont of if the crendetials are empty.. – Slashy Oct 15 '15 at 08:00
  • sir, i have 3 accounts Registrar,Accounting and Admin that has a different access level to a form, so i need it to detect what type of access level that user has, and direct it to desired form. so i put `if` and `else` – Chappie Oct 15 '15 at 08:05
  • i tried `.Hide()` but when i close the **X** in the Loginform it doesn't fully terminated the program – Chappie Oct 15 '15 at 08:10
0

Additional to the correct answer of @Slashy:

  1. "Use Form1.Visible = False;" or "This.Visible = False;" because .Close() on the Mainform exists the whole program. #Edit: ".Hide()" as @Slashy edited is also a possibility :)

  2. You shouldn't continue after the showing the MessageBox as it wouldn't make any sense, so do something like:

if ((txtUserLog.Text.Trim() == "") || (txtPassLog.Text.Trim() == ""))
            {
                MessageBox.Show("Please fill all fields!");
                return; // Abort continuing when no data entered
            }
iDraGoN
  • 119
  • 8
  • why should i abort it? so if the fields are empty the program will exit? what if I want to log in again and then im gonna start it again from my IDE is that it? – Chappie Oct 15 '15 at 08:22
  • Return; prevents the function from continuing and doesn't exit the program - like going to the end of it without running the code after the return. It was an example, you can also ask for the credentials again and then continue in the function after starting it again to check if they're filled in now. – iDraGoN Oct 15 '15 at 08:36
  • oh ok it's much clearer now thanks, didn't understand what your point before – Chappie Oct 15 '15 at 12:50
0

I think your approach just create more problems for your application when you need to handle login and main forms simultaneously.

On my opinion LoginForm must handle only login processing and Main form will be shown only if LoginForm succesfully passed.

Main form and login form are different forms

Write your own Main method which will look like this:

public void Main()
{
    using LoginForm login = new LoginForm()
    {
        if(login.ShowDialog() != DialogResult.Ok)
        {
            return;
        }
    }

    //Show main form if login was succesfull
    using Form1 main = new Form1()
    {
        main.ShowDialog();
    }

}
Fabio
  • 31,528
  • 4
  • 33
  • 72
0
private void buttonLogin_Click(object sender, EventArgs e)
{
    MainForm mainForm = new MainForm();
    this.Hide();
    mainForm.ShowDialog();
    this.Close();
}

ok guys thanks for your suggestion, i think i found the answer here it is and it's working on my code, a big thanks for those who help :)

this is the link:

it came from @Bhaggya Solangaarachchi

https://stackoverflow.com/a/31912692/5448305

Community
  • 1
  • 1
Chappie
  • 31
  • 4