need HELP urgently example code included

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hi All,
Can anyone explain the behaviour of the program below, I have two textboxes and in thier
lostfocus event I am displaying a message box and then setting focus to the other textbox but it seems
to be firing the both textboxes lostfocus events when they have values set, WHY ??

enter 1 in the first textbox and 2 in the second texbox.

any help will be greatly appreicated.
THANK YOU.

using System;
using System.Drawing;
using System.Windows.Forms;


namespace test
{

public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.TextBox tb1;
private System.Windows.Forms.TextBox tb2;

private System.ComponentModel.Container components = null;

public Form1()
{
InitializeComponent();
}


protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}

#region Windows Form Designer generated code

private void InitializeComponent()
{
this.tb1 = new System.Windows.Forms.TextBox();
this.tb2 = new System.Windows.Forms.TextBox();
this.SuspendLayout();
//
// tb1
//
this.tb1.Location = new System.Drawing.Point(216, 80);
this.tb1.Name = "tb1";
this.tb1.TabIndex = 0;
this.tb1.Text = "";
//
// tb2
//
this.tb2.Location = new System.Drawing.Point(216, 128);
this.tb2.Name = "tb2";
this.tb2.TabIndex = 1;
this.tb2.Text = "";
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(608, 318);
this.Controls.Add(this.tb2);
this.Controls.Add(this.tb1);
this.Name = "Form1";
this.Text = "Form1";
this.Load += new System.EventHandler(this.Form1_Load);
this.ResumeLayout(false);

}
#endregion


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

private void Form1_Load(object sender, System.EventArgs e)
{
tb1.LostFocus+=new EventHandler(tb1_LostFocus);
tb2.LostFocus+=new EventHandler(tb2_LostFocus);
}


private void tb2_LostFocus(object sender, EventArgs e)
{
if (tb2.Text=="2")
{
MessageBox.Show("Example 2");
tb1.Focus();
}

}

private void tb1_LostFocus(object sender, EventArgs e)
{
if (tb1.Text=="1")
{
MessageBox.Show("Example 1" );
tb2.Focus();
}
}

}
}
 
Does it still happen when you don't show the messagebox?
If not then you are probibly loosing focus because of the messagebox.

But the other button should get focus when the message box disapears
:)

Hope this helps
 
Back
Top