Hi all.
I felt frustrated when developing an 'UserControl' derivated from
textBox, because sequence event (and Validate event) seems to fail.
I Always thought my code was wrong. But after trying the UserControl a
lot of hours I have the feeling that c# (or .Net) fails.
Thus, I have developed a simple program with 3 textBox, 1 Botton and 1
ListBox (for showing results) which fire OnEnter, OnGotFocus,
OnValidate, OnLostFocus and OnLeave.
OnValidate checks that the TextBox is not empty for Focus Change.
At the end of this mail I add the source program in case that anybody
wants to check it.
The result is unbelievable (it fails a lot)
When programs run, We can see in Listbox
TextBox1 Enter
TextBox1 Gotfocus
Now, with the mouse I click in TextBox2, and appears
TextBox1 LostFocus
TextBox1 Leave
TextBox1 Validating (first surprise this event is fired after
LostFocus and Leave ???)
TextBox1 Enter
Text2Box LostFocus (Second surprise, how is posible LostFocus in
TextBox2, where is TextBox2 GotFocus'?)
TextBox1 GotFocus
I understand nothing I read MSN and I can see Microsoft
Enter
GotFocus
Leave
Validating
Validated
LostFocus
I think this is also wrong the right event sequence may be (my idea).
Enter
GotFocus
now I press Tab (or click on other control).
Validating
if Validation is Wrong do nothing (Focus continues on TextBox1)
if validation is Ok
LostFocus
Leave
Enter (second control)
GotFocus (second control)
Other considerations
in Validation I need know which control will be receive the focus,
and if I want, cancel.
My question.
Now (when I write this e-mail I was looking in google, I have seen
many people have Focus/Validation problems).
Do you know if this have solution. Or Microsoft left this wrong
forever?.
Sorry for my english.
Miquel (a focus frustrated number 13.256.789 )
thanks.
*************** *************** *************** **
using System;
using System.Drawing;
using System.Windows. Forms;
namespace PrPrue
{
/// <summary>
/// Description of MainForm.
/// </summary>
public class MainForm : System.Windows. Forms.Form
{
private System.Windows. Forms.Button button1;
private System.Windows. Forms.TextBox textBox1;
private System.Windows. Forms.TextBox textBox3;
private System.Windows. Forms.TextBox textBox2;
private System.Windows. Forms.ListBox listBox1;
public MainForm()
{
//
// The InitializeCompo nent() call is required for Windows Forms
designer support.
//
InitializeCompo nent();
//
// TODO: Add constructor code after the InitializeCompo nent() call.
//
this.textBox1.G otFocus += new
System.EventHan dler(this.TextB ox1GotFocus);
this.textBox2.G otFocus += new
System.EventHan dler(this.TextB ox2GotFocus);
this.textBox3.G otFocus += new
System.EventHan dler(this.TextB ox3GotFocus);
this.button1.Go tFocus += new
System.EventHan dler(this.Butto n1GotFocus);
this.textBox1.L ostFocus += new
System.EventHan dler(this.TextB ox1LostFocus);
this.textBox2.L ostFocus += new
System.EventHan dler(this.TextB ox2LostFocus);
this.textBox3.L ostFocus += new
System.EventHan dler(this.TextB ox3LostFocus);
this.button1.Lo stFocus += new
System.EventHan dler(this.Butto n1LostFocus);
}
[STAThread]
public static void Main(string[] args)
{
Application.Run (new MainForm());
}
#region Windows Forms Designer generated code
/// <summary>
/// This method is required for Windows Forms designer support.
/// Do not change the method contents inside the source code editor.
The Forms designer might
/// not be able to load this method if it was changed manually.
/// </summary>
private void InitializeCompo nent() {
this.listBox1 = new System.Windows. Forms.ListBox() ;
this.textBox2 = new System.Windows. Forms.TextBox() ;
this.textBox3 = new System.Windows. Forms.TextBox() ;
this.textBox1 = new System.Windows. Forms.TextBox() ;
this.button1 = new System.Windows. Forms.Button();
this.SuspendLay out();
//
// listBox1
//
this.listBox1.L ocation = new System.Drawing. Point(32, 88);
this.listBox1.N ame = "listBox1";
this.listBox1.S ize = new System.Drawing. Size(224, 251);
this.listBox1.T abIndex = 4;
//
// textBox2
//
this.textBox2.L ocation = new System.Drawing. Point(8, 32);
this.textBox2.N ame = "textBox2";
this.textBox2.T abIndex = 2;
this.textBox2.T ext = "";
this.textBox2.V alidating += new
System.Componen tModel.CancelEv entHandler(this .TextBox2Valida ting);
this.textBox2.T extChanged += new
System.EventHan dler(this.TextB ox2TextChanged) ;
this.textBox2.L eave += new
System.EventHan dler(this.TextB ox2Leave);
this.textBox2.E nter += new
System.EventHan dler(this.TextB ox2Enter);
//
// textBox3
//
this.textBox3.L ocation = new System.Drawing. Point(8, 56);
this.textBox3.N ame = "textBox3";
this.textBox3.T abIndex = 3;
this.textBox3.T ext = "";
this.textBox3.V alidating += new
System.Componen tModel.CancelEv entHandler(this .TextBox3Valida ting);
this.textBox3.T extChanged += new
System.EventHan dler(this.TextB ox3TextChanged) ;
this.textBox3.L eave += new
System.EventHan dler(this.TextB ox3Leave);
this.textBox3.E nter += new
System.EventHan dler(this.TextB ox3Enter);
//
// textBox1
//
this.textBox1.L ocation = new System.Drawing. Point(8, 8);
this.textBox1.N ame = "textBox1";
this.textBox1.T abIndex = 0;
this.textBox1.T ext = "";
this.textBox1.V alidating += new
System.Componen tModel.CancelEv entHandler(this .TextBox1Valida ting);
this.textBox1.L eave += new
System.EventHan dler(this.TextB ox1Leave);
this.textBox1.E nter += new
System.EventHan dler(this.TextB ox1Enter);
//
// button1
//
this.button1.Lo cation = new System.Drawing. Point(120, 8);
this.button1.Na me = "button1";
this.button1.Ta bIndex = 1;
this.button1.Te xt = "button1";
this.button1.En ter += new
System.EventHan dler(this.Butto n1Enter);
this.button1.Le ave += new
System.EventHan dler(this.Butto n1Leave);
//
// MainForm
//
this.AutoScaleB aseSize = new System.Drawing. Size(5, 13);
this.ClientSize = new System.Drawing. Size(292, 365);
this.Controls.A dd(this.listBox 1);
this.Controls.A dd(this.textBox 3);
this.Controls.A dd(this.textBox 2);
this.Controls.A dd(this.button1 );
this.Controls.A dd(this.textBox 1);
this.Name = "MainForm";
this.Text = "MainForm";
this.ResumeLayo ut(false);
}
#endregion
void TextBox1Enter(o bject sender, System.EventArg s e)
{
//this.textBox1.B ackColor = System.Drawing. Color.Red;
listBox1.Items. Add("TextBox1 Enter");
}
void TextBox2TextCha nged(object sender, System.EventArg s e)
{
//this.textBox2.B ackColor = System.Drawing. Color.Red;
}
void TextBox3Enter(o bject sender, System.EventArg s e)
{
//this.textBox3.B ackColor = System.Drawing. Color.Red;
listBox1.Items. Add("TextBox3 Enter");
}
void TextBox1Leave(o bject sender, System.EventArg s e)
{
//this.textBox1.B ackColor = System.Drawing. Color.White;
listBox1.Items. Add("TextBox1 Leave");
}
void TextBox2Leave(o bject sender, System.EventArg s e)
{
//this.textBox2.B ackColor = System.Drawing. Color.White;
listBox1.Items. Add("TextBox2 Leave");
}
void TextBox3TextCha nged(object sender, System.EventArg s e)
{
//this.textBox3.B ackColor = System.Drawing. Color.White;
}
void TextBox2Enter(o bject sender, System.EventArg s e)
{
listBox1.Items. Add("TextBox2 Enter");
}
void Button1Enter(ob ject sender, System.EventArg s e)
{
listBox1.Items. Add("Button1 Enter");
}
void Button1Leave(ob ject sender, System.EventArg s e)
{
listBox1.Items. Add("Button1 Leave");
}
void TextBox1Validat ing(object sender,
System.Componen tModel.CancelEv entArgs e)
{
listBox1.Items. Add("TextBox1 Validating");
if (textBox1.Text == "") e.Cancel = true;
}
void TextBox2Validat ing(object sender,
System.Componen tModel.CancelEv entArgs e)
{
listBox1.Items. Add("TextBox2 Validating");
if (textBox2.Text == "") e.Cancel = true;
}
void TextBox3Validat ing(object sender,
System.Componen tModel.CancelEv entArgs e)
{
listBox1.Items. Add("TextBox3 Validating");
if (textBox3.Text == "") e.Cancel = true;
}
void TextBox3Leave(o bject sender, System.EventArg s e)
{
listBox1.Items. Add("TextBox3 Leave");
}
void TextBox1GotFocu s(object sender, System.EventArg s e)
{
listBox1.Items. Add("TextBox1 GotFocus");
}
void TextBox2GotFocu s(object sender, System.EventArg s e)
{
listBox1.Items. Add("TextBox2 GotFocus");
}
void TextBox3GotFocu s(object sender, System.EventArg s e)
{
listBox1.Items. Add("TextBox3 GotFocus");
}
void Button1GotFocus (object sender, System.EventArg s e)
{
listBox1.Items. Add("Button1 GotFocus");
}
void TextBox1LostFoc us(object sender, System.EventArg s e)
{
listBox1.Items. Add("TextBox1 LostFocus");
}
void TextBox2LostFoc us(object sender, System.EventArg s e)
{
listBox1.Items. Add("TextBox2 LostFocus");
}
void TextBox3LostFoc us(object sender, System.EventArg s e)
{
listBox1.Items. Add("TextBox3 LostFocus");
}
void Button1LostFocu s(object sender, System.EventArg s e)
{
listBox1.Items. Add("Button1 LostFocus");
}
}
}