-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathPromptForm.pas
184 lines (155 loc) · 4.69 KB
/
PromptForm.pas
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
unit PromptForm;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, Spin, StdCtrls;
type
// interposer for spin edit
TSpinEdit = class(Spin.TSpinEdit)
protected
Function IsValidChar(Key: Char): Boolean; override;
end;
TfPromptForm = class(TForm)
lblPrompt: TLabel;
eTextValue: TEdit;
seIntValue: TSpinEdit;
btnOK: TButton;
btnCancel: TButton;
procedure FormShow(Sender: TObject);
procedure btnOKClick(Sender: TObject);
procedure btnCancelClick(Sender: TObject);
procedure ValueKeyPress(Sender: TObject; var Key: Char);
private
{ Private declarations }
public
{ Public declarations }
ResultState: TModalResult;
end;
Function IL_InputQuery(const Title,Prompt: String; var Value: String): Boolean; overload;
Function IL_InputQuery(const Title,Prompt: String; var Value: String; PasswordChar: Char): Boolean; overload;
Function IL_InputQuery(const Title,Prompt: String; var Value: Integer; Min: Integer = Low(Integer); Max: Integer = High(Integer)): Boolean; overload;
implementation
{$R *.dfm}
Function TSpinEdit.IsValidChar(Key: Char): Boolean;
begin
// TSpinEdit swallows return, so it cannot be handled in OnKeyPress event, this prevents it
If Key <> #13{return} then
Result := inherited IsValidChar(Key)
else
Result := True;
end;
//==============================================================================
procedure TfPromptForm.FormShow(Sender: TObject);
begin
ResultState := mrNone;
If lblPrompt.Width > ClientWidth + 16 then
ClientWidth := lblPrompt.Width + 16;
eTextValue.Left := 8;
eTextValue.Width := ClientWidth - 16;
seIntValue.Left := 8;
seIntValue.Width := ClientWidth - 16;
end;
//------------------------------------------------------------------------------
procedure TfPromptForm.ValueKeyPress(Sender: TObject; var Key: Char);
begin
case Key of
#13: begin {return}
Key := #0;
ResultState := mrOK;
Close;
end;
#27: begin {escape}
Key := #0;
ResultState := mrCancel;
Close;
end;
end;
end;
//------------------------------------------------------------------------------
procedure TfPromptForm.btnOKClick(Sender: TObject);
begin
ResultState := mrOK;
Close;
end;
//------------------------------------------------------------------------------
procedure TfPromptForm.btnCancelClick(Sender: TObject);
begin
ResultState := mrCancel;
Close;
end;
//==============================================================================
Function IL_InputQuery(const Title,Prompt: String; var Value: String): Boolean; overload;
var
PromptForm: TfPromptForm;
begin
Result := False;
PromptForm := TfPromptForm.Create(Application);
try
PromptForm.Caption := Title;
PromptForm.lblPrompt.Caption := Prompt;
PromptForm.eTextValue.Visible := True;
PromptForm.seIntValue.Visible := False;
PromptForm.eTextValue.Text := Value;
PromptForm.eTextValue.SelectAll;
PromptForm.ShowModal;
If PromptForm.ResultState = mrOK then
begin
Value := PromptForm.eTextValue.Text;
Result := True;
end;
finally
PromptForm.Free;
end;
end;
//------------------------------------------------------------------------------
Function IL_InputQuery(const Title,Prompt: String; var Value: String; PasswordChar: Char): Boolean; overload;
var
PromptForm: TfPromptForm;
begin
Result := False;
PromptForm := TfPromptForm.Create(Application);
try
PromptForm.Caption := Title;
PromptForm.lblPrompt.Caption := Prompt;
PromptForm.eTextValue.Visible := True;
PromptForm.seIntValue.Visible := False;
PromptForm.eTextValue.Text := Value;
PromptForm.eTextValue.SelectAll;
PromptForm.eTextValue.PasswordChar := PasswordChar;
PromptForm.ShowModal;
If PromptForm.ResultState = mrOK then
begin
Value := PromptForm.eTextValue.Text;
Result := True;
end;
finally
PromptForm.Free;
end;
end;
//------------------------------------------------------------------------------
Function IL_InputQuery(const Title,Prompt: String; var Value: Integer; Min: Integer = Low(Integer); Max: Integer = High(Integer)): Boolean; overload;
var
PromptForm: TfPromptForm;
begin
Result := False;
PromptForm := TfPromptForm.Create(Application);
try
PromptForm.Caption := Title;
PromptForm.lblPrompt.Caption := Prompt;
PromptForm.eTextValue.Visible := False;
PromptForm.seIntValue.Visible := True;
PromptForm.seIntValue.MinValue := Min;
PromptForm.seIntValue.MaxValue := Max;
PromptForm.seIntValue.Value := Value;
PromptForm.seIntValue.SelectAll;
PromptForm.ShowModal;
If PromptForm.ResultState = mrOK then
begin
Value := PromptForm.seIntValue.Value;
Result := True;
end;
finally
PromptForm.Free;
end;
end;
end.