C# WinForm窗体应用(第四天)
2024-09-28 04:26:29
一、点击登录按钮,将两个窗体进行连接,并进行用户名和密码验证。
/// <summary>
/// 登录设置
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnLogin_Click(object sender, EventArgs e)
{
string userName = this.textBox1.Text;
string pwd = this.textBox2.Text;
if(userName=="haha"){
if(pwd==""){
MessageBox.Show("登陆成功!");
//链接两个页面
WFromMain mainWindow = new WFromMain();
mainWindow.Show();//展示WFromMain此窗体
this.Hide();//隐藏Form1窗体
}
else
{
MessageBox.Show("输入密码有误,请重新输入!");
this.textBox2.Text = "";
}
}
else
{
MessageBox.Show("用户不存在!");
this.textBox1.Text = "";
this.textBox2.Text = "";
}
}
二、在同一个解决方案中调用不同窗体的方法
在如下位置进行修改,并且切换调用即可:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms; namespace WindowsFormLogin01
{
static class Program
{
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}
}
}
针对不同的窗体,要切换时对 Application.Run(new Form1()); 这一行的 Form1 进行修改即可。
三、项目实现代码(暂存)
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 WindowsFormsDemo1
{
public partial class FrmMain1 : Form
{
public FrmMain1()
{
InitializeComponent();
} private void 保存ToolStripMenuItem_Click(object sender, EventArgs e)
{
MessageBox.Show("保存操作");
} private void EditToolStripMenuItem_Click(object sender, EventArgs e)
{
MessageBox.Show("编辑操作");
} private void AddEmloyeeToolStripMenuItem_Click(object sender, EventArgs e)
{
// MessageBox.Show("添加员工操作");
FrmAddEmployee addEmployee = new FrmAddEmployee(); //判断 是否有子窗体
if (this.MdiChildren == null || this.MdiChildren.Length == ) {
addEmployee.Show();
addEmployee.MdiParent = this;//给子窗体 指定 主窗体
return;
} bool flag = false;
foreach (var item in this.MdiChildren)
{
if (item.Text == addEmployee.Text) {
addEmployee = item as FrmAddEmployee;
addEmployee.Activate();
flag = true;
break;
}
}
if (!flag) {
addEmployee.Show();
addEmployee.MdiParent = this;//给子窗体 指定 主窗体
}
} /// <summary>
/// 弹出修改窗体
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void ModifyEmployeeToolStripMenuItem_Click(object sender, EventArgs e)
{
FrmModifyEmployee modifyEmployee = new FrmModifyEmployee(); if (this.MdiChildren == null || this.MdiChildren.Length == )
{
modifyEmployee.Show();
modifyEmployee.MdiParent = this;//给子窗体 指定 主窗体
return;
}
bool f = false;
foreach (var item in this.MdiChildren)
{
if (modifyEmployee.Text == item.Text) {
modifyEmployee = item as FrmModifyEmployee;
modifyEmployee.Activate();
f = true;
break;
}
} if (!f) {
modifyEmployee.Show();
modifyEmployee.MdiParent = this;
} } private void FrmMain1_FormClosing(object sender, FormClosingEventArgs e)
{
Environment.Exit();//关闭整个运行环境 } private void CloseAllToolStripMenuItem_Click(object sender, EventArgs e)
{ int len = this.MdiChildren.Length;
for (int i = ; i <len ; i++)
{
Form frm = this.MdiChildren[];
frm.Close();
}
}
}
}
四、限制子窗体不超出父窗体界限
private void SchoolToolStripMenuItem_Click(object sender, EventArgs e)
{
//点击后显示SchoolTan这个弹窗
SchoolTan schooltanchuang=new SchoolTan();
schooltanchuang.Show();
//让子窗体不超出父窗体界限
this.IsMdiContainer = true;
WFromMain mainForm = new WFromMain();
schooltanchuang.MdiParent = this;
/*
* 方法:明确父子关系:
son form = new Form();
form.MdiParent = this(father);
form.show();
前提是先要设置father窗体
isMdiContainer = true;
* **/
}
五、鼠标点击右键弹出菜单,并全部关闭弹出所有窗口。
private void 关闭全部窗体ToolStripMenuItem_Click(object sender, EventArgs e)
{
Environment.Exit();
}
并将右边属性菜单的contextMenuStrip进行设置即可(此属性是点击父窗体进行设置)。
最新文章
- Docker 简介
- python判断类型
- Linux / Unix Command: bunzip2--reference
- 169	Majority Element [LeetCode Java实现]
- Cocos开发中性能优化工具介绍之使用Windows任务管理器
- Json 处理datetime
- java 使用正则表达式对文件名非法字符处理
- java并发编程_基本模块构建
- FormsAuthentication登录ReturnUrl使用绝对路径
- Activiti第三篇【连接、排他网关、指定任务处理人、组任务】
- PHP获取一周的日期
- React Native随笔——警告处理方法(持续更新)
- vue项目总结
- AE视频制作 参考 资源 科幻科技风格 开场 公司企业宣传
- 【转】导致SQL执行慢的原因
- VirtualBox fedora29 安装
- 【Spark调优】:如果实在要shuffle,使用map侧预聚合的算子
- Git使用—第一讲:初识版本控制工具
- saltstack二次开发(二)
- centos7配置网桥