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

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

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms; namespace WindowsFormsApplication1
{
static class Program
{
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new MainForm()); //程序启动于主窗体,而非登录窗体
}
}
}
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms; namespace WindowsFormsApplication1
{
public partial class LoginForm : Form
{
public MainForm mainForm; //获得主窗体引用,可以更新主窗体的登录标志
public LoginForm()
{
InitializeComponent();
} private void button1_Click(object sender, EventArgs e)
{
if (textBox1.Text == "admin")
{
mainForm.isLogin = true;
this.Close(); //登陆成功则关闭登录窗体
}
else
{
MessageBox.Show("密码错误!");
}
}
}
}
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms; namespace WindowsFormsApplication1
{
public partial class MainForm : Form
{
public bool isLogin = false; //登录标志
public MainForm()
{
InitializeComponent();
} private void MainForm_Load(object sender, EventArgs e)
{
LoginForm lf = new LoginForm();
lf.mainForm = this;
lf.ShowDialog();
if (isLogin)
{
label1.Text = "load success!"; //原load事件要放到这里
}
else
{
this.Close();
}
}
}
}

winform程序登陆后关闭登录窗体的更多相关文章

  1. winform关闭登录窗体打开主窗体的方法

    实际使用 Program.cs代码 //声明一个线程 private static System.Threading.Mutex mutex; /// <summary> /// 应用程序 ...

  2. WPF:验证登录后关闭登录窗口,显示主窗口的解决方法

    http://www.27ba.com/post/145.html WPF:验证登录后关闭登录窗口,显示主窗口的解决方法 最近想做一个基于Socket的通讯工具,想模仿QQ那样,需要先登录,登录成功后 ...

  3. C# 关闭登录窗体,显示主窗体

    首先在programm.cs里设置登录窗体显示 static class Program    {        /// <summary>        /// The main ent ...

  4. .NET混合开发解决方案15 WebView2控件集成到WinForm程序编译后的文件及结构说明

    系列目录     [已更新最新开发文章,点击查看详细] WebView2控件应用详解系列博客 .NET桌面程序集成Web网页开发的十种解决方案 .NET混合开发解决方案1 WebView2简介 .NE ...

  5. winform程序中为无边框窗体手动添加窗体拖动代码

            Point oldMousePoint;//记录开始移动窗口前鼠标点下箭头的位置        Point oldFormPoint;//记录开始移动窗口前窗体位置        // ...

  6. Winform程序拖拽文件到窗体

    1:首先需要将接收拖拽的窗体属性AllowDrop设置为True. 2:编写窗体拖拽进入(DragEnter)和拖拽完成(DragDrop)事件. private void FrmCode_DragE ...

  7. Identity Server 4登陆后返回登录前页面

  8. winform程序公布后,client下载报错“您的 Web 浏览器设置不同意执行未签名的应用程序”

    如题 在winserver2008服务器上操作会报错.解决的方法: IE→Internet选项→安全→可信网站,加入信任公布的IP地址

  9. WinForm 之 程序启动不显示主窗体

    在 WinForm 程序启动时,不显示主窗体的实现方法主要有以下5种,第五种最简单,而且效果也不错,第四种方法也值得推荐. 实例代码如下: //隐藏窗体的方法1/5:不指定任何窗体为主窗体 //注意: ...

随机推荐

  1. linux用户的基本操作2 用户密码管理

    目录 linux系统的基本用户操作2 用户的扩展知识 用户密码管理 linux系统的基本用户操作2 3)使用userdel删除账户 语法 : userdel [-r] username -r 同时删除 ...

  2. httptesting HTTP(s)接口自动化测试框架

    坐标: https://github.com/HttpTesting/pyhttp # HttpTesting ![PyPI](https://img.shields.io/pypi/v/HttpTe ...

  3. day12 python函数名的应用 闭包 迭代器

    day12 python   一.函数名的应用(是第一类对象)     函数名的命名规范和变量是一样的     函数名其实就是变量名 def func():     print('i am funct ...

  4. 【leetcode】941. Valid Mountain Array

    题目如下: Given an array A of integers, return true if and only if it is a valid mountain array. Recall ...

  5. loadRunner之参数关联

    录制脚本,对用户名和密码进行参数化: Action() { web_url("WebTours", "URL=http://127.0.0.1:1080/WebTours ...

  6. 洛谷P1087--FBI树(二叉树)

    题目描述 我们可以把由“0”和“1”组成的字符串分为三类:全“0”串称为B串,全“1”串称为I串,既含“0”又含“1”的串则称为F串. FBI树是一种二叉树,它的结点类型也包括F结点,B结点和I结点三 ...

  7. [CSP-S模拟测试]:队长快跑(DP+离散化+线段树)

    题目背景 传说中,在远古时代,巨龙大$Y$将$P$国的镇国之宝窃走并藏在了其巢穴中,这吸引着整个$P$国的所有冒险家前去夺回,尤其是皇家卫士队的队长小$W$.在$P$国量子科技实验室的帮助下,队长小$ ...

  8. Vuetify按需加载配置

    自己配置vuetify按需加载的步骤,在此记录: 执行npm install vuetify –save 或 yarn add vuetify添加vuetify添加依赖执行npm install -- ...

  9. css使子元素在父元素居中的各种方法

    html结构: <div class="parent"> <div class="child"></div> </di ...

  10. linux日常---1、linux下安装、查看、卸载包常用命令

    linux日常---1.linux下安装.查看.卸载包常用命令 一.总结 一句话总结: 对比学习 1.linux如何查看系统中安装的程序? rpm -qa   # 查看所有安装的软件包 2.linux ...