R – How to access the value of bound control on a non-displayed tab for validation

data-bindingtabcontrolvalidationwinforms

I have created a dialog which contains 3 tabs. Controls on Tabs 1 & 2 have validation using the Validating() event. The user will mainly be working on Tabs 1 & 3, never displaying Tab 2 unless necessary. All controls on Tabs 1 & 2 are bound to a BindingSource object.

From my observation, it appears that the bound controls are not initialized on Tab 2 until the tab is displayed. As a result, validating for the entire form fails since those controls have no value. The TextBox.Text value is "" when Validating() is called the first time, and somevalue after I view the tab.

I tried to 'pre-initialize' the controls on Tab 2 from the Load event (e.g. TextBox.Value = 'test';, but found the value was cleared before Validating() was called.

I had thoughts about checking the value from BindingSource.Current, but this particular solution has multiple pitfalls, notably the BindingSource containing an unvalidated value.

What step(s) do I need to take to either:

  • initialize the controls before they are displayed
  • obtain the proper value (control or BindingSource) for Validation()

Best Answer

I solved this by programmatically changing to the Tab2 and then switching back to Tab1

        foreach (TabPage tab in tabControl.TabPages)
        {
            tab.Visible = true;
        }
        tabControl.SelectedIndex = 0;
Related Topic