My purpose is that the users will never be able to check a TCheckBox when the number is entered into a TEdit less than 7 digits. Also, this TCheckBox can never be checked when the TEdit is being empty.
A problem of my codes is sometimes TCheckBox can still be checked although TEdit is being empty.
Moreover, my another target is that the start button can never be executed or will always display an error message if the start button is clicked when the TCheckBox is checked while the TEdit is being empty.
The problem is what codes should I put in the start button ?.
I am using the following code:
//--------------------------------------------------------------------------------
void __fastcall TForm::MyTEditBoxKeyPress(TObject *Sender, System::WideChar &Key)
{
if( Key == VK_BACK ) return;
if((Key < '1') || (Key > '9'))
{
MessageDlg("Please enter number only.",mtInformation, TMsgDlgButtons()<< mbOK, 0);
Key = 0;
}
}
//--------------------------------------------------------------------------------
void __fastcall TForm::MyTEditBoxExit(TObject *Sender)
{
if (MyTEditBox->Text.Length() < 7) {
MessageDlg("Please enter at least 7 digit.",mtInformation, TMsgDlgButtons()<< mbOK, 0);
}
}
//--------------------------------------------------------------------------------
void __fastcall TForm::MyCheckBoxClick(TObject *Sender)
{
if (MyCheckBox->Tag == 0 ) {
MyCheckBox->Tag = 1;
if (MyTEditBox->Text.Length() >= 7)
MyCheckBox->Checked = true;
IdThrottler->BitsPerSec = StrToInt64(MyTEditBox->Text);
}
else {
MyCheckBox->Tag = 0;
if (MessageDlg("Please enter at least 7 digit.",mtInformation, TMsgDlgButtons()<< mbOK, 0) == mrYes)
MyCheckBox->Checked = false;
}
}
First off, the throttler's
BitsPerSecproperty is anint, not an__int64, so you should be usingStrtoInt()instead ofStrToInt64().You are setting the
TCheckBox::Enabledproperty in theTCheckBox::OnClickevent, so the user has to actually click on theTCheckBoxto make it update itself. If the user only typed in theTEditand never clicks on theTCheckBox, it will never be updated.If you don't want the user to click on the
TCheckBoxat all unless theTEdittext is adequate, you should use theTEdit::OnChangeevent to set theTCheckBox::Enabledproperty, and get rid of yourTCheckBox::Taghandling altogether:Do note that just because the user can type in more than 6 digits does not mean its
Textrepresents a valueintvalue. In that situation,StrToInt()will raise an exception.A different way to handle this is to add a
TActionListto your Form, create a custom action in it, assign that action to theTCheckBox::Actionproperty, and then use theTAction::OnUpdateevent to set theTAction::Enabledproperty (which will enable/disable theTCheckBox):The benefit of this approach is that the
TCheckBox::Enabledproperty will be updated automatically and in real-time without having to manually react to changes in theTEditat all.With that said, if you are using a modern version of C++Builder,
TEdithas aNumbersOnlyproperty. When set to true, you don't have to filter keystrokes in theTEdit::OnKeyPressevent anymore, the OS will prevent the user from typing non-digit characters for you (besides, when you are filtering manually, you are not allowing the user to type in0digits, which is wrong).If you really must allow the user to enter a number via a
TEdit, and if theTEdit::NumbersOnlyproperty is not available in your version of C++Builder, you still have a couple of other options (which you should consider anyway, even in modern C++Builder versions):make the
TEditread-only, attach aTUpDownto it via theTUpDown::Associateproperty, and assign appropriateTUpDown::MinandTUpDown::Maxvalues as needed. Use theTUpDown::Positionproperty to update the throttler'sBitsPerSecproperty:Maybe also use a
TTrackBarthat sets theTUpDown::Valueproperty in larger increments so the user does not have to press the up/down arrows for more than small adjustments:Don't bother using a
TEditat all. Use aTCSpinEditorTSpinEditinstead (depending on your version of C++Builder). The user can type in numbers, and it will reject non-numeric input. It provides up/down arrows, likeTUpDown, for making small adjustments. And it has aValueproperty that returns/accepts anintinstead of aString, just like theTUpDown::Positionproperty.Either way, the user cannot enter non-numeric values at all, and the
TCheckBoxstill auto-disables itself for values that are smaller than your desired threshold.