2
votes

I have a windows form application , In which i created two tab pages. In one of the tab page i have a button to send email notification. In the tabpage leave event i have some code to perform some other actions. When i click on this button to send email. First it fires tabpage leave event , as ithe button contains button1.enabled=false; in the first line as below,

 private void btnTestEmail_Click(object sender, EventArgs e)
        {
            btnTestEmail.Enabled = false;
            bool sent = Support.SendEmail("Test Email", "This is a test email, Please ignore.", null);
--
}

But when i remove btnTestEmail.Enabled = false; code it is not firing tabpage leave event. What could be the reason that it fires the leave event of tab page. As it is vbery strange behaviour. As i dont want to fire any event of tab page .

Regards

2
do you have any other control in that tab page - Sathish
Yes, textboxes dropdown, button controls, check boxes. - Sangeetha
before set button enabled focus move to another control. its alternate way - Sathish
By setting the Enabled property to false, you force Winforms to find another control to give the focus to. And if that other control is not on the tab page then the Leave event is going to fire. You'll want to move code to a separate method that you can call both from the Click as well as the Leave event handler. Perhaps. - Hans Passant

2 Answers

0
votes

Changing btnTestEmail.Enabled to false will change the ActiveControl, which fires the Leave event.

According to MSDN:

When you change the focus by using the keyboard (TAB, SHIFT+TAB, and so on), by calling the Select or SelectNextControl methods, or by setting the ContainerControl.ActiveControl property to the current form, focus events occur in the following order:

  1. Enter

  2. GotFocus

  3. Leave

  4. Validating

  5. Validated

  6. LostFocus

What you can do:

What I would do to eliminate this behavior is unsubscribing the Leave event and re-subscribing it after setting the Enabled property to false.

Like this:

this.tabPage1.Leave -= new System.EventHandler(this.tabPage1_Leave);

btnTestEmail.Enabled = false;

this.tabPage1.Leave += new System.EventHandler(this.tabPage1_Leave);
0
votes

The Problem is, that the Leave event fires if the control isnt the active control. If you click the Button the TabPage changes from active to inactive because the Button is active control now.