473,287 Members | 1,946 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,287 software developers and data experts.

How do "dirty checking" with a Windows Form?

How do I check in a Windows Forms app if any controls have changed? I have
a form that collects data, and I want to prompt the user if they try to exit
the app, or load a new file, without saving changes that have been made.

In MFC/Win32, you'd trap the WM_COMMAND/EN_CHANGE notification messages,
etc. But, this doesn't seem to happen in Windows Forms. I tried Spy-ing a
windows forms app, and the WM_COMMAND messages don't even get sent (!).

Thanks!

- Dave
Nov 16 '05 #1
5 11316
Have event handlers on the controls. Multiple controls can all call the same
event handler.

"Dave" <dp*@nospam.eliassen.com> wrote in message
news:uy**************@TK2MSFTNGP14.phx.gbl...
How do I check in a Windows Forms app if any controls have changed? I
have
a form that collects data, and I want to prompt the user if they try to
exit
the app, or load a new file, without saving changes that have been made.

In MFC/Win32, you'd trap the WM_COMMAND/EN_CHANGE notification messages,
etc. But, this doesn't seem to happen in Windows Forms. I tried Spy-ing
a
windows forms app, and the WM_COMMAND messages don't even get sent (!).

Thanks!

- Dave

Nov 16 '05 #2
Ok, but I'm using standard controls. Isn't there a standard notification
event that controls send to their parent in Windows Forms, notifying the
parent that the control has changed? I'm pretty sure there must be an easy
way to do this - there is in Win32, and I doubt they'd have gotten rid of
such basic functionality when it was wrapped with .net.

"Bonj" <benjtaylor at hotpop d0t com> wrote in message
news:%2****************@TK2MSFTNGP10.phx.gbl...
Have event handlers on the controls. Multiple controls can all call the same event handler.

"Dave" <dp*@nospam.eliassen.com> wrote in message
news:uy**************@TK2MSFTNGP14.phx.gbl...
How do I check in a Windows Forms app if any controls have changed? I
have
a form that collects data, and I want to prompt the user if they try to
exit
the app, or load a new file, without saving changes that have been made.

In MFC/Win32, you'd trap the WM_COMMAND/EN_CHANGE notification messages,
etc. But, this doesn't seem to happen in Windows Forms. I tried Spy-ing a
windows forms app, and the WM_COMMAND messages don't even get sent (!).

Thanks!

- Dave


Nov 16 '05 #3
I dont know about Win32, but as bonj says what you want can be done simply
by suscribing a standard event handler to each controls changed event (or
relevent event for that control). Use a private bool to remember the state.
You are writing about 5 lines of code (and one for each new control added to
the form) simple e.g.

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;

namespace WindowsApplication6
{
/// <summary>
/// Summary description for Form1.
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private bool dataChanged = false;
private System.Windows.Forms.TextBox textBox1;
private System.Windows.Forms.TextBox textBox2;
private System.Windows.Forms.Button buttonClose;
private System.Windows.Forms.Button buttonSave;
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components = null;

public Form1()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();

//
// TODO: Add any constructor code after InitializeComponent call
//
}

/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}

#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.textBox1 = new System.Windows.Forms.TextBox();
this.textBox2 = new System.Windows.Forms.TextBox();
this.buttonClose = new System.Windows.Forms.Button();
this.buttonSave = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// textBox1
//
this.textBox1.Location = new System.Drawing.Point(32, 32);
this.textBox1.Name = "textBox1";
this.textBox1.TabIndex = 0;
this.textBox1.Text = "textBox1";
this.textBox1.TextChanged += new
System.EventHandler(this.formControlChanged);
//
// textBox2
//
this.textBox2.Location = new System.Drawing.Point(32, 72);
this.textBox2.Name = "textBox2";
this.textBox2.TabIndex = 1;
this.textBox2.Text = "textBox2";
this.textBox2.TextChanged += new
System.EventHandler(this.formControlChanged);
//
// buttonClose
//
this.buttonClose.Location = new System.Drawing.Point(192, 216);
this.buttonClose.Name = "buttonClose";
this.buttonClose.TabIndex = 2;
this.buttonClose.Text = "&Close";
this.buttonClose.Click += new
System.EventHandler(this.buttonClose_Click);
//
// buttonSave
//
this.buttonSave.Location = new System.Drawing.Point(112, 216);
this.buttonSave.Name = "buttonSave";
this.buttonSave.TabIndex = 3;
this.buttonSave.Text = "&Save";
this.buttonSave.Click += new System.EventHandler(this.buttonSave_Click);
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(292, 266);
this.Controls.Add(this.buttonSave);
this.Controls.Add(this.buttonClose);
this.Controls.Add(this.textBox2);
this.Controls.Add(this.textBox1);
this.Name = "Form1";
this.Text = "Form1";
this.Closing += new
System.ComponentModel.CancelEventHandler(this.Form 1_Closing);
this.ResumeLayout(false);

}
#endregion

