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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Edit number control 2

Status
Not open for further replies.

JustBeginer

Programmer
Mar 13, 2003
39
Hi !!
I'm looking for editnumber control....
If anybody know something for free download :))
Thanks in advance !!!
 
Im just curious

what is an editnumber control

tomcruz.net
 
I have done this to "make" an editNumber

Code:
//---------------------------------------------------------
AnsiString __fastcall ParserEntero(AnsiString value)
{   
   AnsiString newstring = "";
   for (int i=1; i<=value.Length(); i++)
      if (value[i] >= '0' && value[i] <= '9')
         newstring += value[i];
   return newstring;
}
//---------------------------------------------------------
void __fastcall TForm1::EditNumberChange(TObject *Sender)
{
   int sel = EditNumber->SelStart;
   int Len = EditNumber->Text.Length();
   EditNumber->Text = ParserEntero(EditNumber->Text);
   if (Len == EditNumber->Text.Length())
      EditNumber->SelStart = sel;
   else
      EditNumber->SelStart = sel - 1;
}
//---------------------------------------------------------


The first function deletes any no number character.

The second one is a simple onChange event, but this way you can prevent no number characters being pasted.
 
I thnk you may find this more compact

Code:
void __fastcall TForm1::Edit1KeyPress(TObject *Sender, char &Key)
{
    if (!isdigit(Key))
        Key = NULL;
}

yep I remember LastCyborg

tomcruz.net
 
Thanks butthead !
But I wanna control for borland. I'm VC programmer and I have perfect edit control for VC :) I'm not very familiar with borland...
 
The easiest way is to write an OnKeyDown-event for the edit control like:

void __fastcall TAdminForm::EdAantalKeyDown(TObject *Sender, WORD &Key,
TShiftState Shift)
{
//Only numeric, enter en backspace are allowed
if ((char)Key == 8 || (char)Key == 13)
return;

if ((char)Key < '0' || (char)Key > '9')
{
TMsg msg;
PeekMessage(&msg, 0, WM_CHAR, WM_CHAR, PM_REMOVE);
Beep();
}
}

In this functions the keys you want to refuge are deleted from the keyboard cue.
 
Raet, but what about copy-paste events?

that's why I did it using the OnChange event
 
something inspired by a recent thread
Code:
void __fastcall TForm1::Edit13Change(TObject *Sender)
{    
    int x = Edit13->SelStart;
    char *buff  = new char [Edit13->Text.Length () + 1];
    strcpy (buff, Edit13->Text.c_str ());

    for (int x = 0; x < Edit13->Text.Length (); x++)
    {
        if(!strchr("0123456789.", buff [x]))
        {
            //ShowMessage ("Invalid Input");
            Edit13->Text = str;
            break;
        }  
    }

    str = Edit13->Text;
    Edit13->SelStart = x;
    delete buff;
}

instead of buff you could use the ansistring stuff
to iterate through the text. I just revert to char
array by habit.

TC
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top