Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use the Dialog dialog box in C #

2025-03-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)05/31 Report--

This article mainly explains "how to use the Dialog dialog box in C#". The content in the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "how to use the Dialog dialog box in C#".

1. MessageBox pop-up box

MessageBox.Show (Text, Title, nType,MessageBoxIcon)

The first parameter is the String type, which represents the contents of the prompt box

The second parameter is the String type, which represents the title of the prompt box

The third parameter is an integer type, which represents the type of message box, which generally uses several types provided by the system.

The fourth parameter is the icon of the prompt box, such as warnings, prompts, questions, and so on.

MessageBoxButtons type:

AbortRetryIgnore: the message box contains abort, retry, and ignore buttons.

OK: the message box contains an OK button. (default)

OKCancel: the message box contains OK and cancel buttons. (shown in the above example)

RetryCancel: the message box contains retry and cancel buttons.

YesNo: the message box contains Yes and No buttons.

YesNoCancel: the message box contains Yes, No, and cancel buttons

MessageBoxIcon icon style:

MessageBoxIcon.Question

MessageBoxIcon.Asterisk

MessageBoxIcon.Information

MessageBoxIcon.Error

MessageBoxIcon.Stop

MessageBoxIcon.Hand

MessageBoxIcon.Exclamation

MessageBoxIcon.Warning

For example, MessageBox.Show ("username or password cannot be empty"); MessageBox.Show ("username or password cannot be empty", "login prompt"); MessageBox.Show ("username or password cannot be empty", "login prompt", MessageBoxButtons.OKCancel); MessageBox.Show ("username or password cannot be empty", "login prompt", MessageBoxButtons.OKCancel,MessageBoxIcon.Exclamation); 2. WinForm comes with a dialog box.

All dialogs except PrintPreviewDialog inherit from the abstract class CommonDialog.

Inheritance structure of CommonDialog

1. File dialog box (FileDialog) it is commonly used to two:

Open the file dialog box (OpenFileDialog)

Save File Dialog (SaveFileDialog)

2. Font dialog box (FontDialog)

3. Color dialog box (ColorDialog)

4. Print preview dialog box (PrintPreviewDialog)

5. Page setup (PrintDialog)

6. Print dialog box (PrintDialog)

CommonDialog's method

OnHelpRequest (EventArgs): raises the HelpRequest event.

Reset (): when overridden in a derived class, resets the properties of the generic dialog box to their default values.

ShowDialog (): runs the generic dialog box with the default owner.

ShowDialog (IWin32Window): runs a generic dialog box with the specified owner.

1. Open the file dialog box (OpenFileDialog)

Basic attribute

The initial directory of the InitialDirectory dialog box

The file filter to be displayed by Filter in the dialog box, for example, "text files (* .txt) | * .txt | all files (*. *) | *. *"

The index of the file filter selected by FilterIndex in the dialog box, set to 1 if the first item is selected

RestoreDirectory controls whether the dialog box restores the current directory before closing

FileName gets or sets a string containing the file name selected in the file dialog box.

The characters that Title will display in the title bar of the dialog box

Whether AddExtension automatically adds the default extension

CheckPathExists checks whether the specified path exists before the dialog box returns

DefaultExt default extension

Whether DereferenceLinks dereferencing shortcuts before returning from the dialog box

ShowHelp enables the help button

The ValiDateNames control dialog box checks whether the file name does not contain invalid characters or sequences

Example

