The employee dialog has fields that must be filled in inorder that the information will be complete. You want to make sure that the user does not press the ok button unless these items are filled in. This is accomplished by disabling the ok button until the items have been filled in. Notice that no checking is made here to see that numeric items are in fact numeric, since you should not allow items to be of an inappropriate type. You can restrict input by using a TMaskEdit box for items such as phone numbers or just restrict input by overriding the KeyPressed handler.
Once you have identified those items you will require, you must check to see their state everytime they are changed. So if we require the first name, last name, and the hours if hourly is checked, you will create a handler for the changed event for each of these items. In each case the handler will set the Enabled property of the Ok button. The actual test will be whether the first name and the last name are no longer blank and if the hours button is down if the hours are not blank. Another way to look at the last condition is that either the flat rate button is pressed or the hours field is not blank.
Since each time one of the required fields or buttons changes the same test is made, you only need one function to make these tests. The following function is used in each case.
void __fastcallTEmployeeInfoDlg::OkEnabler()
{
OKBtn->Enabled = FirstName->Text !="" &&
LastName->Text != "" &&
(FlatRate->Checked == true ||
Hours->Text != "");
}
![]() |
First click once on the item you want to create a handler for. Next look at the Events tab in the Object inspector. The first item on this page is the OnChange handler. Simply click on the emply box to create a handler. Then call OkEnabler from this function. |
Click here to see the video for this process.
When the user clicks on the flat rate button, you want to indicate that the user does not have to fill in the hours worked area. You do this in two ways. First, by changing the color of the title of this box to gray, and second by not allowing anything to be entered into this box. Since this occurs on the button clicked which is also where the ok button enabler is called, you need only add code to the function you already have. Similarly if the user clicks on the hourly button, you want to reactive the hours TEditBox and indicate this by setting the color back to black (its normal color).
|
void __fastcall TEmployeeInfoDlg::FlatRateClick(TObject *Sender)
{
Hours->Enabled = false;
GroupBox4->Enabled = false;
GroupBox4->Font->Color = clGray;
OkEnabler();
}
//---------------------------------------------------------------------------
void __fastcall TEmployeeInfoDlg::HourlyClick(TObject *Sender)
{
Hours->Enabled = true;
GroupBox4->Enabled = true;
GroupBox4->Font->Color = clBlack;
OkEnabler();
} |
Click here to see the video for this process.