winform 窗体退出前判断表单是不是修改过,窗体上的控件有很多个,TextBox,combox,datagridview
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了winform 窗体退出前判断表单是不是修改过,窗体上的控件有很多个,TextBox,combox,datagridview相关的知识,希望对你有一定的参考价值。
判断修改的目的是为了提示用户是否要保存!
给你思路,在退出窗体的事件中写代码遍历窗体中的控件,如果当前控件是TextBox 并判断他的Text属性是否为空
如果不为空,则提示保存
代码如下
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
foreach (Control currentControl in Controls)
if (currentControl is TextBox)
TextBox objTextBox = currentControl as TextBox;
if (!string.IsNullOrEmpty(objTextBox.Text))
MessageBox.Show("请保存数据");
追问
是判断是否修改过或者说编辑过!还有不是只有TextBox啊!
追答是什么类型就去判断他的默认值是不是变了,比如CheckBox 默认值是False,但在遍历的时候是True,就说明改变了。原理都是一样的
参考技术A 加载时将信息保存到tag属性里 窗体退出时比较tag和当前的值 参考技术B 简单说呢,就是比较两个值,一个是打开时的值,一个是关闭时的值,相同不保存,不同则要提示保存。具体操作,可以自己思考思考哦~ 参考技术C 这个主要看你使用什么方式编写代码的标准对象编写方式,你可以在UI view实体上实现INotifyPropotyChanged 和IEditAbled两个接口去实现
如果是使用dataset+Bindingsource的话,本身dataset已经实现上面的接口,你可以用dataset.getChanged方法获得是否有修改的值
如果你是自己手工赋值的,那就没啥简单方法了。只能自己缓存一个原始的对象值,然后自己挨个判定 参考技术D 添加每个控件的值改变事件
winform 屏幕右上角悬浮窗demo
当窗体最小化时,显示右悬浮窗!!!
1新建右上角小窗口
(1)去除边框 (2)最前显示(设置背景图案)
1 using System; 2 using System.Collections.Generic; 3 using System.ComponentModel; 4 using System.Data; 5 using System.Drawing; 6 using System.Text; 7 using System.Windows.Forms; 8 9 namespace TopMost 10 { 11 public partial class frmTopMost : Form 12 { 13 private Point ptMouseCurrentPos, ptMouseNewPos, ptFormPos, ptFormNewPos; 14 private bool blnMouseDown = false; 15 private frmMain pParent; 16 //Add ContextMenuStrip . 17 private ContextMenuStrip docMenu; 18 19 public frmTopMost(frmMain pMain) 20 { 21 InitializeComponent(); 22 pParent=pMain; 23 } 24 25 private void frmTopMost_Load(object sender, EventArgs e) 26 { 27 this.FormBorderStyle = FormBorderStyle.None; 28 this.TopMost = true; 29 this.Show(); 30 this.Top = 100; 31 this.Left = Screen.PrimaryScreen.Bounds.Width - 200; 32 this.Width = 35; 33 this.Height = 35; 34 35 docMenu = new ContextMenuStrip(); 36 37 ToolStripMenuItem OpenLable = new ToolStripMenuItem(); 38 OpenLable.Text = "Open"; 39 OpenLable.Click+=new EventHandler(OpenLable_Click); 40 ToolStripMenuItem ExitLable = new ToolStripMenuItem(); 41 ExitLable.Text = "Exit"; 42 ExitLable.Click+=new EventHandler(ExitLable_Click); 43 44 docMenu.Items.AddRange(new ToolStripMenuItem[] { OpenLable,ExitLable}); 45 this.ContextMenuStrip = docMenu; 46 } 47 private void frmTopMost_MouseMove(object sender, System.Windows.Forms.MouseEventArgs e) 48 { 49 if (blnMouseDown) 50 { 51 //Get the current postion of the mouse in the screen. 52 ptMouseNewPos = Control.MousePosition; 53 54 //Set window postion. 55 ptFormNewPos.X = ptMouseNewPos.X - ptMouseCurrentPos.X + ptFormPos.X; 56 ptFormNewPos.Y = ptMouseNewPos.Y - ptMouseCurrentPos.Y + ptFormPos.Y; 57 58 //Save window postion. 59 Location = ptFormNewPos; 60 ptFormPos = ptFormNewPos; 61 62 //Save mouse pontion. 63 ptMouseCurrentPos = ptMouseNewPos; 64 } 65 } 66 67 private void frmTopMost_MouseDown(object sender,System.Windows.Forms.MouseEventArgs e) 68 { 69 if (e.Button == MouseButtons.Left) 70 { 71 blnMouseDown = true; 72 73 //Save window postion and mouse postion. 74 ptMouseCurrentPos = Control.MousePosition; 75 ptFormPos = Location; 76 } 77 } 78 79 private void frmTopMost_MouseUp(object sender, System.Windows.Forms.MouseEventArgs e) 80 { 81 if (e.Button == MouseButtons.Left) 82 { 83 blnMouseDown = false; 84 } 85 } 86 //Restore parent from. 87 private void frmTopMost_DoubleClick(object sender, System.EventArgs e) 88 { 89 SwitchToMain(); 90 } 91 private void SwitchToMain() 92 { 93 pParent.RestoreWindow(); 94 this.Hide(); 95 } 96 97 #region //Context events. 98 private void OpenLable_Click(object sender, System.EventArgs e) 99 { 100 SwitchToMain(); 101 } 102 private void ExitLable_Click(object sender, System.EventArgs e) 103 { 104 Application.Exit(); 105 } 106 #endregion 107 108 private void frmTopMost_MouseEnter(object sender, System.EventArgs e) 109 { 110 this.toolTip1.Active = true; 111 this.toolTip1.AutoPopDelay = 500000; 112 this.toolTip1.InitialDelay = 500; 113 this.toolTip1.ReshowDelay = 100; 114 this.toolTip1.ShowAlways = true; 115 } 116 private void frmTopMost_MouseLeave(object sender, System.EventArgs e) 117 { 118 this.toolTip1.Active = false; 119 } 120 121 } 122 }
2、主窗体
1 using System; 2 using System.Collections.Generic; 3 using System.ComponentModel; 4 using System.Data; 5 using System.Drawing; 6 using System.Text; 7 using System.Windows.Forms; 8 9 namespace TopMost 10 { 11 public partial class frmMain : Form 12 { 13 public frmMain() 14 { 15 InitializeComponent(); 16 } 17 18 private FormWindowState fwsPrevious; 19 private frmTopMost myTopMost; 20 private void frmMain_Load(object sender, EventArgs e) 21 { 22 //save the windowState; 23 fwsPrevious = this.WindowState; 24 25 //Create top most window; 26 myTopMost = new frmTopMost(this); 27 } 28 29 private void frmMain_SizeChanged(object sender,EventArgs e) 30 { 31 if (this.WindowState == FormWindowState.Minimized) 32 { 33 //window minimized. 34 //show the most window. 35 myTopMost.Show(); 36 this.ShowInTaskbar = false; 37 } 38 else if(this.WindowState!=fwsPrevious) 39 { 40 fwsPrevious = WindowState; 41 } 42 } 43 //Restore window. 44 public void RestoreWindow() 45 { 46 this.WindowState = fwsPrevious; 47 this.ShowInTaskbar = true; 48 } 49 } 50 }
完!!
以上是关于winform 窗体退出前判断表单是不是修改过,窗体上的控件有很多个,TextBox,combox,datagridview的主要内容,如果未能解决你的问题,请参考以下文章
c# winform程序 如何判断用户输入的验证码是不是正确?