views:

156

answers:

2

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()
A: 

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;
It is a solution, but not ideal, due to what I consider an form appearance to the user. It did however open up a few more doors in my search for an answer to my question.
KDrewiske
A: 

While I couldn't find any authoritative sources to confirm, it appears that bound controls are not initialized until they are made visible. I was able to confirm similar behavior on my default displayed tab by hiding a text box. The control's .Text value was "" until the control was made visible, at which time the value was populated with my expected value.

A question on Microsoft forums further confirmed what I was experiencing:

This is the way .NET data binding works: the binding is nonfunctional until the control first becomes visible. ... There is no workaround short of making the control visible temporarily.

KDrewiske