Hello,
How to validate tabcontrol pages?
Using the full .NET Framework I would suggest using the Validating event,
however, the .NET Compact Framework does not support the CausesValidation
property on Control so honestly I am not entirely sure when Validating gets
fired. I will check into this.
An alternative could be to use SelectedIndexChanged on the tab control. In
this event you could validate the last tab that was selected. I don't
believe there is a way to get the last index from the event args so you will
need to store the last selected tab. Here is some code that should help.
private int _currentTabIndex = 0;
private void tabControl1_SelectedIndexChanged(object sender,
System.EventArgs e) {
// Do not validate if the _currentTabIndex equals the selected
// index to avoid endless loop.
if (_currentTabIndex != tabControl1.SelectedIndex) {
// Validate Last Tab.
if (!IsTabValid(_currentTabIndex)) {
tabControl1.SelectedIndex = _currentTabIndex; // Will fire
SelectedIndexChanged event again.
MessageBox.Show("Something is wrong.");
}
// Set current tab index.
_currentTabIndex = tabControl1.SelectedIndex;
}
}
private bool IsTabValid(int tabIndex) {
switch(_currentTabIndex) {
case 0:
if (textBox1.Text.Length == 0) {
return false;
}
break;
case 1:
if (textBox2.Text.Length == 0) {
return false;
}
break;
}
return true;
}
Let me know how this works out for you.
Tom
--
Tom Krueger
Microsoft Corporation
Program Manager
http://weblogs.asp.net/tom_krueger
This posting is provided "AS IS" with no warranties, and confers no rights.
Use of included script samples are subject to the terms specified at
http://www.microsoft.com/info/cpyright.htm
A Gill said:
Hey Guys,
Does anybody know much about the tabcontrol. Seems very limited in what it
can do. Does anyone know how I would go about validating a tabpage when the
user clicks on the next tab button? And stop the user if the validation
fails.