GroupBox/DateTimePicker event order

gushie

Newcomer
Joined
Dec 24, 2004
Messages
6
Location
Suffolk, UK
I have a GroupBox which contains two DateTimePickers.
In the GroupBox validating event, I'm trying to check to see whether one DateTimePicker value is greater than the other and then set an ErrorProvider.

The problem I'm facing is that when I click off the DateTimePicker to somewhere outside it's parent groupbox, the GroupBox Validating event gets fired before the DateTimePicker Control has been updated. I.e. if I interrogate the DateTimePicker all the properties still contain the old values, even the 'Text'. This obviously makes the validation a bit difficult!

I can't seem to find an event on the GroupBox that does get fired after the DateTimePicker is updated... can anyone help?

Thanks,

Jonathan.
C# VS2003.
 
The original project is a bit big to post. I've written a smaller project which duplicates similar behaviour. I wasn't able to duplicate the clicking, but if you change the date and then press tab, the groupbox validate reports the old date.

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

namespace DTinGBTest
{
	/// <summary>
	/// Summary description for Form1.
	/// </summary>
	public class Form1 : System.Windows.Forms.Form
	{
		private System.Windows.Forms.GroupBox groupBox1;
		private System.Windows.Forms.GroupBox groupBox2;
		private System.Windows.Forms.TextBox textBox1;
		private System.Windows.Forms.DateTimePicker dateTimePicker1;
		private System.ComponentModel.Container components = null;

		public Form1()
		{
			InitializeComponent();
		}
		private void InitializeComponent()
		{
			this.groupBox1 = new System.Windows.Forms.GroupBox();
			this.dateTimePicker1 = new System.Windows.Forms.DateTimePicker();
			this.groupBox2 = new System.Windows.Forms.GroupBox();
			this.textBox1 = new System.Windows.Forms.TextBox();

			this.groupBox1.Controls.Add(this.dateTimePicker1);
			this.groupBox1.Location = new System.Drawing.Point(13, 15);
			this.groupBox1.Name = "groupBox1";
			this.groupBox1.Size = new System.Drawing.Size(177, 72);
			this.groupBox1.TabIndex = 0;
			this.groupBox1.Validating += new System.ComponentModel.CancelEventHandler(this.groupBox1_Validating);
			this.groupBox1.Validated += new System.EventHandler(this.groupBox1_Validated);
			this.groupBox1.Leave += new System.EventHandler(this.groupBox1_Leave);

			this.dateTimePicker1.Location = new System.Drawing.Point(28, 28);
			this.dateTimePicker1.Name = "dateTimePicker1";
			this.dateTimePicker1.Size = new System.Drawing.Size(136, 20);
			this.dateTimePicker1.TabIndex = 1;

			this.groupBox2.Controls.Add(this.textBox1);
			this.groupBox2.Location = new System.Drawing.Point(13, 98);
			this.groupBox2.Name = "groupBox2";
			this.groupBox2.Size = new System.Drawing.Size(614, 133);
			this.groupBox2.TabIndex = 1;

			this.textBox1.Location = new System.Drawing.Point(10, 19);
			this.textBox1.Multiline = true;
			this.textBox1.Name = "textBox1";
			this.textBox1.Size = new System.Drawing.Size(590, 102);
			this.textBox1.TabIndex = 1;

			this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
			this.ClientSize = new System.Drawing.Size(658, 249);
			this.Controls.Add(this.groupBox2);
			this.Controls.Add(this.groupBox1);
			this.Name = "Form1";
			this.Text = "Form1";

		}
		[STAThread]
		static void Main() 
		{
			Application.Run(new Form1());
		}

		private void groupBox1_Validating(object sender, System.ComponentModel.CancelEventArgs e)
		{
			textBox1.Text += "groupBox1_Validating: dateTimePicker1.Text is " + dateTimePicker1.Text + "\r\n";
		}

		private void groupBox1_Leave(object sender, System.EventArgs e)
		{
			textBox1.Text += "groupBox1_Leave:  dateTimePicker1.Text is " + dateTimePicker1.Text + "\r\n";
		
		}

		private void groupBox1_Validated(object sender, System.EventArgs e)
		{
			textBox1.Text += "groupBox1_Validated:  dateTimePicker1.Text is " + dateTimePicker1.Text + "\r\n";
		
		}
	}
}
 
Note, I have found a rather kludgy workaround which involves checking in the DateTimePicker.ValueChanged if the Form.ActiveControl is inside the GroupBox, and doing the range validation if not.
It would be a bit nicer if I could hook it into the groupbox properly though!
(Investigating further, I've found the DateTimePicker value/text isn't even updated in the Validating event of the DateTimePicker... !)
 
Back
Top