473,320 Members | 1,744 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,320 software developers and data experts.

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

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
0 4997

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

Similar topics

1
by: Josh | last post by:
I'm stumped. I have three checkboxs on an Item of a datalist, they all post back. When I check one it unchecks the other two ( like a Radio Button). Each CheckedChanged Event sets the Checked...
1
by: Robains | last post by:
I'm using a TextBox_Leave event to trigger the saving of a value to my object, however, my object is created via the AfterSelect from a TreView control -- which is fine. BUT, the problem seems...
10
by: lux | last post by:
Hi, How can I capture the EVT_SET_FOCUS on a wxGrid? Tank's in advance Luca
4
by: =?Utf-8?B?TWlrZQ==?= | last post by:
Hi. If an ASP.NET page postsback, for example, after a button is clicked, and then I refresh the page (ie, by pressing F5 key), why does the same event occur when I press F5 as it did when I...
2
by: mbosco51 | last post by:
Hi, I am extremely new to .net, I am actually working on my first application so I apologize if there is an obvious answer.... Is there a got focus event that fires when a user clicks on a textbox?...
1
by: ravikumar2007 | last post by:
I am working with .NET 3.5 framework within an WPF application. I have a windows application where for a textbox, I have defined the lost focus event. I need to call the Focus() method of any other...
0
by: Builder | last post by:
I made a simple app to test Leave focus event and AutoComplete functionality on the text box control. I added 2 text box controls on the blank form. Then I added Leave focus event handlers for...
1
by: sandeepthachan | last post by:
I have written a C# class file with database connection and to retrieve the data through a GridView. But now i am not able to get the data throught that Class file in an Asp.net Page_load() event....
2
by: =?Utf-8?B?U2F2dm91bGlkaXMgSW9yZGFuaXM=?= | last post by:
Which is the event sequence when a gridview button (for update) is clicked and the page is post back? I am also a bit confused when to use ..IsPostBack(). If my page data is changing maybe in every...
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
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...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.