1

I have 2 forms in one project. One form is used as "login screen" and the other one is used as program after login. I have a problem with closing forms completely, because if I press the 'x' button on top right project can still be found as background process.

GIF of the problem: click me

Code i'm using:

    private void bVpisi_Click(object sender, EventArgs e)
    {
        SqlConnection conn = new SqlConnection(@"Data Source=LUKA-PRENOSNIK\SQLEXPRESS;Database=Registracija;Trusted_Connection=True");
        SqlDataAdapter sda = new SqlDataAdapter("SELECT COUNT(*) FROM Registracija WHERE Up_ime='" + tbUp_ime.Text + "' AND Geslo='" + tbGeslo.Text + "'", conn);
        DataTable dt = new DataTable();
        sda.Fill(dt);
        if (dt.Rows[0][0].ToString() == "1")
        {
            this.Hide();
            Glavna g = new Glavna();
            g.Show();
        }
        else
        {
            MessageBox.Show("Vnesena kombinacija uporabniškega imena in gesla nista pravilna!");
        }
    }
}

Thank you or your help!

Hasan Fathi
  • 5,610
  • 4
  • 42
  • 60

3 Answers3

2

If you only have those two forms, Login form, and this main form, then it will be better you completely shut down the application by using

Application.Exit();

This will close the whole application, threads and other app depended background process running..

Abdul Saleem
  • 10,098
  • 5
  • 45
  • 45
  • Not the non UI threads though and what are those other processes that you talk about? Application.Exit deceives you easily with threads – Steve Mar 07 '15 at 10:31
  • Sorry. I've edited that Mr. Appatakkar. I meant closing of non UI threads because, i've used this in some applications where i keep Serverthreads and some other back working threads, and i've observed this statement closing all those threads. – Abdul Saleem Mar 07 '15 at 10:52
0

formInstance.Close() usually works. Hide() might be keeping it in background. I am wondering if you hiding your Login form or any other form before reaching to the program.

Gaurav Sharma
  • 586
  • 3
  • 10
0

Use the "Close" function in C#, an example here

        private void button1_Click(object sender, EventArgs e)
{
    SqlConnection conn = new SqlConnection(@"Data Source=LUKA-PRENOSNIK\SQLEXPRESS;Database=Registracija;Trusted_Connection=True");
    SqlDataAdapter sda = new SqlDataAdapter("SELECT COUNT(*) FROM Registracija WHERE Up_ime='" + tbUp_ime.Text + "' AND Geslo='" + tbGeslo.Text + "'", conn);
    DataTable dt = new DataTable();
    sda.Fill(dt);
    if (dt.Rows[0][0].ToString() == "1")
    {
        this.Hide();
        Glavna g = new Glavna();
        g.Show();
        Close();
    }
    else
    {
        MessageBox.Show("Vnesena kombinacija uporabniškega imena in gesla nista pravilna!");

    }

It will close the form, if there are other forms opened, then they will not close. If you want to navigate to another form use this code

        Form2 form2 = new Form2(); // This is the code to navigate to the right form
        form2.Tag = this;
        form2.Show(this);
        Hide();
Hugo Woesthuis
  • 193
  • 2
  • 15
  • Close Class? Where is it? Do you mean the Form.Close() function? – Abdul Saleem Mar 07 '15 at 08:56
  • No. 'Form' is the class and Close() is a function in that class. – Abdul Saleem Mar 07 '15 at 09:41
  • Good. But still the Close() function does'nt solve his problem. That's why he posted a question here. Close() function just closes the form that he tries to close by using X button. But there is another form that still remains unclosed in his application, i.e his Splash Screen. So the process of the application still remains unclosed. You can see the status of your processes by opening Task Manager. Ctrl + Shift + Esc. – Abdul Saleem Mar 07 '15 at 09:46