?
Solved

Restricting input into fields

Posted on 2004-03-23
10
Medium Priority
?
332 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 3
  • 2
10 Comments
 
LVL 11

Accepted Solution

by:
shaneholmes earned 200 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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 23

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 23

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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses

718 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