Difference between revisions of "ReNamer:Pascal Script:Dialogs"

From den4b Wiki
Jump to navigation Jump to search
Line 11: Line 11:
 
If we know that the string might contain unicode characters we need to use WideShowMessage(const Msg: WideString) procedure instead.
 
If we know that the string might contain unicode characters we need to use WideShowMessage(const Msg: WideString) procedure instead.
  
There are several dialogs to ask user for data. The most representative are:
+
There are several dialogs to ask user for data. The most representative functions are:
  
'''DialogYesNo'''(const Msg: String): Boolean;
+
'''DialogYesNo''' (const Msg: String): Boolean;
 
 
and
 
  
 
'''InputQuery''' (const ACaption, APrompt: String; var Value: String): Boolean;
 
'''InputQuery''' (const ACaption, APrompt: String; var Value: String): Boolean;
 
functions.
 
  
 
The first one takes a message string and lets user to choose between YES and NO buttons to click. It returns TRUE if YES was choosen and FALSE otherwise.
 
The first one takes a message string and lets user to choose between YES and NO buttons to click. It returns TRUE if YES was choosen and FALSE otherwise.
Line 25: Line 21:
 
<pre><nowiki>
 
<pre><nowiki>
 
begin
 
begin
 
+
  if DialogYesNo('This filename "'+FileName+'" looks bad.'+#13+
If DialogYesNo('This filename ('''+FileName+''') looks stupid.'+#13+
+
    'I would prefer to name that file "Very Important File.txt". '+#13#13+'Do you agree?') then
              'I would prefer to name that file ''Very Important File.txt''. '+#13+'Do you agree?') then
+
   FileName := 'Very Important File.txt';
   FileName:='Very Important File.txt';
 
 
 
 
end.
 
end.
 
</nowiki></pre>
 
</nowiki></pre>
Line 39: Line 33:
 
<pre><nowiki>
 
<pre><nowiki>
 
var
 
var
Value: String;
+
  Value: String;
 
 
 
begin
 
begin
Value:='Meaningless filename.txt';
+
  Value := 'Meaningless filename.txt';
InputQuery ('Incorrect filename', 'I am not able to produce any meaningful filename!'+
+
  InputQuery('Incorrect filename',
            ' Would you mind giving it manually?', Value);
+
    'I am not able to produce any meaningful filename!'+
FileName:=Value;
+
    ' Would you mind giving it manually?', Value);
 +
  FileName := Value;
 
end.
 
end.
 
</nowiki></pre>
 
</nowiki></pre>

Revision as of 23:07, 25 May 2009

Interactive dialogs or How to let user decide

Sometimes we need to ask the user for data or inform him about something. In that case we need an interactive dialog.

The simplest one is ShowMessage(const Msg: String) procedure that takes a string and displays it as a small popup window with OK button.

ShowMessage('That''s a message for you!');

If we know that the string might contain unicode characters we need to use WideShowMessage(const Msg: WideString) procedure instead.

There are several dialogs to ask user for data. The most representative functions are:

DialogYesNo (const Msg: String): Boolean;

InputQuery (const ACaption, APrompt: String; var Value: String): Boolean;

The first one takes a message string and lets user to choose between YES and NO buttons to click. It returns TRUE if YES was choosen and FALSE otherwise.

begin
  if DialogYesNo('This filename "'+FileName+'" looks bad.'+#13+
    'I would prefer to name that file "Very Important File.txt". '+#13#13+'Do you agree?') then
  FileName := 'Very Important File.txt';
end.

The code above shows two important things about string constants. First is that you need to use #13 (or #10 or #13#10) to break the line. And second that you need to escape every ’ by doubling it (’’). So if you want to start a string constant with ’ you will need three of them ’’’!

InputQuery is the most powerful dialog. It takes two string constants (a caption of the popup window and a prompt, in which you can explain what kind of data you are expecting from user). The third parameter is a string variable Value that will return user text input. The current content of the Value variable is displayed in the input text box as the default value.

var
  Value: String;
begin
  Value := 'Meaningless filename.txt';
  InputQuery('Incorrect filename',
    'I am not able to produce any meaningful filename!'+
    ' Would you mind giving it manually?', Value);
  FileName := Value;
end.