473,657 Members | 2,535 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 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");
}
}
}
Nov 16 '05 #1
0 5069

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

Similar topics

1
959
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 Value of the other two check boxes to false. Lets call the CheckBoxes "1","2" and "4".
1
2566
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 to be if my focus is on the TextBox and I click on a TreView control node the sequence of events seems to be: TreView_AfterSelect TextBox_Leave
10
4640
by: lux | last post by:
Hi, How can I capture the EVT_SET_FOCUS on a wxGrid? Tank's in advance Luca
4
6720
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 clicked the button? For example - On an ASP.NET webform I click a button which produces a postback and a Button.Click event. After the postback, I press F5 to refresh my webform, which executes Button.Click again. Is there a way to prevent the...
2
6410
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? Upon clicking on the textbox (which is disabled), I want a calendar control to be displayed. Is this possible? Thanks.
1
4750
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 control in a scenario where the first textbox's lost focus is being called automatically since we are moving out of the control. Is there any way to call Focus() method without calling the lost focus event handler for this textbox? I have tried...
0
1655
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 these text boxes, set AutoCompleteMode property to SuggestAppend and AutoCompleteSource property set to CustomSource. AutoCompleteCustomSource property is changed in a Leave focus event handlers for each text box, see code below. When I run the...
1
1124
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. Does anybody knows the codings to dispaly the class details of the same. My class Code is Like this: public void Get_Data() { SqlConnection SConn =new SqlConnection("ConnectionStringl"); SConn.Open(); DataSet Ds = new...
2
1441
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 10 or 20 seconds, should I use .IsPostback? If not, then the gridviews databinding in the page should be written in Page_Load or in the other events like SelectedIndexChanged, RowCommand where the click is handled? TIA Iordanis
0
8324
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8740
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8513
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8617
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7352
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
4330
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2742
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1970
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1733
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.