Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Delphi -> Builder 1

Status
Not open for further replies.

digimortal

Programmer
Oct 12, 2003
28
TR
procedure TForm1.Edit1KeyPress(Sender: TObject; var Key: Char);
const
ValidInput = [ '0'..'9', #8];
begin
if not ( key in ValidInput ) then
begin
key := #0;
Beep;
end;
end;"

How can I change this code so that it will work with C++ Builder? I want this; Users can not enter characters other than numbers in an editbox... BTW I don't want to use a MaskEdit
Thanks in advance...
 
void __fastcall TForm1::Edit1KeyPress(Sender: TObject; var Key: Char)
{
const char * ValidInput = "0123456789";
if(!strchr(ValidInput, Key)) // Or are the parameters other way around?
{
Key = 0;
// Beep; I can't remember how.....
}
}

Totte
Keep making it perfect and it will end up broken.
 
Oh yes, the Beep is:
BOOL Beep(
DWORD dwFreq, // sound frequency, in hertz
DWORD dwDuration // sound duration, in milliseconds
);
i.e. Beep(2000, 1000); // Beep with 2KHz in 1 sec.

Totte
Keep making it perfect and it will end up broken.
 
But this time Backspace and enter does not work any help?
 
void __fastcall TForm1::Edit1KeyPress(Sender: TObject; var Key: Char)
{
const char * ValidInput = "0123456789\n\r\x08"; // Add to it the ASCII for Backspace, i have given it the value 0x08 ('\x08'=hex value) here but i can't remember for my life if it's that value
if(!strchr(ValidInput, Key))
{
Key = 0;
Beep(2000, 1000); // Beep with 2KHz in 1 sec.
}
}



Totte
Keep making it perfect and it will end up broken.
 
Hi :)

You can also use isdigit to check if the char is a number or not

void __fastcall TForm1::Edit1KeyPress(TObject *Sender, char &Key)
{

// this will verify if the key is a digit or a backspace
if(!isdigit(Key) && Key!='\b')
{
Key=0;
Beep(); //may need to include SysUtils
}

}
//---------------------------------------------------------------------------

or

void __fastcall TForm1::Edit1KeyPress(TObject *Sender, char &Key)
{
if(!isdigit(Key) && Key!=VK_BACK)
{
Key = 0;
Beep();
}
}
//---------------------------------------------------------------------------
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top