Solved

Restricting input into fields

Posted on 2004-03-23
10
329 Views
Last Modified: 2010-04-05

Hi I'm using Dev Express custom control DBInspector in Delphi5, to display info from a query.

The user is able to enter and update info via the control, but in certain rows I want to restrict input to numeric or spaces, the events for the rows are only OnChange, OnValidate, OnDrawCaption & OnDrawValue.

On a normal edit field it would simply be a case of using the event keypress, but with these limited events I'm not sure, any ideas?

Thanks
0
Comment
Question by:dealclickcouk
  • 5
  • 3
  • 2
10 Comments
 
LVL 11

Accepted Solution

by:
shaneholmes earned 50 total points
ID: 10657488
dealclickcouk, I did this inside the TEDit's OnChange event for practical application purposes only.

procedure TForm1.Edit1Change(Sender: TObject);
var
 S: String;
begin
 S:= edit1.Text;
 if S[Length(S)] in ['0', '1','2', '3','4','5','6','7','8','9'] then
 begin
  //send a back space command
  Keybd_Event(vk_back,0,KEYEVENTF_EXTENDEDKEY,0);
  beep();
 end;
end;


SHane
0
 

Author Comment

by:dealclickcouk
ID: 10657643

Hi, I've tried this code and got all sort of errors, plus I want allow numeric's this seems to stop them.

ie if I enter a char in the middle of the string is removes all to the left, if blank then causes an access violation error etc...
0
 
LVL 11

Expert Comment

by:shaneholmes
ID: 10657687
it was an example off the top of my head how to restrict characters using the OnChange event. You didn't say which chars you wanted to restrict, so i used the numerical characters as an example. Create a new application, drop a TEdit on the form and place the code in its onChange event and see what it does.

Of course, I have never used the DBInspector component, so I dont know how it acts to this code. I was only trying to give you an example of using a OnChange event to prevent keys versus the OnKeyPress event.

Shane
0
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
LVL 11

Expert Comment

by:shaneholmes
ID: 10657776
can you post you onchange event (code i displayed above that causes the area) so we can look?

Shane
0
 

Author Comment

by:dealclickcouk
ID: 10658070

When u say I didn't specify what chars, "restrict input to numeric or spaces" this from my original post gives a hint...

OK take this scenario...

field already has the following entry:

0115 939291

user insert into existing text:

0115 (mail first) 939291

the code you have supplied would (assuming it allowed only numerics & spaces)  not delete anything as your code only looks at the last char.

Also the line

 S:= InspectorPhoneWork.EditText;

only gives the var S what was last posted, so if blank to start with it would produce an error, even though text had been entered  because the change event happens before post.  I think this is strange, but this is what seems to be happening.
0
 
LVL 11

Expert Comment

by:shaneholmes
ID: 10658297


Well, you could just replace the ['1'........'9'] with ['A'...........'Z'....'a'........'z']

or

C:= S[length(S)];

  if (ord(C) >= 65) OR (ord(C) <= 90) OR
    (ord(C) >= 97) OR (ord(C) <= 122) then


Shane
0
 
LVL 11

Expert Comment

by:shaneholmes
ID: 10658326
Yeah, i see what you mean now with the last char............hmmmm, sorry, i dont have that DBInspector control to try things out with, maybe someone else has it here, im gonna listen in...........

Shane
0
 
LVL 22

Expert Comment

by:Ferruccio Accalai
ID: 10659545
What about somethin like this?

procedure TForm1.Edit1Change(Sender: TObject);
VAR
I,J: Integer;
s : STring;
begin
J := Edit1.SelStart;
for i := 1 to length(Edit1.Text) do
  if Edit1.Text[i] in ['0'..'9',' '] then
    s := s+Edit1.Text[i];
Edit1.Text := s;
If J < Length( Edit1.Text) then
dec(j);
  Edit1.SelStart := J;
end;

F68 ;-)
0
 

Author Comment

by:dealclickcouk
ID: 10659598

What is the SelStart as this isn't valid within the inspector object? gives error undeclared identifier...
0
 
LVL 22

Expert Comment

by:Ferruccio Accalai
ID: 10659610
mmm....that's for the caret position....
Just try skipping the selstart parts and tell us about the effect
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…

828 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question