[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

Validate TEdit to allow only valid file name characters?

Posted on 2015-02-19
6
Medium Priority
?
342 Views
Last Modified: 2015-05-30
Hi! In a Delphi XE7 VCL application, at runtime I need to validate the text input inside a TEdit to allow only valid file name characters. In Delphi XE7 there is the System.IOUtils.TPath.HasValidFileNameChars function. How can this be applied in real-time in a TEdit component?

Unfortunately, the TEdit component has no OnValidating event. I don't want to use the OnChange event, because it looks ugly when the character appears for a short time. The validation must occur BEFORE the character appears.
0
Comment
Question by:PeterDelphin
[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
  • 2
6 Comments
 
LVL 24

Expert Comment

by:jimyX
ID: 40620411
You can make use of one of the KeyDown/KeyUp/KeyPress events which enables you to check the pressed key before it is printed to the component.

procedure TForm1.Edit1KeyPress(Sender: TObject; var Key: Char);
begin
  if Key in ['a'..'z', 'A'..'Z','\', ..etc]  // List all the allowed Char ranges
  if not TPath.HasValidFileNameChars(Edit1.Text+Key, ...) // Or check the key if is not valid just reset the key
    Key:= #0;
end;
0
 

Author Comment

by:PeterDelphin
ID: 40623965
This is not reliable, as one can paste still PASTE chars which are not allowed as filename.
0
 
LVL 24

Accepted Solution

by:
jimyX earned 1000 total points
ID: 40624087
In this case you have to use OnChange event, but with a unique technique, I will show you how to avoid the invalid-chars appearance.

procedure TForm1.Edit1Change(Sender: TObject);
begin
  if not HasValidFileNameChars(TEdit(Sender).Text,..) then
    begin
      TEdit(Sender).Undo; //Clear last entry it was not valid char
      Exit;
    end;

  TEdit(Sender).ClearUndo; //reset the Undo, when you have a valid entry
end;

Open in new window


Mahdi78 has wrote a wonderful article about utilizing Undo & ClearUndo. It worth reading it.
0
 
LVL 38

Assisted Solution

by:Geert Gruwez
Geert Gruwez earned 1000 total points
ID: 40660415
use a maskedit to allow only the characters you want
0

Featured Post

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Question has a verified solution.

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

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…
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…
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…
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …
Suggested Courses

656 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