C ++ builder beginner's questions and answers (12)

Source: Internet
Author: User

 

13. Dialog Box Components

96) Q: How to compile a dialog box for opening and saving files?

A:C ++Builder provides many commonly used dialog box components, which are placed on the dialogs tab of the component template, such as font, color, open, and Print dialog boxes. The opendialog dialog box component is the most representative. Once you have mastered its usage, you will use other dialog box components.

97) Q: What are the features of the dialog box component?

A: The dialog box component is an invisible component. When you place it on a form, it is a graphical button with the same size.ProgramWhen running, the dialog box component is not displayed immediately. Instead, you must use the execute method in each dialog box to display the dialog box.

Most of the dialogs provided by C ++ Builder are stateful dialogs. This dialog box requires that you must first respond to this dialog box before continuing to work. In the response dialog box, you must click OK or cancel to return a value. If you click OK in the dialog box, return true, and click Cancel to return false. You can perform different processing based on the returned values.

A non-mode dialog box allows you to complete other tasks before responding to the dialog box, without having to first respond to the dialog box. Such dialogs include finddialog and replacedialog.

98) Q: Can I elaborate on the usage of the dialog box components?

A: We will compile a text reader to illustrate how to use the file opening dialog box.

Open a new form, put a button component and a memo component, set the scrollbars attribute of memo to ssboth, so that memo can display vertical and horizontal scroll bars.

Select the lines attribute to delete the text.

Place the opendialog component on the form.

By selecting the opendialog component, we can see that opendialog has such main features.

The filrerindex attribute is used to specify the default filter used to open each dialog box.

The defaultext attribute is an extension. When the file name entered in the file editing box does not contain an extension, the dialog box uses it as the default extension of the file.

The initialdir attribute is used to set the directory displayed when the dialog box is opened.

The filename attribute is used to return the complete path name of the last selected file.

Filter attribute, which is used to list objects in the file list of the dialog box.

The opendialog dialog box provides a special editor to edit the attribute filter. Click the omitted button in the value column of the attribute filter to open the editor.

Enter the Filter Name on the left and the file extension on the right.

Now, we enter the filters for text files and C ++ files. In this way, the name of the filter is displayed in the file type drop-down list each time the dialog box is opened.

Double-click the text button and enter such a paragraphCode:

//------------------------------------------

# Include <VCL. h>

# Pragma hdrstop

# Include "unit1.h"

//------------------------------------------

# Pragma package (smart_init)

# Pragma resource "*. DFM"

Tform1 * form1;

//-----------------------------------------

_ Fastcall tform1: tform1 (tcomponent * owner)

: Tform (owner)

{

}

//-------------------------------------------------

Void _ fastcall tform1: button1click (tobject * sender)

