C# WinForm窗体应用(第四天)
一、点击登录按钮,将两个窗体进行连接,并进行用户名和密码验证。
1 /// <summary> 2 /// 登录设置 3 /// </summary> 4 /// <param name="sender"></param> 5 /// <param name="e"></param> 6 private void btnLogin_Click(object sender, EventArgs e) 7 { 8 string userName = this.textBox1.Text; 9 string pwd = this.textBox2.Text; 10 if(userName=="haha"){ 11 if(pwd=="123456"){ 12 MessageBox.Show("登陆成功!"); 13 //链接两个页面 14 WFromMain mainWindow = new WFromMain(); 15 mainWindow.Show();//展示WFromMain此窗体 16 this.Hide();//隐藏Form1窗体 17 } 18 else 19 { 20 MessageBox.Show("输入密码有误,请重新输入!"); 21 this.textBox2.Text = ""; 22 } 23 } 24 else 25 { 26 MessageBox.Show("用户不存在!"); 27 this.textBox1.Text = ""; 28 this.textBox2.Text = ""; 29 } 30 }
二、在同一个解决方案中调用不同窗体的方法
在如下位置进行修改,并且切换调用即可:
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Threading.Tasks; 5 using System.Windows.Forms; 6 7 namespace WindowsFormLogin01 8 { 9 static class Program 10 { 11 /// <summary> 12 /// 应用程序的主入口点。 13 /// </summary> 14 [STAThread] 15 static void Main() 16 { 17 Application.EnableVisualStyles(); 18 Application.SetCompatibleTextRenderingDefault(false); 19 Application.Run(new Form1()); 20 } 21 } 22 }
针对不同的窗体,要切换时对 Application.Run(new Form1()); 这一行的 Form1 进行修改即可。
三、项目实现代码(暂存)
1 using System; 2 using System.Collections.Generic; 3 using System.ComponentModel; 4 using System.Data; 5 using System.Drawing; 6 using System.Linq; 7 using System.Text; 8 using System.Threading.Tasks; 9 using System.Windows.Forms; 10 11 namespace WindowsFormsDemo1 12 { 13 public partial class FrmMain1 : Form 14 { 15 public FrmMain1() 16 { 17 InitializeComponent(); 18 } 19 20 private void 保存ToolStripMenuItem_Click(object sender, EventArgs e) 21 { 22 MessageBox.Show("保存操作"); 23 } 24 25 private void EditToolStripMenuItem_Click(object sender, EventArgs e) 26 { 27 MessageBox.Show("编辑操作"); 28 } 29 30 private void AddEmloyeeToolStripMenuItem_Click(object sender, EventArgs e) 31 { 32 // MessageBox.Show("添加员工操作"); 33 FrmAddEmployee addEmployee = new FrmAddEmployee(); 34 35 //判断 是否有子窗体 36 if (this.MdiChildren == null || this.MdiChildren.Length == 0) { 37 addEmployee.Show(); 38 addEmployee.MdiParent = this;//给子窗体 指定 主窗体 39 return; 40 } 41 42 bool flag = false; 43 foreach (var item in this.MdiChildren) 44 { 45 if (item.Text == addEmployee.Text) { 46 addEmployee = item as FrmAddEmployee; 47 addEmployee.Activate(); 48 flag = true; 49 break; 50 } 51 } 52 if (!flag) { 53 addEmployee.Show(); 54 addEmployee.MdiParent = this;//给子窗体 指定 主窗体 55 } 56 } 57 58 /// <summary> 59 /// 弹出修改窗体 60 /// </summary> 61 /// <param name="sender"></param> 62 /// <param name="e"></param> 63 private void ModifyEmployeeToolStripMenuItem_Click(object sender, EventArgs e) 64 { 65 FrmModifyEmployee modifyEmployee = new FrmModifyEmployee(); 66 67 if (this.MdiChildren == null || this.MdiChildren.Length == 0) 68 { 69 modifyEmployee.Show(); 70 modifyEmployee.MdiParent = this;//给子窗体 指定 主窗体 71 return; 72 } 73 bool f = false; 74 foreach (var item in this.MdiChildren) 75 { 76 if (modifyEmployee.Text == item.Text) { 77 modifyEmployee = item as FrmModifyEmployee; 78 modifyEmployee.Activate(); 79 f = true; 80 break; 81 } 82 } 83 84 if (!f) { 85 modifyEmployee.Show(); 86 modifyEmployee.MdiParent = this; 87 } 88 89 } 90 91 private void FrmMain1_FormClosing(object sender, FormClosingEventArgs e) 92 { 93 Environment.Exit(0);//关闭整个运行环境 94 95 } 96 97 private void CloseAllToolStripMenuItem_Click(object sender, EventArgs e) 98 { 99 100 int len = this.MdiChildren.Length; 101 for (int i = 0; i <len ; i++) 102 { 103 Form frm = this.MdiChildren[0]; 104 frm.Close(); 105 } 106 } 107 } 108 }
四、限制子窗体不超出父窗体界限
1 private void SchoolToolStripMenuItem_Click(object sender, EventArgs e) 2 { 3 //点击后显示SchoolTan这个弹窗 4 SchoolTan schooltanchuang=new SchoolTan(); 5 schooltanchuang.Show(); 6 //让子窗体不超出父窗体界限 7 this.IsMdiContainer = true; 8 WFromMain mainForm = new WFromMain(); 9 schooltanchuang.MdiParent = this; 10 /* 11 * 方法:明确父子关系: 12 son form = new Form(); 13 form.MdiParent = this(father); 14 form.show(); 15 前提是先要设置father窗体 16 isMdiContainer = true; 17 * **/ 18 }
五、鼠标点击右键弹出菜单,并全部关闭弹出所有窗口。
1 private void 关闭全部窗体ToolStripMenuItem_Click(object sender, EventArgs e) 2 { 3 Environment.Exit(0); 4 }
并将右边属性菜单的contextMenuStrip进行设置即可(此属性是点击父窗体进行设置)。
存在的就是合理的,总有问题要解决!