如何在MainForm(启动)窗体关闭时阻止自动关闭窗体? [英] How to Prevent Auto Closing of Forms When MainForm(Start Up) form is Closed?

查看:81
本文介绍了如何在MainForm(启动)窗体关闭时阻止自动关闭窗体?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Visual Studio中有一个c#Winform应用程序...

当我的项目运行时为用户打开登录表单,用户名和密码

当用户提供时正确的信息这个登录表单已关闭,主MDI打开,一秒钟后突然关闭了

i想要阻止他们自动关闭

所以请问任何人都知道如何解决? ??

i have an c# Winform application in Visual studio ...
when my project runs Login form is opened for user that take user name and password
when user provide correct information this login form closed and Main MDI open which suddenly closed itself after a second
i want to Prevent them from auto closing
so Please can any one knows how to fix???

public void login()
        {
            string a = "No";
            if (txtuser.Text != "" & txtpass.Text != "")
            {
             string queryText = "SELECT Count(*) FROM tbllogin " +
                         "WHERE username = @Username AND pass = @Password";
                using (SqlConnection cn = new SqlConnection("Server= localhost;                
database=tailoringmng; integrated Security=true"))
                using (SqlCommand cmd = new SqlCommand(queryText, cn))
                {
                    cn.Open();
                    cmd.Parameters.AddWithValue("@Username", txtuser.Text);  
                    cmd.Parameters.AddWithValue("@Password", txtpass.Text);
                    int result = (int)cmd.ExecuteScalar();
                    if (result > 0)
                    {
                        frmmainmdi mdi = new frmmainmdi();
                        a = "Yes";
                        mdi.A = "Yes";
                        mdi.Show();



                        this.Close();
                    }
                    else
                        MessageBox.Show("User Not Found!");
                }

            }
            

        }

推荐答案

似乎Login()是你的主要形式,这就是为什么当它关闭时,其他会自动关闭..所以你可以做的是,将表单的可见性设置为false

It seems that Login() is your main form, that is why when it is closed, other are automatically closed.. So instead what you can do is, set the visibility of the form the to false.
public string login()
        {
            string a = "No";
            if (txtuser.Text != "" & txtpass.Text != "")
            {
             string queryText = "SELECT Count(*) FROM tbllogin " +
                         "WHERE username = @Username AND pass = @Password";
                using (SqlConnection cn = new SqlConnection("Server= localhost;
database=tailoringmng; integrated Security=true"))
                using (SqlCommand cmd = new SqlCommand(queryText, cn))
                {
                    cn.Open();
                    cmd.Parameters.AddWithValue("@Username", txtuser.Text);
                    cmd.Parameters.AddWithValue("@Password", txtpass.Text);
                    int result = (int)cmd.ExecuteScalar();
                    if (result > 0)
                    {
                        frmmainmdi mdi = new frmmainmdi();
                        a = "Yes";
                        mdi.A = "Yes";
                        mdi.Show();

                        this.Visible = false;
                    }
                    else
                        MessageBox.Show("User Not Found!");
                }

            }


        }



-KR


-KR


如果处理事件 FormClosing ,则可以阻止关闭主窗体。此事件可以取消。如果为事件参数传递的属性 Handled 指定true,则可以在某些条件下取消关闭。请参阅:

http://msdn.microsoft.com/en-us/library/system.windows.forms.form.formclosing(v = vs.110).aspx [ ^ ],

http:/ /msdn.microsoft.com/en-us/library/system.windows.forms.formclosingeventhandler(v=vs.110).aspx [ ^ ],

< a href =http://msdn.microsoft.com/en-us/library/system.windows.forms.formclosingeventargs(v=vs.110).aspx> http://msdn.microsoft.com/en- us / library / system.windows.forms.formclosingeventargs(v = vs.110).aspx [ ^ ]。



取消收盘时,您可以拨打隐藏



有不同的技术。您可以在 Main< code>中关闭应用程序并重新启动它。方法(切入点)。比如,您可以循环(或多次)运行< code> Application.Run ,并将不同的表单作为参数传递。有时,它用于为应用程序提供两种主要形式,例如,一种是登录或启动,另一种是真正的主要形式。只记住:你不能重复使用真正封闭的形式;处置。



-SA
You can prevent closing main form if you handle the event FormClosing. This event is cancellable. You can cancel closing on some condition, if you assign true to the property Handled of the event arguments parameter passing. Please see:
http://msdn.microsoft.com/en-us/library/system.windows.forms.form.formclosing(v=vs.110).aspx[^],
http://msdn.microsoft.com/en-us/library/system.windows.forms.formclosingeventhandler(v=vs.110).aspx[^],
http://msdn.microsoft.com/en-us/library/system.windows.forms.formclosingeventargs(v=vs.110).aspx[^].

When you cancel the closing, you can call Hide instead.

There are differen techniques. You can close the application and start it again, in your Main<code> method (entry point). Say, you can run <code>Application.Run in cycle (or several times), with different form passing as a parameter. Sometimes it is used to give the application two main form, say, one is loging or splash, and, on second run, "real" main. Only remember: you cannot re-use truly closed form; it is disposed.

—SA


这篇关于如何在MainForm(启动)窗体关闭时阻止自动关闭窗体?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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