{

If (opendialog1-> execute () // first execute the open file dialog box function. Then, the open file dialog box is displayed. If the execution is successful, this function returns true; otherwise, false.

{

Memo1-> lines-> clear (); // clear the content in memo
Caption = opendialog1-> filename; // obtain the path name of the selected file and set it to the title of the form. Note: because the operation is performed in the form1 window (container), form1 is saved before the caption. Of course, you can add memo1-> lines-> loadfromfile (opendialog1-> filename ); // call the text Loading Function to load the text from the file and display it in the editing box.
 

99) Q: What are the important attributes of other dialogs?

A: saving the savedialog dialog box is very similar to opening the dialog box. You don't need to talk about openpicturedialog and savepicturedialog. Font attribute of fontdialog in the fontdialog font dialog box, color attribute of colordialog in the color dialog box, And findtext in the finddialog dialog box. I don't need to talk about it here. Let's take a look.

100) Q: Can I only use the components mentioned above in the dialog box group? If so, can you introduce it?

A: InC ++A series of standard dialog box functions are provided to the application developers in the Builder integrated with the visual component library of the development environment. We can directly call these functions to display some standard dialogs.

There are a lot of such functions, but we only need to master the following five functions.

Extern package int _ fastcall messagedlg (const ansistring MSG, tmsgdlgtype dlgtype, tmsgdlgbuttons buttons, int helpctx): displays the dialog box in the center of the computer screen.

The msg parameter specifies the string to be displayed in the dialog box.

The parameter dlgtype is a variable of the tmsgdlgtype class. It is used to specify the display features of the dialog box. Tmsgdlgtype is a set that can take the following values:

Mtwarning: the dialog box contains the yellow exclamation mark;

Mterror: the dialog box contains the red exclamation mark;

Mtinformation: the dialog box contains the blue "I" symbol;

Mtconfirmation: the dialog box contains a green question mark;

Mtcustom: the dialog box does not contain bitmap symbols. The title name is the application name.

The buttons parameter is used to specify the type of buttons contained in the dialog box. It is a set and can be a combination of the following elements:

Mbyes: the dialog box contains the "yes" button;

Mbno: the dialog box contains the "no" button;

Mbok: the dialog box contains the "OK" button;

Mbcancel: the dialog box contains the "cancel" button;

Mbabort: the dialog box contains the "Abort" button;

Mbretry: the dialog box contains the "retry" button;

Mbignore: the dialog box contains the "Ignore" button;

Mball: the dialog box contains the "all" button;

Mbnotoall: the dialog box contains the "notoall" button;

Mbyestoall: the dialog box contains the "yestoall" button;

Mbhelp: the dialog box contains the "help" button.

C ++Builder also defines three commonly used button combinations:

Mbyesnocancel: Contains mbyes, mbno, and mbcancel elements;

Mbokcancel: Contains mbok and mbcancel elements;

Mbabortretryignore: Contains mbabort, mbretry, and mbignore elements.

The helpctx parameter is used to specify the context number in the Help system when the user selects help. Based on the selected button, this function returns the following values: mrnone, mrabort, mrok, mrretry, mrno, mrcancel, mrignore, and mrall.

Extern package void _ fastcall showmessage (const ansistring MSG): This function is used to display a dialog box that contains an "OK" button. The content displayed in the dialog box is determined by the msg parameter.

Extern package ansistring _ fastcall inputbox (const ansistring acaption, const ansistring aprompt, const ansistring adefault): This function is used to display a dialog box on the computer screen. This dialog box has a text edit box, it can be used to receive user input string information.

Acaption: used to specify the title of the dialog box;

Aprompt: used to specify the content displayed in the dialog box;

Adefault: used to specify the content displayed in the edit box when the dialog box is opened.

Extern package bool _ fastcall logindialog (const ansistring acaption, ansistring & ausername, ansistring & ausername): This function is used to display a standard Logon dialog box to connect toDatabaseServerThe parameters are the database name, user name, and password in the dialog box.

Extern package bool _ fastcall selectdirectory (const ansistring caption, const widestring root, ansistring & directory) or extern package bool _ fastcall selectdirectory (ansistring & directory, tselectdiropts options, int helpctx ): this function is used to display a dialog box for users to enter the directory name. Where:

The caption parameter of the first syntax is used to specify the title of the dialog box, root is used to specify the root directory from which to start browsing, and dircetory is used to determine the directory selected by the user. This syntax cannot change the current directory.

The Parameter options of the second syntax is a set, which can be composed of the following elements:

Sdallowcreate: allows users to enter a non-existing directory name, but does not create this new directory;

Sdperformcreate: it must be used with sdallowcreate. when you enter a non-existent directory name, this directory will be created in the dialog box;

Sdprompt: it must be used with sdallowcreate. when you enter a directory name that does not exist, a message dialog box is displayed asking you whether to create the new directory. If you select Yes, this new directory will be created when the sdperformcreate element is included. If the element sdperformcreate is not included, no directory will be created.

This is the end of basic entry 100 Q & A. All the routines in this article are in the XP system,BCB6.

(This series is not over yet. Please stay tuned)

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.