使用消息框关闭应用程序时出错 [英] error in closing an application with mesage box

查看:85
本文介绍了使用消息框关闭应用程序时出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在关闭我的应用程序时遇到问题.我有三种形式
正在注册的form1
登录的form2
mainsoftware是我的软件,正确登录后即可运行.
我已经使用了许多this.hide,所以我认为当我单击退出按钮时出现了一些问题.它确实停止调试.所以我使用了一个窗体关闭事件,在该事件中是一个是否"按钮的消息框.当我运行程序时,当我单击退出按钮时,将运行第一个注册表格.它向我显示是或否.当我单击是"按钮时,表单将退出,但当我单击否"按钮时,同一消息框将出现一次.""我不希望该消息""
我认为我的编码中存在一些问题.一个可以帮助我的人吗?
帮帮我
在此先感谢
这是我的第一种形式的编码
================================================== =====================



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

推荐答案

这是因为"Application.Exit()"方法引发FormClosing()事件本身.因此,您需要重新设计.尝试在此处查看Form事件的顺序: http://msdn.microsoft .com/en-us/library/86faxx0d(v = vs.80).aspx [ http://msdn.microsoft.com/zh-CN/library/ms157894.aspx [
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:
string q = "insert into Table1 values('" + textBox1.Text + "','" + textBox2.Text + "')";

这是非常不安全的,请尝试使用SqlCommand.Parameters.

it is very unsafe, try to use SqlCommand.Parameters instead.


只需使用以下代码

Just use the following code

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()导致消息框再次出现.



The Application.Exit() causes the message box to appear again.


您是否尝试调用f3.ShowDialog()方法而不是调用普通的Show?
Have you tried to call f3.ShowDialog() method instead ofcalling ordinary Show?


这篇关于使用消息框关闭应用程序时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