Solved

Restricting input into fields

Posted on 2004-03-23
10
323 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
 
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

861 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now