Inspired by inputbox and inputquery

Source: Internet
Author: User
I have read the source code of inputbox and inputquery functions, and I have some gains and experiences...

You can use inputbox to obtain user input strings:

 
Procedure tform1.button1click (Sender: tobject); var STR: string; begin STR: = inputbox ('input window title', 'input prompt ', 'default input content '); showmessage (STR); // display the input content end;

  

Inputbox calls inputquery, and inputquery obtains the new string through a VaR parameter:

 
Procedure tform1.button2click (Sender: tobject); var STR: string; begin inputquery ('input window title', 'input prompt ', STR); showmessage (STR ); // display the input content end;

  

Inputquery returns a Boolean value, which can be used to determine whether the user confirms or cancels the operation:

 
Procedure tform1.button3click (Sender: tobject); var STR: string; begin STR: = 'default input content'; If inputquery ('input window title', 'input prompt ', STR) then showmessage (STR); // if you click OK, the end of the input content is displayed;

  

I often use Windows that require user input or selection, usually show a window designed in advance. This will waste resources and the inputquery window should be dynamically created.

However, if dynamic creation is still complicated, such as adding events to buttons...

However, the source code of inputquery only contains dozens of lines. It is a dynamically created window, but no event-related information is found.Code!

Next, we can see that this is related to the "mode window". If we didn't use showmodal to start the window, I'm afraid the "event" code is not writable.

How is the "button" in "Mode window" associated with "Mode window?

Let's take a look:

 
// Subject code: Procedure tform1.button1click (Sender: tobject); var frm: tform; begin frm: = tform. create (application); frm. showmodal; frm. free; end;

  

Add two buttons dynamically at the same time:

Procedure tform1.button1click (Sender: tobject); var frm: tform; begin frm: = tform. create (application); with tbutton. create (FRM) Do begin caption: = 'confirmed'; parent: = FRM; left: = 100; top: = 40; end; with tbutton. create (FRM) Do begin caption: = 'cancel'; parent: = FRM; left: = 100; top: = 80; end; frm. showmodal; frm. free; end;

  

However, the buttons written in the code above do not have any function. In fact, you only need to set the modalresult attribute of the buttons:

 
Procedure tform1.button1click (Sender: tobject); var frm: tform; begin frm: = tform. create (application); with tbutton. create (FRM) Do begin caption: = 'confirmed'; modalresult: = mrok; {1} parent: = FRM; left: = 100; top: = 40; end; with tbutton. create (FRM) Do begin caption: = 'cancel'; modalresult: = mrcancel; {2} parent: = FRM; left: = 100; top: = 80; end; frm. showmodal; frm. free; end;

  

The modalresult attribute of the button is an integer, and the form has the same attributes. When you click the button, it is passed to the modalresult attribute of the corresponding form. The default value of this attribute is 0, when the value is not 0, the window is closed immediately.

In addition, showmodal is a function that returns the modalresult value of the window during calling.

The above statements are very important, and I have also verified them from the relevant source code.

 
Procedure tform1.button1click (Sender: tobject); var frm: tform; begin frm: = tform. create (application); with tbutton. create (FRM) Do begin caption: = 'confirmed'; modalresult: = mrok; {1} parent: = FRM; left: = 100; top: = 40; end; with tbutton. create (FRM) Do begin caption: = 'cancel'; modalresult: = mrcancel; {2} parent: = FRM; left: = 100; top: = 80; end; if frm. showmodal = mrok then showmessage ('confirmed ') else showmessage ('unconfirmed'); frm. free; end;

  

Complete the settings and add two text boxes for acceptance:

Procedure tform1.button1click (Sender: tobject); var frm: tform; begin frm: = tform. create (application); frm. position: = poscreencenter; frm. clientwidth: = 270; frm. clientheight: = 100; frm. caption: = 'input dialog box '; with tedit. create (FRM) Do begin parent: = FRM; Name: = 'edit1'; setbounds (10, 10,100, 20); end; with tedit. create (FRM) Do begin parent: = FRM; Name: = 'edit2'; setbounds (150, 10,100, 20); end; with tbutton. create (FRM) Do begin caption: = 'confirmed'; modalresult: = mrok; default: = true; parent: = FRM; setbounds (100, 40, 75, 25 ); end; with tbutton. create (FRM) Do begin caption: = 'cancel'; modalresult: = mrcancel; cancel: = true; parent: = FRM; setbounds (100, 70, 75, 25 ); end; If frm. showmodal = mrok then begin showmessage ('confirmed '); end; frm. free; end;

  

How can I return user input information? I think it is quite good to use the VaR parameter similar to the inputquery function to accept information. This method is also very convenient to accept complex information. However, you must first write the above code into a function:

{Function} function getinfo (VAR str1, str2: string): Boolean; var frm: tform; myedit1, myedit2: tedit; begin result: = false; frm: = tform. create (application); frm. position: = poscreencenter; frm. clientwidth: = 270; frm. clientheight: = 100; frm. caption: = 'input dialog box '; myedit1: = tedit. create (FRM); with myedit1 do begin parent: = FRM; text: = str1; setbounds (10, 10,100, 20); end; myedit2: = tedit. create (FRM); with myedit2 do begin parent: = FRM; text: = str2; setbounds (150, 10,100, 20); end; with tbutton. create (FRM) Do begin caption: = 'confirmed'; modalresult: = mrok; default: = true; parent: = FRM; setbounds (100, 40, 75, 25 ); end; with tbutton. create (FRM) Do begin caption: = 'cancel'; modalresult: = mrcancel; cancel: = true; parent: = FRM; setbounds (100, 70, 75, 25 ); end; If frm. showmodal = mrok then begin str1: = myedit1.text; str2: = myedit2.text; Result: = true; end; frm. free; end; {call test} procedure tform1.button1click (Sender: tobject); var S1, S2: string; begin S1: = 'aaa'; S2: = 'bbb '; if getinfo (S1, S2) Then showmessagefmt ('% s-% s', [S1, S2]); end;
 
   
 

This is done, but it is far from the brilliant source code of inputquery. However, I will try to use this function to complete the collection information dialog box from now on; this is what I learned by reading the inputquery source code.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.