/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}

private void formControlChanged(object sender, System.EventArgs e)
{
this.dataChanged = true;
}

private void buttonClose_Click(object sender, System.EventArgs e)
{
this.Close();
}

private void Form1_Closing(object sender,
System.ComponentModel.CancelEventArgs e)
{
if (this.dataChanged)
{
e.Cancel = true;
MessageBox.Show("Warning! Data has changed and must be saved");
}

}

private void buttonSave_Click(object sender, System.EventArgs e)
{
//data saved
this.dataChanged = false;
}

}
}

"Dave" <dp*@nospam.eliassen.com> wrote in message
news:OQ**************@TK2MSFTNGP14.phx.gbl...
Ok, but I'm using standard controls. Isn't there a standard notification
event that controls send to their parent in Windows Forms, notifying the
parent that the control has changed? I'm pretty sure there must be an
easy
way to do this - there is in Win32, and I doubt they'd have gotten rid of
such basic functionality when it was wrapped with .net.

"Bonj" <benjtaylor at hotpop d0t com> wrote in message
news:%2****************@TK2MSFTNGP10.phx.gbl...
Have event handlers on the controls. Multiple controls can all call the

same
event handler.

"Dave" <dp*@nospam.eliassen.com> wrote in message
news:uy**************@TK2MSFTNGP14.phx.gbl...
> How do I check in a Windows Forms app if any controls have changed? I
> have
> a form that collects data, and I want to prompt the user if they try to
> exit
> the app, or load a new file, without saving changes that have been
> made.
>
> In MFC/Win32, you'd trap the WM_COMMAND/EN_CHANGE notification
> messages,
> etc. But, this doesn't seem to happen in Windows Forms. I tried Spy-ing > a
> windows forms app, and the WM_COMMAND messages don't even get sent (!).
>
> Thanks!
>
> - Dave
>
>



Nov 16 '05 #4
Thanks, Mark. Yes, I finally figured out pretty much the same thing. Kind
of a hassle if you have lots of controls, though. In Win32, the controls
are sending the change event to the parent without having to subscribe.
Easier. There is an easier way than having a separate handler for each
control, though - you can use the same handler for each control of the same
type.

- Dave
"Mark Broadbent" <no****@nospam.com> wrote in message
news:Or**************@TK2MSFTNGP11.phx.gbl...
I dont know about Win32, but as bonj says what you want can be done simply
by suscribing a standard event handler to each controls changed event (or
relevent event for that control). Use a private bool to remember the state. You are writing about 5 lines of code (and one for each new control added to the form) simple e.g.

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;

namespace WindowsApplication6
{
/// <summary>
/// Summary description for Form1.
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private bool dataChanged = false;
private System.Windows.Forms.TextBox textBox1;
private System.Windows.Forms.TextBox textBox2;
private System.Windows.Forms.Button buttonClose;
private System.Windows.Forms.Button buttonSave;
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components = null;

public Form1()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();

//
// TODO: Add any constructor code after InitializeComponent call
//
}

/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}

#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.textBox1 = new System.Windows.Forms.TextBox();
this.textBox2 = new System.Windows.Forms.TextBox();
this.buttonClose = new System.Windows.Forms.Button();
this.buttonSave = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// textBox1
//
this.textBox1.Location = new System.Drawing.Point(32, 32);
this.textBox1.Name = "textBox1";
this.textBox1.TabIndex = 0;
this.textBox1.Text = "textBox1";
this.textBox1.TextChanged += new
System.EventHandler(this.formControlChanged);
//
// textBox2
//
this.textBox2.Location = new System.Drawing.Point(32, 72);
this.textBox2.Name = "textBox2";
this.textBox2.TabIndex = 1;
this.textBox2.Text = "textBox2";
this.textBox2.TextChanged += new
System.EventHandler(this.formControlChanged);
//
// buttonClose
//
this.buttonClose.Location = new System.Drawing.Point(192, 216);
this.buttonClose.Name = "buttonClose";
this.buttonClose.TabIndex = 2;
this.buttonClose.Text = "&Close";
this.buttonClose.Click += new
System.EventHandler(this.buttonClose_Click);
//
// buttonSave
//
this.buttonSave.Location = new System.Drawing.Point(112, 216);
this.buttonSave.Name = "buttonSave";
this.buttonSave.TabIndex = 3;
this.buttonSave.Text = "&Save";
this.buttonSave.Click += new System.EventHandler(this.buttonSave_Click); //
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(292, 266);
this.Controls.Add(this.buttonSave);
this.Controls.Add(this.buttonClose);
this.Controls.Add(this.textBox2);
this.Controls.Add(this.textBox1);
this.Name = "Form1";
this.Text = "Form1";
this.Closing += new
System.ComponentModel.CancelEventHandler(this.Form 1_Closing);
this.ResumeLayout(false);

}
#endregion

