Luka Košir
Luka Košir

Reputation: 13

How do you close c# winform completely?

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!

Upvotes: 1

Views: 349

Answers (3)

Abdul Saleem
Abdul Saleem

Reputation: 10612

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..

Upvotes: 2

Hugo Woesthuis
Hugo Woesthuis

Reputation: 193

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();

Upvotes: 0

Gaurav Sharma
Gaurav Sharma

Reputation: 586

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.

Upvotes: 0

Related Questions