private:
void AddHandlers()
{
// Add the Validating and Validated handlers for textboxes.
myTextBox1->Validating += gcnew System::ComponentModel::CancelEventHandler( this, &Form1::myTextBox1_Validating );
myTextBox1->Validated += gcnew System::EventHandler( this, &Form1::myTextBox1_Validated );
myTextBox2->Validating += gcnew System::ComponentModel::CancelEventHandler( this, &Form1::myTextBox2_Validating );
myTextBox2->Validated += gcnew System::EventHandler( this, &Form1::myTextBox2_Validated );
myTextBox1->CausesValidationChanged += gcnew System::EventHandler( this, &Form1::myTextBox1_CausesValidationChanged );
myTextBox2->CausesValidationChanged += gcnew System::EventHandler( this, &Form1::myTextBox2_CausesValidationChanged );
if ( myTextBox1->CausesValidation == true && myTextBox2->CausesValidation == true )
{
button1->Text = "Disable Validation";
myLabel->Text = "Validation Enabled";
this->Focus();
}
}
void myTextBox1_Validating( Object^ /*sender*/, System::ComponentModel::CancelEventArgs^ e )
{
if ( !CheckIfTextBoxNumeric( myTextBox1 ) )
{
myLabel->Text = "Has to be numeric";
e->Cancel = true;
}
}
void myTextBox1_Validated( Object^ /*sender*/, System::EventArgs^ /*e*/ )
{
myLabel->Text = "Validated first control";
}
void myTextBox2_Validating( Object^ /*sender*/, System::ComponentModel::CancelEventArgs^ e )
{
if ( !CheckIfTextBoxNumeric( myTextBox2 ) )
{
myLabel->Text = "Has to be numeric";
e->Cancel = true;
}
}
void myTextBox2_Validated( Object^ /*sender*/, System::EventArgs^ /*e*/ )
{
myLabel->Text = "Validated second control";
}
void myTextBox1_CausesValidationChanged( Object^ /*sender*/, System::EventArgs^ /*e*/ )
{
myLabel->Text = "CausesValidation property was changed for First Textbox";
}
void myTextBox2_CausesValidationChanged( Object^ /*sender*/, System::EventArgs^ /*e*/ )
{
myLabel->Text = "CausesValidation property was changed for Second Textbox";
}
bool CheckIfTextBoxNumeric( TextBox^ myTextBox1 )
{
bool isValid = true;
if ( myTextBox1->Text->Equals( "" ) )
{
isValid = false;
}
else
{
for ( int i = 0; i < myTextBox1->Text->Length; i++ )
{
if ( !(System::Char::IsNumber( myTextBox1->Text[ i ] )) )
{
myTextBox1->Text = "";
isValid = false;
break;
}
}
}
return isValid;
}
void myButtonAdd_Click( Object^ /*sender*/, System::EventArgs^ /*e*/ )
{
try
{
int result = Convert::ToInt32( myTextBox1->Text ) + Convert::ToInt32( myTextBox2->Text );
myLabel->Text = result.ToString();
}
catch ( Exception^ myException )
{
myLabel->Text = "Exception : ",myException->Message;
}
}
void button1_Click( Object^ /*sender*/, System::EventArgs^ /*e*/ )
{
if ( myTextBox1->CausesValidation == false && myTextBox2->CausesValidation == false )
{
myTextBox1->CausesValidation = true;
myTextBox2->CausesValidation = true;
button1->Text = "Disable Validation";
myLabel->Text = "Validation Enabled";
}
else
if ( myTextBox1->CausesValidation == true && myTextBox2->CausesValidation == true )
{
myTextBox1->CausesValidation = false;
myTextBox2->CausesValidation = false;
button1->Text = "Enable Validation";
myLabel->Text = "Validation Disabled";
}
}