Click here to Skip to main content
15,896,557 members
Please Sign up or sign in to vote.
2.00/5 (3 votes)
See more:
i am facing a problem in closing my application.i have three forms
form1 which is of registration
form2 which is of login
mainsoftware which is my software which will run after a correct login is done.
i have used many this.hide so i think there is some problem when i click on exit button. it doesent stop debugging. so i used a form closing event in which a message box of yes no button. when i run my program the first registration form is run when i click on exit button. it shows me yess or no. when i click on yess button the form exits but when i click on no button the same message box is appear once."""" which i dont want that""""
i think there is some problem in my coding. can an one help me out plz
help me
thanks in advance
here is my coding of first form
========================================================================



private void button1_Click(object sender, EventArgs e)
        {
            SqlConnection con = new SqlConnection();
            con.ConnectionString = @"Data Source=.\SQLEXPRESS;AttachDbFilename=E:\project\insert\login\Database1.mdf;Integrated Security=True;User Instance=True";
            string q = "insert into Table1 values('" + textBox1.Text + "','" + textBox2.Text + "')";
            con.Open();
            SqlCommand cmd = new SqlCommand(q, con);
            try
            {
                cmd.ExecuteNonQuery();
                MessageBox.Show("thanx for registration " + textBox1.Text);
                this.Hide();
                Form2 f7 = new Form2();
                f7.Show();
            }

            catch (Exception)
            {
                MessageBox.Show("username is present already choose a diffrent username");
                this.Hide();
                Form1 f4 = new Form1();
                f4.Show();
            }



            con.Close();


        }

        public void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            this.Hide();
            Form2 f3 = new Form2();
            f3.Show();

        }

        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (MessageBox.Show("do you want to save changes to your text?", "My application", MessageBoxButtons.YesNo) == DialogResult.No)
            {
                e.Cancel = true;
                Application.Exit();
            }
        }
Posted
Comments
[no name] 17-Sep-12 14:55pm    
Instead of reposting this over and over why don't you update the questions that you already have? Your problem is that you are calling Application.Exit from within your form closing event which causes your form closing event to be called again.

It is because 'Application.Exit()' method raises FormClosing() event itself. So you need to redesign this. Try to see the order of Form events here: http://msdn.microsoft.com/en-us/library/86faxx0d(v=vs.80).aspx[^] and about Application.Exit() method: http://msdn.microsoft.com/en-us/library/ms157894.aspx[^]
I also suggest you not to use such a code:
C#
string q = "insert into Table1 values('" + textBox1.Text + "','" + textBox2.Text + "')";
it is very unsafe, try to use SqlCommand.Parameters instead.
 
Share this answer
 
Comments
sariqkhan 16-Sep-12 8:58am    
bro. Can oyu give the code? I cant understand msdin.
If you'll provide me with the simpe code i can understand by seeing it.
As i am new to coding part. I am learning .net
skydger 17-Sep-12 4:42am    
Ok, steps are following as I understood you application.
When user is not found in database, then Form1 appeared, but when you call Show() method, the form 'detaches' from that scope and works independantly. And the similar problem with another form.
So there are at least three solutions.
a) You can make objects of Form1 and Form2 a private members of your main class, therefore can't lose contol on them when main thread ending its work.
b) You can create delegates which being invokeed when Form1 or Form2 are closing.
c) You simply can use ShowDialog() method.

For example:

MessageBox.Show("username is present already choose a diffrent username");
this.Hide();
Form1 f4 = new Form1();
if( f4.ShowDialog() == DialogResult.OK ){
// creating a new user
}else{
// no action?..
}
this.Show(); // if needed
sariqkhan 17-Sep-12 6:35am    
bro you have not understood my question
form example i have 2 forms.
Form1 have a button which is linked with form2
example
this.hide// this closes the current form
form2 f2 = new form2()
f2.show()// shows the second form.
But when i click on close button i manually have to stop debugging. So i used form closing method.
In which messagebox of yes or no..
When i click on yess the program ends but i manually have to stop debugging(which i think is not good) so i want some help in this. Plz bro help me.. I have put this question to many others but no one knows what can be done.
: (
shaikh-adil 17-Sep-12 6:41am    
doing same project Help us
Please
Just use the following code

C#
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
    {
        if (MessageBox.Show("do you want to save changes to your text?", "My application", MessageBoxButtons.YesNo) == DialogResult.No)
        {
            e.Cancel = true;
        }
    }


The Application.Exit() causes the message box to appear again.
 
Share this answer
 
Comments
sariqkhan 16-Sep-12 8:59am    
okay ahmed. I will try after namaz.
And thanx for helping.
Zazaak allah
sariqkhan 16-Sep-12 9:38am    
bhai its not working..
For example i have the forms. First is connected the second. I have created a object and shows it
like form2 f2 = new form2()
f2.show() and putting the form1. Hide.
By
this.hide()
and the second form is connected with the third.
Like same way.
When i use.
Form closing event and done
e.cancle= false.
Then the application exits but i have to stop debugging myself.
Their is no other way for it? So i used application.exit()
what can i do now?
shaikh-adil 17-Sep-12 6:42am    
please suggest some thing. Which will help
Have you tried to call f3.ShowDialog() method instead ofcalling ordinary Show?
 
Share this answer
 
Comments
sariqkhan 17-Sep-12 6:38am    
bro showing showdialog. It is okay but i have to close the previous form with this.close. Which creates a problem in form closing event. I want confirmation yess no. In form closing event but when i click on yess form closes but i manually have to stop debugging. Some thread is running in background which is. This.hide.
What can be done? Plz suggest something
shaikh-adil 17-Sep-12 6:41am    
doing same project Help us
Please
You can make a class for DB connection for better coding. and avoid
C#
Application.Exit()
From
C#
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (MessageBox.Show("do you want to save changes to your text?", "My application", MessageBoxButtons.YesNo) == DialogResult.No)
            {
                e.Cancel = true;
                Application.Exit();
            }
        }
 
Share this answer
 
Ok, try this solution.
At first I created a following delegate somewhere in a namespace scope:
C#
public delegate void OnCloseForm( bool login_accepted );

Then I created a login form:
C#
// AuthorizationForm.Designer.cs file
partial class AuthorizationForm
{
    private System.ComponentModel.IContainer components = null;
    /// <summary>
    /// A delegate which being invoke on exit.
    /// </summary>
    private OnCloseForm cfd;

    protected override void Dispose(bool disposing)
    {
        if (disposing && (components != null))
        {
            components.Dispose();
        }
        base.Dispose(disposing);
    }

    #region Windows Form Designer generated code
    private void InitializeComponent()
    {
         /// skipped
    }
    #endregion

    private System.Windows.Forms.Button btnLogin;
    private System.Windows.Forms.Button btnCancel;
    private System.Windows.Forms.Label lblLogin;
    private System.Windows.Forms.TextBox tbLogin;
    private System.Windows.Forms.TextBox tbPasswd;
    private System.Windows.Forms.Label lblPasswd;
}

Also I set in designer window btnAccept as Accept property and btnCancel as Cancel property of AuthorizationForm.
C#
// /AuthorizationForm.cs file
public partial class AuthorizationForm : Form
{
    public OnCloseForm Result{
        set { this.cfd = value; }
    }
    public string Login
    {
        get { return this.tbLogin.Text; }
    }

    public string Password
    {
        get { return this.tbPasswd.Text; }
    }

    public AuthorizationForm()
    {
        InitializeComponent();
        this.cfd = null;
    }

    private void btnLogin_Click(object sender, EventArgs e)
    {
        this.DialogResult = DialogResult.OK;
        this.Close();
        // Invoking 'accept' event
        if (this.cfd != null) this.cfd.Invoke(true);
    }

    private void btnCancel_Click(object sender, EventArgs e)
    {
        this.DialogResult = DialogResult.Cancel;
        this.Close();
        // Invoking 'abort' event
        if (this.cfd != null) this.cfd.Invoke(false);
    }
}

Then I added following code to application's main frame:
C#
/// ExampleForm.cs file
public partial class ExampleForm : Form
{
    public ExampleForm()
    {
        InitializeComponent();
    }

    private void btnAuthorize_Click(object sender, EventArgs e)
    {
        this.Hide();
        AuthorizationForm aform = new AuthorizationForm();
        aform.Result = OtherFormClosing;
        aform.Show();
    }

    // Implementation fo delegate

    public void OtherFormClosing(bool login_accepted){
        this.Show();
        string acc_text = (login_accepted ? "Login entered!" : "Authorization canceled!");
        MessageBox.Show(acc_text);
    }
}

One can do anything in delegate and there will be no any need to catch FormClosing event to destroy detached forms randomly.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900