How to restrict user input of the directory edit box? [closed]

混江龙づ霸主 提交于 2020-01-03 17:05:18

问题


I need to prevent users from entering . at the end of the path entered in the directory edit box.

For example, the path cannot be:

C:\Program Files\InnoSetup.

How can I validate the directory edit box input, or how can I prevent users from entering . to the end of the path ?


回答1:


To automatically delete all dots from the end of a target directory, you can use this script. You haven't answered my question, what you want to do when a dot is found at the end of the path, so I chose just this way to show up. Note that this will delete all dots from the end of a folder string, so from a path like:

C:\Program Files (x86)\My Program.....

this script makes:

C:\Program Files (x86)\My Program

Here is the script:

[Setup]
AppName=My Program
AppVersion=1.5
DefaultDirName={pf}\My Program

[code]
procedure OnDirEditChange(Sender: TObject);
var
  S: string;
begin
  S := WizardDirValue;
  if (Length(S) > 0) and (S[Length(S)] = '.') then
  begin
    MsgBox('Last char(s) of the entered target folder is "."' + #13#10 +
      'All "." chars from the end will be deleted!', mbInformation, MB_OK);
    while (Length(S) > 0) and (S[Length(S)] = '.') do
      Delete(S, Length(S), 1);
    WizardForm.DirEdit.Text := S;
  end;
end;

procedure InitializeWizard;
begin  
  WizardForm.DirEdit.OnChange := @OnDirEditChange;
end;

procedure CurPageChanged(CurPageID: Integer);
begin
  // this is just a paranoid event trigger, in case the DefaultDirName
  // would be able to contain dots at the end, what can't at this time
  if CurPageID = wpSelectDir then
    OnDirEditChange(nil);
end;

There are of course another ways to validate the path, you can e.g. let the user enter the path with dots at the end and validate it when you move to the next step in the wizard etc. But you just didn't specified what do you mean with your how to write validation question.



来源:https://stackoverflow.com/questions/12931222/how-to-restrict-user-input-of-the-directory-edit-box

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!