Windows Develop Bookmark and Share   
 index > Windows Forms Data Controls and Databinding > How to detect when a tab page is being selected?
 

How to detect when a tab page is being selected?

Hi all!

I have a tab control and in one tab page I have 8 textbox and 2 grids. The textbox are all numerics and have the following behavior: when I digit the 2nd textbox then the 3rd textbox is updated with the 2nd more the difference between the 1st and 2nd, and so on.

Example 1:
TB 1: 10, TB 2: 20 -> TB 3 will be updated with 30
Example 2:
TB 1: 15, TB 2: 30, TB 3: 45 -> TB 4 will be updated with 60

When I say "will be updated", it is updated on textbox_Validating() event.

If I'd like to digit just 4 textbox, I left the 5th with zero (or blank) and it works fine. If I am on the 4th textbox and then click on one of the grids, it is ok too. But if the focus is on the 4th textbox (with some value greater than 0), and I click on another page of the tab control, it is not working. The problem is that on textbox_Validating() event, where I have the following test:

private void textbox_Validating(object sender, CancelEventArgs e)
{
TextBox tb = sender as TextBox;
if (tb.Focused == true)
{
UpdateNextTextbox();
}
}

In UpdateNextTextbox() method I update the next number according I explained above and call Focus() event of the next textbox. When I click on the grid, the "if (tb.Focused == true)" is false and the next textbox will not be updated, and it is ok. The problem is that if I click on another tab page, this "if" is true, i.e., the focus is still on the textbox, and the next textbox is updated, what is wrong.

I already tried Selecting(), Deselecting(), SelectedIndexChanged() and other events of tab control, but all them are called after textbox_Validating(). So what I want is to know if it is changing tab page before textbox_Validating(). Any idea?

Thank you!
Vanessa

vnihues72  Tuesday, July 15, 2008 5:26 PM

Hi Vanessa,

Based on my understanding, the problem is we don’t want TextBox’s validating event triggered when switching to another page.

I have simulated your application and write a simple sample. With many times�tests, I got to find if we set the CauseValidation property of TabControl and the TabPage which contains the eight TextBox to false, then TextBox_Validating event will not be triggered when we switch to another page.

Code Snippet

this.tabControl1.CausesValidation = false;

this.tabPage1.CausesValidation = false;

If it doesn’t work, please feel free to contact me.

Best Regards,

Bruce Zhou

Bruce.Zhou  Monday, July 21, 2008 9:59 AM

Hi Vanessa,

Based on my understanding, the problem is we don’t want TextBox’s validating event triggered when switching to another page.

I have simulated your application and write a simple sample. With many times�tests, I got to find if we set the CauseValidation property of TabControl and the TabPage which contains the eight TextBox to false, then TextBox_Validating event will not be triggered when we switch to another page.

Code Snippet

this.tabControl1.CausesValidation = false;

this.tabPage1.CausesValidation = false;

If it doesn’t work, please feel free to contact me.

Best Regards,

Bruce Zhou

Bruce.Zhou  Monday, July 21, 2008 9:59 AM

You can use google to search for other answers

Custom Search

More Threads

• equivalent of Datagrid.CurrentRowIndex in DatagridView 2.0?
• DataGridView - how to prevent deselect in full-row single-select mode ?
• SelectionForeColor in DataGridViewLinkColumn
• Complicated DataGridView with Combobox problem
• datagrid
• Why is AddNewItem disabled???
• GetChanges?
• The v2 DatePicker doesn't support null databinding (bug?)
• Datagridview: Need to know when list data changed
• Open a new form with related record (in same table)