winform程序登陆后关闭登录窗体

摘要:
";//原load事件要放到这里}else{this.Close();}}}}

用winform做程序的时候,我们一般都是在Program先启动登录窗体,然后登录成功后才创建主窗体,结果这就导致了登录窗体无法关闭

所以如果我们不在Program的程序入口先创建登录窗体的话就能完美实现登录成功后关闭登录窗体的功能了,直接贴代码

usingSystem;
usingSystem.Collections.Generic;
usingSystem.Linq;
usingSystem.Threading.Tasks;
usingSystem.Windows.Forms;

namespaceWindowsFormsApplication1
{
    static classProgram
    {
        /// <summary>
        ///应用程序的主入口点。
        /// </summary>
[STAThread]
        static voidMain()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new MainForm()); //程序启动于主窗体,而非登录窗体
}
    }
}
usingSystem;
usingSystem.Collections.Generic;
usingSystem.ComponentModel;
usingSystem.Data;
usingSystem.Drawing;
usingSystem.Linq;
usingSystem.Text;
usingSystem.Threading.Tasks;
usingSystem.Windows.Forms;

namespaceWindowsFormsApplication1
{
    public partial classLoginForm : Form
    {
        public MainForm mainForm; //获得主窗体引用,可以更新主窗体的登录标志
        publicLoginForm()
        {
            InitializeComponent();
        }

        private void button1_Click(objectsender, EventArgs e)
        {
            if (textBox1.Text == "admin")
            {
                mainForm.isLogin = true;
                this.Close(); //登陆成功则关闭登录窗体
}
            else{
                MessageBox.Show("密码错误!");
            }
        }
    }
}
usingSystem;
usingSystem.Collections.Generic;
usingSystem.ComponentModel;
usingSystem.Data;
usingSystem.Drawing;
usingSystem.Linq;
usingSystem.Text;
usingSystem.Threading.Tasks;
usingSystem.Windows.Forms;

namespaceWindowsFormsApplication1
{
    public partial classMainForm : Form
    {
        public bool isLogin = false; //登录标志
        publicMainForm()
        {
            InitializeComponent();
        }

        private void MainForm_Load(objectsender, EventArgs e)
        {
            LoginForm lf = newLoginForm();
            lf.mainForm = this;
            lf.ShowDialog();
            if(isLogin)
            {
                label1.Text = "load success!"; //原load事件要放到这里
}
            else{
                this.Close();
            }
        }
    }
}

免责声明:文章转载自《winform程序登陆后关闭登录窗体》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇Egg.js 实现向服务器上传图片supervisor常用命令下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

Delphi中Form的position属性与代码自定义窗体位置

通过Form的Position属性可设置窗体的初始位置,如选择DesktopCenter为桌面中心,ScreenCenter为屏幕中心,等等。 这个属性在很多时候简化了程序代码。 但是,如果设置了position为ScreenCenter和DesktopCenter等,在窗体oncreate和onshow事件中使用代码控制窗体的位置就会不成功了,无论设置S...

Winform中实现更改DevExpress的RadioGroup的选项时更改其他控件(TextEdit、ColorPickEdit)的值

场景 Winform中实现读取xml配置文件并动态配置ZedGraph的RadioGroup的选项: https://blog.csdn.net/BADAO_LIUMANG_QIZHI/article/details/100540708 在上面实现了将RadioGroup的选项根据配置文件动态配置后, 比如这里有三个选项,在更改选项时会对其他的控件的值进行...

一名Delphi程序员的开发习惯

一名Delphi程序员的开发习惯 有关开发习惯的一些想法,如鲠在喉,不吐不快。究其发贴动机,当然不排除有骗取参与分的可能,但另一方面,也希望能给同行(念Xing)者提供一些建议,或者参考(希望不是误人子弟)。同时,也希望各位能够就我的这些陋习,发表一点看法,给出批评和指正的意见。谢谢。一.建立工程目录首先,第一步要做的,当然是给新项目建一个单独的目录(别笑)...

C#中WinForm程序退出方法技巧总结

C#中WinForm程序退出方法技巧总结 一、关闭窗体 在c#中退出WinForm程序包括有很多方法,如:this.Close();Application.Exit();Application.ExitThread();System.Environment.Exit(0);等他们各自的方法不一样,下面我们就来详细介绍一下。 1.this.Close();只...

wpf mvvm模式下 在ViewModel关闭view

本文只是博主用来记录笔记,误喷 使用到到了MVVM中消息通知功能 第一步:在需要关闭窗体中注册消息   1 public UserView() 2 { 3 this.DataContext = new UserViewModel(); 4 InitializeComponent();...

C#(winform)为button添加背景图片

转自:https://www.cnblogs.com/zhangchenliang/p/4335372.html 1.既然是添加背景图片 所以这里应该使用 Button.BackgroudImage = "" ;来设置图片 而不应该使用 Button.Image = ""; 因为使用BackgroudImage来设置背景图片,我们还可以使用 Backgro...