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

From den4b Wiki
Jump to: navigation, search
m (Text replacement - "<source>" to "<syntaxhighlight lang="pascal">")
 
(13 intermediate revisions by 2 users not shown)
Line 1: Line 1:
 +
{{Up|ReNamer:Pascal Script}}
 +
 
==Interactive dialogs or How to let user decide==
 
==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.
 
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.
+
====Informative dialogs====
 
+
The simplest one is '''WideShowMessage'''(const Msg: String) procedure that takes a string and displays it as a small popup window with OK button.
<pre><nowiki>
 
ShowMessage('That''s a message for you!');
 
</nowiki></pre>
 
  
If we know that the string might contain unicode characters we need to use WideShowMessage(const Msg: WideString) procedure instead.
+
<syntaxhighlight lang="pascal">
 +
WideShowMessage('That''s a message for you!');
 +
</syntaxhighlight>
  
There are several dialogs to ask user for data. The most representative are:
+
If you know that the string will contain only ANSI characters you may use '''ShowMessage'''(const Msg: WideString) procedure instead.
  
'''DialogYesNo'''(const Msg: String): Boolean; 
+
====Asking user for data====
 +
There are several dialogs to ask user for data:  
  
and
+
{| class="wikitable"
 +
|-
 +
|function '''DialogYesNo'''(const Msg: String): Boolean;
 +
|-
 +
|function '''InputBox'''(const ACaption, APrompt: String; var Value: String): Boolean;
 +
|-
 +
|function '''InputQuery'''(const ACaption, APrompt: String; var Value: String): Boolean;
 +
|-
 +
|function '''WideInputBox'''(const ACaption, APrompt: String; var Value: String): Boolean;
 +
|-
 +
|function '''WideInputQuery'''(const ACaption, APrompt: String; var Value: String): Boolean;
 +
|}
  
'''InputQuery''' (const ACaption, APrompt: String; var Value: String): Boolean;
+
Let's take a look on some of them.
  
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.
+
'''DialogYesNo''' function 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.
  
<pre><nowiki>
+
<syntaxhighlight lang="pascal">
 
begin
 
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.
 +
</syntaxhighlight>
  
If DialogYesNo('This filename ('''+FileName+''') looks stupid.'+#13+
+
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 <nowiki>'</nowiki> by doubling it (<nowiki>''</nowiki>). So if you want to start a string constant with <nowiki>'</nowiki> you will need three of them <nowiki>'''</nowiki> !
              'I would prefer to name that file ''Very Important File.txt''. '+#13+'Do you agree?') then
 
FileName:='Very Important File.txt';
 
  
end.
 
</nowiki></pre>
 
  
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 <nowiki>’</nowiki> by doubling it (<nowiki>’’</nowiki>). So if you want to start a string constant with <nowiki>’</nowiki> you will need three of them <nowiki>’’’</nowiki>!
+
'''WideInputQuery''' 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 the user). The third parameter is a WideString variable Value that will return user text input back to you. The current content of the Value variable is displayed in the input text box as the default value.
 +
Apart from that the InputQuery function will return True if OK button was pressed and False otherwise.
  
'''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.
+
<syntaxhighlight lang="pascal">
 
 
<pre><nowiki>
 
 
var
 
var
Value: String;
+
  Value: WideString;
 
 
 
begin
 
begin
Value:='Meaningless filename.txt';
+
  Value := 'Meaningless filename.txt';
InputQuery ('Incorrect filename', 'I am not able to produce any meaningful filename! Would you mind giving it manually?', Value);
+
  if WideInputQuery('Incorrect filename',
FileName:=Value;
+
    'I am not able to produce any meaningful filename!'+
 +
    ' Would you mind giving it manually?', Value) then  FileName := Value;
 
end.
 
end.
</nowiki></pre>
+
</syntaxhighlight>
 +
 
 +
[[Category:ReNamer]]
 +
[[Category:Pascal Script]]

Latest revision as of 15:03, 8 February 2017

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.

Informative dialogs

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

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

If you know that the string will contain only ANSI characters you may use ShowMessage(const Msg: WideString) procedure instead.

Asking user for data

There are several dialogs to ask user for data:

function DialogYesNo(const Msg: String): Boolean;
function InputBox(const ACaption, APrompt: String; var Value: String): Boolean;
function InputQuery(const ACaption, APrompt: String; var Value: String): Boolean;
function WideInputBox(const ACaption, APrompt: String; var Value: String): Boolean;
function WideInputQuery(const ACaption, APrompt: String; var Value: String): Boolean;

Let's take a look on some of them.


DialogYesNo function 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 ''' !


WideInputQuery 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 the user). The third parameter is a WideString variable Value that will return user text input back to you. The current content of the Value variable is displayed in the input text box as the default value. Apart from that the InputQuery function will return True if OK button was pressed and False otherwise.

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