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
BitsPerSec
property is anint
, not an__int64
, so you should be usingStrtoInt()
instead ofStrToInt64()
.You are setting the
TCheckBox::Enabled
property in theTCheckBox::OnClick
event, so the user has to actually click on theTCheckBox
to make it update itself. If the user only typed in theTEdit
and never clicks on theTCheckBox
, it will never be updated.If you don't want the user to click on the
TCheckBox
at all unless theTEdit
text is adequate, you should use theTEdit::OnChange
event to set theTCheckBox::Enabled
property, and get rid of yourTCheckBox::Tag
handling altogether:Do note that just because the user can type in more than 6 digits does not mean its
Text
represents a valueint
value. In that situation,StrToInt()
will raise an exception.A different way to handle this is to add a
TActionList
to your Form, create a custom action in it, assign that action to theTCheckBox::Action
property, and then use theTAction::OnUpdate
event to set theTAction::Enabled
property (which will enable/disable theTCheckBox
):The benefit of this approach is that the
TCheckBox::Enabled
property will be updated automatically and in real-time without having to manually react to changes in theTEdit
at all.With that said, if you are using a modern version of C++Builder,
TEdit
has aNumbersOnly
property. When set to true, you don't have to filter keystrokes in theTEdit::OnKeyPress
event 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 in0
digits, which is wrong).If you really must allow the user to enter a number via a
TEdit
, and if theTEdit::NumbersOnly
property 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
TEdit
read-only, attach aTUpDown
to it via theTUpDown::Associate
property, and assign appropriateTUpDown::Min
andTUpDown::Max
values as needed. Use theTUpDown::Position
property to update the throttler'sBitsPerSec
property:Maybe also use a
TTrackBar
that sets theTUpDown::Value
property 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
TEdit
at all. Use aTCSpinEdit
orTSpinEdit
instead (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 aValue
property that returns/accepts anint
instead of aString
, just like theTUpDown::Position
property.Either way, the user cannot enter non-numeric values at all, and the
TCheckBox
still auto-disables itself for values that are smaller than your desired threshold.