Solved

Restricting input into fields

Posted on 2004-03-23
10
321 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
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
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…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

746 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

11 Experts available now in Live!

Get 1:1 Help Now