그래서 Edit에는 KeyPress 이벤트가 있습니다.
void __fastcall TForm1::Edit1KeyPress(TObject *Sender, char &Key)
{
if((Key<'0' || Key>'9')&& Key!=8) Key=0;
}
요러면 숫자가 아닌건 다 무시되고 Backspace까진 먹어요.
이민준 님이 쓰신 글 :
: 감사합니다. 드디어 원활히 실행이 되네요 !!
: 마지막으로 숫자대신 알파벳이나 문자를 입력할 경우 0으로 바뀌도록 해야하는데
: 일단 알파벳 입력할 경우라도 해결하게 cctype헤더파일에 있는 isalpha()를 이용하여 아래와같이 해봤는데
: 여의치 않네요.. 도와주세요ㅠ
:
:
: -------------------------------------------------------------------------------------: :
: : :
: : : : :
: : :
: : : void __fastcall TForm1::Button1Click(TObject *Sender)
: {
: double no1,no2;
: AnsiString oprator;
:
: no1=num1->Text.ToDouble();
: no2=num2->Text.ToDouble();
: oprator=oprtor->Text;
:
:
: if (isalpha(no1))
: {no1=0;}
: if (isalpha(no2))
: {no2=0;}
:
:
:
: if (oprator=='+') {
:
: this->result->Caption=AnsiString(no1+no2);
:
: }
:
: else if (oprator=='-') {
: this->result->Caption=AnsiString(no1-no2);
: }
: else if (oprator=='*') {
: this->result->Caption=AnsiString(no1*no2);
: }
:
: else if (oprator=='/')
: {
: if (no2!=0) {
: this->result->Caption=AnsiString(no1/no2);
: }
: else if (no2==0)
: {ShowMessage(" You have to put non-zero numbers"); }
: }
:
:
: }
|