/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}

private void formControlChanged(object sender, System.EventArgs e)
{
this.dataChanged = true;
}

private void buttonClose_Click(object sender, System.EventArgs e)
{
this.Close();
}

private void Form1_Closing(object sender,
System.ComponentModel.CancelEventArgs e)
{
if (this.dataChanged)
{
e.Cancel = true;
MessageBox.Show("Warning! Data has changed and must be saved");
}

}

private void buttonSave_Click(object sender, System.EventArgs e)
{
//data saved
this.dataChanged = false;
}

}
}

"Dave" <dp*@nospam.eliassen.com> wrote in message
news:OQ**************@TK2MSFTNGP14.phx.gbl...
Ok, but I'm using standard controls. Isn't there a standard notification event that controls send to their parent in Windows Forms, notifying the
parent that the control has changed? I'm pretty sure there must be an
easy
way to do this - there is in Win32, and I doubt they'd have gotten rid of such basic functionality when it was wrapped with .net.

"Bonj" <benjtaylor at hotpop d0t com> wrote in message
news:%2****************@TK2MSFTNGP10.phx.gbl...
Have event handlers on the controls. Multiple controls can all call the

same
event handler.

"Dave" <dp*@nospam.eliassen.com> wrote in message
news:uy**************@TK2MSFTNGP14.phx.gbl...
> How do I check in a Windows Forms app if any controls have changed? I > have
> a form that collects data, and I want to prompt the user if they try to > exit
> the app, or load a new file, without saving changes that have been
> made.
>
> In MFC/Win32, you'd trap the WM_COMMAND/EN_CHANGE notification
> messages,
> etc. But, this doesn't seem to happen in Windows Forms. I tried

Spy-ing
> a
> windows forms app, and the WM_COMMAND messages don't even get sent (!). >
> Thanks!
>
> - Dave
>
>



Nov 16 '05 #5
Yes you are right (my code only used one handler for each of the textboxes).
There is a slightly easier way to implement this (if you have loads of
controls) and that would be to loop through the forms control collection and
assign the delegate handler to each (conditionally if required). You could
therefore write a reusable bit of code to do this.
Anyhow good luck with the coding.
Br,

Mark.
"Dave T" <dp*@eliassen.XXcomXX> wrote in message
news:uj**************@TK2MSFTNGP12.phx.gbl...
Thanks, Mark. Yes, I finally figured out pretty much the same thing.
Kind
of a hassle if you have lots of controls, though. In Win32, the controls
are sending the change event to the parent without having to subscribe.
Easier. There is an easier way than having a separate handler for each
control, though - you can use the same handler for each control of the
same
type.

- Dave
"Mark Broadbent" <no****@nospam.com> wrote in message
news:Or**************@TK2MSFTNGP11.phx.gbl...
I dont know about Win32, but as bonj says what you want can be done
simply
by suscribing a standard event handler to each controls changed event (or
relevent event for that control). Use a private bool to remember the

state.
You are writing about 5 lines of code (and one for each new control added

to
the form) simple e.g.

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;

namespace WindowsApplication6
{
/// <summary>
/// Summary description for Form1.
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private bool dataChanged = false;
private System.Windows.Forms.TextBox textBox1;
private System.Windows.Forms.TextBox textBox2;
private System.Windows.Forms.Button buttonClose;
private System.Windows.Forms.Button buttonSave;
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components = null;

public Form1()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();

//
// TODO: Add any constructor code after InitializeComponent call
//
}

/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}

#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.textBox1 = new System.Windows.Forms.TextBox();
this.textBox2 = new System.Windows.Forms.TextBox();
this.buttonClose = new System.Windows.Forms.Button();
this.buttonSave = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// textBox1
//
this.textBox1.Location = new System.Drawing.Point(32, 32);
this.textBox1.Name = "textBox1";
this.textBox1.TabIndex = 0;
this.textBox1.Text = "textBox1";
this.textBox1.TextChanged += new
System.EventHandler(this.formControlChanged);
//
// textBox2
//
this.textBox2.Location = new System.Drawing.Point(32, 72);
this.textBox2.Name = "textBox2";
this.textBox2.TabIndex = 1;
this.textBox2.Text = "textBox2";
this.textBox2.TextChanged += new
System.EventHandler(this.formControlChanged);
//
// buttonClose
//
this.buttonClose.Location = new System.Drawing.Point(192, 216);
this.buttonClose.Name = "buttonClose";
this.buttonClose.TabIndex = 2;
this.buttonClose.Text = "&Close";
this.buttonClose.Click += new
System.EventHandler(this.buttonClose_Click);
//
// buttonSave
//
this.buttonSave.Location = new System.Drawing.Point(112, 216);
this.buttonSave.Name = "buttonSave";
this.buttonSave.TabIndex = 3;
this.buttonSave.Text = "&Save";
this.buttonSave.Click += new