System.Windows.Forms.OpenFileDialog dlg = new System.Windows.Forms.OpenFileDialog (); dlg.Title = "Open file"; dlg.InitialDirectory = Environment.GetFolderPath (Environment.SpecialFolder.Templates); dlg.Filter = "txt files (* .txt) | * .txt | All files (*. *) | *. *"; dlg.FilterIndex = 2transferdlg.RestoreDirectory = true;if (dlg.ShowDialog () = DialogResult.OK) {if (dlg.FileName! = ") / if dlg.Multiselect=true It can be dlg.FileNames {MessageBox.Show ("you chose" + dlg.FileName);}} 2, Save File dialog box (SaveFileDialog)

Attribute

The file filter to be displayed by Filter in the dialog box, for example, "text files (* .txt) | * .txt | all files (*. *) | *. *"

The index of the file filter selected by FilterIndex in the dialog box, set to 1 if the first item is selected

RestoreDirectory controls whether the dialog box restores the current directory before closing

Whether AddExtension automatically adds the default extension

CheckFileExists gets or sets a value indicating whether the dialog box displays a warning if the user specifies a file name that does not exist.

CheckPathExists checks whether the specified path exists before the dialog box returns

Container controls whether to prompt the user when a file is about to be created. Applies only if ValidateNames is true.

DefaultExt default extension

Whether DereferenceLinks dereferencing shortcuts before returning from the dialog box

FileName gets or sets a string containing the file name selected in the file dialog box.

The initial directory of the InitialDirector dialog box

OverwritePrompt controls whether to prompt the user when rewriting the current file, and applies only if ValidateNames is true

ShowHelp enables the help button

The characters that Title will display in the title bar of the dialog box

The ValidateNames control dialog box checks whether the file name does not contain invalid characters or sequences

Example

System.IO.Stream stream;System.Windows.Forms.SaveFileDialog saveFileDialog1 = new System.Windows.Forms.SaveFileDialog (); saveFileDialog1.Filter = "txt files (* .txt) | * .txt | All files (*. *) | *. *"; saveFileDialog1.FilterIndex = 2saveFileDialog1.RestoreDirectory = true;if (saveFileDialog1.ShowDialog () = = DialogResult.OK) {if ((stream = saveFileDialog1.OpenFile ())! = null) {/ / Code to write the stream goes here. Stream.Close ();}} 3, print preview dialog box and print dialog box 1, print preview dialog box (PrintPreviewDialog) properties:

AutoScrollMargin gets or sets the size of the auto-scroll margin.

AutoScrollMinSize gets or sets the minimum size for automatic scrolling.

DialogResult gets or sets the dialog result of the form.

Document gets or sets the document to preview.

HelpButton gets or sets a value indicating whether the help button should be displayed in the title box of the form.

2. Print dialog box (PrintDialog) properties:

AllowPrintToFile disables or uses the print to File check box

AllowSelection disables or uses the selection radio box

AllowSomePages disables or uses the Page radio button

The PrintDocument from which Document gets printer settings

Whether the PrintToFile print to file check box is selected

ShowHelp controls whether the help button is displayed

ShowNetWork controls whether the Network button is displayed

3. Example: private void printPreviewButton_Click (object sender, EventArgs e) {StreamReader streamToPrint = new StreamReader ("PrintMe.Txt"); try {PrintDocument pd = new PrintDocument (streamToPrint); / / assume the default printer if (storedPageSettings! = null) {pd.DefaultPageSettings = storedPageSettings;} PrintPreviewDialog dlg = new PrintPreviewDialog (); dlg.Document = pd; dlg.ShowDialog () } finally {streamToPrint.Close ();}} private void printButton_Click (object sender, EventArgs e) {StreamReader streamToPrint = new StreamReader ("PrintMe.Txt"); try {PrintDocument pd = new PrintDocument (streamToPrint); PrintDialog dlg = new PrintDialog (); dlg.Document = pd; DialogResult result = dlg.ShowDialog (); if (result = = DialogResult.OK) pd.Print () } finally {streamToPrint.Close ();}} III. Custom dialog box 1 Modal window: ShowDialog ():

When a modal window is opened, the mouse focus or cursor stays on the window as long as it is not closed. The calling window cannot continue until the window is closed.

After the modal window is closed, you can still read the information in the modal window, such as the return state of the window, and later you can use ShowDialog () to make it visible.

2 modeless window: Show ():

After you open a modeless window, you can still manipulate the call window. The following code is executed immediately.

Close the modeless window, the window will no longer exist, all resources of the window will be released, so no information about the window can be obtained. The common Hide () method (equivalent to Visible=false) then calls the Show () method to make it visible.

3. Dialog form: Form2public Form1 (string para) / / get parameter {InitializeComponent (); this.StartPosition = FormStartPosition.CenterParent;// startup location, parent window center this.MaximizeBox = false; this.MinimizeBox = false; this.ShowIcon = false;// does not display icon this.ControlBox = false; this.ShowInTaskbar = false; this.FormBorderStyle = FormBorderStyle.FixedDialog;// frame style is fixed dialog box this.btnOK.DialogResult = DialogResult.OK / / "Enter" defines a public property for the OK button this.btnCancel.DialogResult = DialogResult.Cancel;// "ESC" and the Cancel button this.textBox1.Text = para;} public string ReturnText / / for the calling window Form1 to use {get {return this.textBox1.Text + "b" }} private void Form1_Load (object sender, EventArgs e) {if (this.Owner.Name! = "Form1") / / Owner is the calling form, that is, the form MessageBox.Show of the dialog box is called ("illegal call");} private void BtnOK_Click (object sender, EventArgs e) {if (this.textBox1.Text.Trim (). Length = = 0) MessageBox.Show ("No input"); this.textBox1.Focus () This.DialogResult = DialogResult.None;// blocks hidden dialog boxes, dialog box does not disappear} 4, main form Form1:Form f2 = new Form2 ("a"); if (f2.ShowDialog (this) = = DialogResult.OK) / / the Owner,this in the corresponding Form2 passes the value this.textBox1.Text = f2.ReturnTexttern f2.Close () to the dialog form; f2.Dispose () Thank you for your reading, the above is the content of "how to use the Dialog dialog box in C#". After the study of this article, I believe you have a deeper understanding of how to use the Dialog dialog box in C#, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report