By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,412 Members | 2,882 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,412 IT Pros & Developers. It's quick & easy.

c# (or .NET) focus event sequence is wrong

P: n/a
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 InitializeComponent() call is required for Windows Forms
designer support.
//
InitializeComponent();

//
// TODO: Add constructor code after the InitializeComponent() call.
//
this.textBox1.GotFocus += new
System.EventHandler(this.TextBox1GotFocus);
this.textBox2.GotFocus += new
System.EventHandler(this.TextBox2GotFocus);
this.textBox3.GotFocus += new
System.EventHandler(this.TextBox3GotFocus);
this.button1.GotFocus += new
System.EventHandler(this.Button1GotFocus);
this.textBox1.LostFocus += new
System.EventHandler(this.TextBox1LostFocus);
this.textBox2.LostFocus += new
System.EventHandler(this.TextBox2LostFocus);
this.textBox3.LostFocus += new
System.EventHandler(this.TextBox3LostFocus);
this.button1.LostFocus += new
System.EventHandler(this.Button1LostFocus);

}

[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 InitializeComponent() {
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.SuspendLayout();
//
// listBox1
//
this.listBox1.Location = new System.Drawing.Point(32, 88);
this.listBox1.Name = "listBox1";
this.listBox1.Size = new System.Drawing.Size(224, 251);
this.listBox1.TabIndex = 4;
//
// textBox2
//
this.textBox2.Location = new System.Drawing.Point(8, 32);
this.textBox2.Name = "textBox2";
this.textBox2.TabIndex = 2;
this.textBox2.Text = "";
this.textBox2.Validating += new
System.ComponentModel.CancelEventHandler(this.Text Box2Validating);
this.textBox2.TextChanged += new
System.EventHandler(this.TextBox2TextChanged);
this.textBox2.Leave += new
System.EventHandler(this.TextBox2Leave);
this.textBox2.Enter += new
System.EventHandler(this.TextBox2Enter);
//
// textBox3
//
this.textBox3.Location = new System.Drawing.Point(8, 56);
this.textBox3.Name = "textBox3";
this.textBox3.TabIndex = 3;
this.textBox3.Text = "";
this.textBox3.Validating += new
System.ComponentModel.CancelEventHandler(this.Text Box3Validating);
this.textBox3.TextChanged += new
System.EventHandler(this.TextBox3TextChanged);
this.textBox3.Leave += new
System.EventHandler(this.TextBox3Leave);
this.textBox3.Enter += new
System.EventHandler(this.TextBox3Enter);
//
// textBox1
//
this.textBox1.Location = new System.Drawing.Point(8, 8);
this.textBox1.Name = "textBox1";
this.textBox1.TabIndex = 0;
this.textBox1.Text = "";
this.textBox1.Validating += new
System.ComponentModel.CancelEventHandler(this.Text Box1Validating);
this.textBox1.Leave += new
System.EventHandler(this.TextBox1Leave);
this.textBox1.Enter += new
System.EventHandler(this.TextBox1Enter);
//
// button1
//
this.button1.Location = new System.Drawing.Point(120, 8);
this.button1.Name = "button1";
this.button1.TabIndex = 1;
this.button1.Text = "button1";
this.button1.Enter += new
System.EventHandler(this.Button1Enter);
this.button1.Leave += new
System.EventHandler(this.Button1Leave);
//
// MainForm
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(292, 365);
this.Controls.Add(this.listBox1);
this.Controls.Add(this.textBox3);
this.Controls.Add(this.textBox2);
this.Controls.Add(this.button1);
this.Controls.Add(this.textBox1);
this.Name = "MainForm";
this.Text = "MainForm";
this.ResumeLayout(false);
}
#endregion
void TextBox1Enter(object sender, System.EventArgs e)
{
//this.textBox1.BackColor = System.Drawing.Color.Red;
listBox1.Items.Add("TextBox1 Enter");
}

void TextBox2TextChanged(object sender, System.EventArgs e)
{
//this.textBox2.BackColor = System.Drawing.Color.Red;

}

void TextBox3Enter(object sender, System.EventArgs e)
{
//this.textBox3.BackColor = System.Drawing.Color.Red;
listBox1.Items.Add("TextBox3 Enter");

}

void TextBox1Leave(object sender, System.EventArgs e)
{
//this.textBox1.BackColor = System.Drawing.Color.White;
listBox1.Items.Add("TextBox1 Leave");

}

void TextBox2Leave(object sender, System.EventArgs e)
{
//this.textBox2.BackColor = System.Drawing.Color.White;
listBox1.Items.Add("TextBox2 Leave");

}

void TextBox3TextChanged(object sender, System.EventArgs e)
{
//this.textBox3.BackColor = System.Drawing.Color.White;

}

void TextBox2Enter(object sender, System.EventArgs e)
{
listBox1.Items.Add("TextBox2 Enter");

}

void Button1Enter(object sender, System.EventArgs e)
{
listBox1.Items.Add("Button1 Enter");

}

void Button1Leave(object sender, System.EventArgs e)
{
listBox1.Items.Add("Button1 Leave");

}

void TextBox1Validating(object sender,
System.ComponentModel.CancelEventArgs e)
{
listBox1.Items.Add("TextBox1 Validating");
if (textBox1.Text == "") e.Cancel = true;

}

void TextBox2Validating(object sender,
System.ComponentModel.CancelEventArgs e)
{
listBox1.Items.Add("TextBox2 Validating");
if (textBox2.Text == "") e.Cancel = true;
}

void TextBox3Validating(object sender,
System.ComponentModel.CancelEventArgs e)
{
listBox1.Items.Add("TextBox3 Validating");
if (textBox3.Text == "") e.Cancel = true;
}

void TextBox3Leave(object sender, System.EventArgs e)
{
listBox1.Items.Add("TextBox3 Leave");
}

void TextBox1GotFocus(object sender, System.EventArgs e)
{
listBox1.Items.Add("TextBox1 GotFocus");
}
void TextBox2GotFocus(object sender, System.EventArgs e)
{
listBox1.Items.Add("TextBox2 GotFocus");
}
void TextBox3GotFocus(object sender, System.EventArgs e)
{
listBox1.Items.Add("TextBox3 GotFocus");
}
void Button1GotFocus(object sender, System.EventArgs e)
{
listBox1.Items.Add("Button1 GotFocus");
}
void TextBox1LostFocus(object sender, System.EventArgs e)
{
listBox1.Items.Add("TextBox1 LostFocus");
}

void TextBox2LostFocus(object sender, System.EventArgs e)
{
listBox1.Items.Add("TextBox2 LostFocus");
}
void TextBox3LostFocus(object sender, System.EventArgs e)
{
listBox1.Items.Add("TextBox3 LostFocus");
}
void Button1LostFocus(object sender, System.EventArgs e)
{
listBox1.Items.Add("Button1 LostFocus");
}
}
}
Nov 16 '05 #1
Share this question for a faster answer!
Share on Google+

This discussion thread is closed

Replies have been disabled for this discussion.