Solved

Restricting input into fields

Posted on 2004-03-23
10
325 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
Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

 
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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

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…
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…
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.

776 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