System.EventHandler(this.buttonSave_Click);
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(292, 266);
this.Controls.Add(this.buttonSave);
this.Controls.Add(this.buttonClose);
this.Controls.Add(this.textBox2);
this.Controls.Add(this.textBox1);
this.Name = "Form1";
this.Text = "Form1";
this.Closing += new
System.ComponentModel.CancelEventHandler(this.Form 1_Closing);
this.ResumeLayout(false);

}
#endregion

/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}

private void formControlChanged(object sender, System.EventArgs e)
{
this.dataChanged = true;
}

private void buttonClose_Click(object sender, System.EventArgs e)
{
this.Close();
}

private void Form1_Closing(object sender,
System.ComponentModel.CancelEventArgs e)
{
if (this.dataChanged)
{
e.Cancel = true;
MessageBox.Show("Warning! Data has changed and must be saved");
}

}

private void buttonSave_Click(object sender, System.EventArgs e)
{
//data saved
this.dataChanged = false;
}

}
}

"Dave" <dp*@nospam.eliassen.com> wrote in message
news:OQ**************@TK2MSFTNGP14.phx.gbl...
> Ok, but I'm using standard controls. Isn't there a standard notification > event that controls send to their parent in Windows Forms, notifying
> the
> parent that the control has changed? I'm pretty sure there must be an
> easy
> way to do this - there is in Win32, and I doubt they'd have gotten rid of > such basic functionality when it was wrapped with .net.
>
> "Bonj" <benjtaylor at hotpop d0t com> wrote in message
> news:%2****************@TK2MSFTNGP10.phx.gbl...
>> Have event handlers on the controls. Multiple controls can all call
>> the
> same
>> event handler.
>>
>> "Dave" <dp*@nospam.eliassen.com> wrote in message
>> news:uy**************@TK2MSFTNGP14.phx.gbl...
>> > How do I check in a Windows Forms app if any controls have changed? I >> > have
>> > a form that collects data, and I want to prompt the user if they try to >> > exit
>> > the app, or load a new file, without saving changes that have been
>> > made.
>> >
>> > In MFC/Win32, you'd trap the WM_COMMAND/EN_CHANGE notification
>> > messages,
>> > etc. But, this doesn't seem to happen in Windows Forms. I tried
> Spy-ing
>> > a
>> > windows forms app, and the WM_COMMAND messages don't even get sent (!). >> >
>> > Thanks!
>> >
>> > - Dave
>> >
>> >
>>
>>
>
>



Nov 16 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Web Developer | last post by:
Hi, I come across the term "type checking" very often in my readings on C++, and have never heard it in Java. Besides the simplistic answer that it checks the "type", what more does it mean? ...
6
by: Matthew | last post by:
How would I go about creating a simple "hello world" program that will run in Unix. I am using MS Visual C++.
1
by: RC | last post by:
If I want to explicitly save the record before executing a Close action. Which of the following should I use? Or does it depend on whether it is based on a Form or something else? If Me.Dirty...
15
by: aussie rules | last post by:
Hi, I have some really, really large forms with dozens of text boxs. What I want to do is somehow know that the user has edited a text box, so that if they close the form, i can ask them if...
7
by: Mitchell Vincent | last post by:
I've been trying to get a standard toolbar to play nice with some nice icons that I have. When I put them on a button or anything they look perfect, but through an imagelist and on a toolbar they...
43
by: Christoph Schneegans | last post by:
Hi! Okay, so positions on "text/html" XHTML are totally contradicting. Anyway! I hope there's more consensus about "application/xml" XHTML. I've recently learned that Opera 9.0b2 does not only...
10
by: morangolds | last post by:
Hi, I've been having a problem with C++ Windows Forms apps not "ending" when you close the form window. I've searched about this problem all over the place and most searches have lead me to...
23
by: deathtospam | last post by:
A day or two ago, I wrote a quick ASPX page with a CS codebehind using Visual Studio .NET 2005 -- it worked, I saved it and closed the project. Today, I came back to the project, reopened the...
6
by: JHNielson | last post by:
This is a very simple question.... I have a form that looks up Records for an unbound drop-down list. It has worked just fine up until last night. Now the button on the form to delete a record...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.