Search code examples
c#winformsbuttontabpage

Windows Form tabpage event


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


Solution

  • 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);