Asp. NET Form dialog box implementation

Source: Internet
Author: User
Tags filter empty file system print format save file
Asp.net| dialog box


The form dialog component is the same as the dialog box in the Microsoft Windows operating system; That is, the PrintDialog component is the Print dialog box, the OpenFileDialog component is the Open File dialog box, and so on.

Similar to previous Windows programming languages such as Microsoft Visual Basic 6.0, the. NET Framework provides a familiar dialog box for Windows users. The specific purpose of a dialog box, such as Printdialog can be used for file printing, is usually varied. Therefore, the underlying classes provided in the. NET framework do not contain code for specific operations such as file printing, color selection, and you can implement them flexibly, depending on the needs of your application. Therefore, in the. NET framework, you can not only apply the standard dialog box directly, but also respond differently depending on the user's choice. The code provided in this article is intended for this purpose.

Note that the complete description of the properties, methods, and events for each dialog box can be found in the members page of the corresponding class. For example, to see a method of the OpenFileDialog component, you can find related topics in the "OpenFileDialog class, all Members" section of the document index.

OpenFileDialog Components

The OpenFileDialog dialog box allows the user to open the selected file by browsing the local or remote file system. It returns the file path and the selected file name.

The OpenFileDialog component and the SaveFileDialog component (described in detail below) contain the basic code necessary to browse and select files. With them, you don't have to write any code for these functions, and then you can focus on the specific operations of opening or saving files.

Note that the FilterIndex property of the FileDialog class (which is common to OpenFileDialog and SaveFileDialog classes for inheritance) uses the one-based index (the index that is numbered from 1). This attribute is used in the code below (and is highlighted again in the appropriate location). This is important when an application opens a file through a type filter, or when it needs to be saved as a file in a particular format (for example, when saving as a plain text file instead of a binary file). People often forget it when they use the FilterIndex attribute, so be sure to remember it now.

The following code invokes the OpenFileDialog component through the Click event of the Button control. When the user selects a file and clicks OK, the selected file is opened. In this case, the contents of the file will be displayed in a message box to confirm that the file stream is read.

This example assumes the presence of a Button control named Button1 and a OpenFileDialog control named OpenFileDialog1.

' Visual Basic
' note:you must import the following namespace:
' Imports System.IO
' without this impor T statement at the beginning
' of your code, the example would not function.
Private Sub button1_click (ByVal sender as System.Object, _
ByVal e as System.EventArgs) Handles Button1.Click If openfiledialog1.showdialog () = DialogResult.OK Then
Dim sr as New StreamReader (openfiledialog1.filename)
M Essagebox.show (Sr. READTOEND)
Sr. Close ()
End If
End Sub

C#
Note:you must import the following namespace:
Using System.IO;
Without this import statement at the beginning
of your code, the example would not function.
private void Button1_Click (object sender, System.EventArgs e)
{
if (openfiledialog1.showdialog () = = DialogResult.OK)
{
StreamReader sr = new StreamReader (openfiledialog1.filename);
MessageBox.Show (Sr. ReadToEnd ());
Sr. Close ();
}
}

You can also open a file by using the OpenFile method of the OpenFileDialog component, which returns each byte of the file. In the following example, a OpenFileDialog component is instantiated and uses a cursor filter to qualify the user to select only the cursor file (extension. cur). Once a. cur file is selected, the form's cursor is set to the cursor shape that the file depicts.

This example assumes the presence of a Button control named Button1.

' Visual Basic
Private Sub button1_click (ByVal sender as System.Object, _
ByVal e as System.EventArgs) Handles Button1.Click
' Display an OpenFileDialog so the user can select a Cursor.
Dim OpenFileDialog1 as New OpenFileDialog ()
Openfiledialog1.filter = "Cursor files|*.cur"
Openfiledialog1.title = "Select a Cursor File"

' Show the Dialog.
' If the user clicked OK in the dialog and
A. CUR file was selected, open it.
If openfiledialog1.showdialog () = DialogResult.OK Then
If openfiledialog1.filename <> "" Then
' Assign the cursor in the Stream to the Form ' cursor property.
Me.cursor = New Cursor (openfiledialog1.openfile ())
End If
End If
End Sub

C#
private void Button1_Click (object sender, System.EventArgs e)
{
Display an OpenFileDialog so the user can select a Cursor.
OpenFileDialog openFileDialog1 = new OpenFileDialog ();
Openfiledialog1.filter = "Cursor files|*.cur";
Openfiledialog1.title = "Select a Cursor File";

Show the Dialog.
If the user clicked OK in the dialog and
A. CUR file was selected, open it.
if (openfiledialog1.showdialog () = = DialogResult.OK)
{
if (Openfiledialog1.filename!= "")
{
Assign the cursor in the Stream to the Form's cursor property.
This. Cursor = new Cursor (Openfiledialog1.openfile ());
}
}
}

For further information on reading file streams, see the Filestream.beginread method.

SaveFileDialog Components

This dialog box allows the user to browse the file system and select the file to be written. Of course, you must also write specific code for file writing.

The following code invokes the SaveFileDialog component through the Click event of the Button control. When a user selects a file and clicks OK, the contents of the RichTextBox control are saved to the selected file.

This example assumes the presence of a Button control named Button1, a RichTextBox control named RichTextBox1, and a SaveFileDialog control named OpenFileDialog1.

' Visual Basic
' Note:you must import the following namespace:
' Imports System.IO
' Without this import statement at the beginning
' of your code, the code example would not function.
Private Sub button1_click (ByVal sender as System.Object, _
ByVal e as System.EventArgs) Handles Button1.Click
If savefiledialog1.showdialog () = DialogResult.OK Then
Richtextbox1.savefile (Savefiledialog1.filename, _
Richtextboxstreamtype.plaintext)
End If
End Sub

C#
Note:you must import the following namespace:
Using System.IO;
Without this import statement at the beginning
of your code, the code example would not function.
private void Button1_Click (object sender, System.EventArgs e)
{
if ((savefiledialog1.showdialog () = = DialogResult.OK)
{
Richtextbox1.savefile (Savefiledialog1.filename, Richtextboxstreamtype.plaintext);
}
}

You can also save the file by using the OpenFile method of the SaveFileDialog component, which provides a Stream object for writing.

In the following example, there is a Button control that contains a picture. When you click this button, a SaveFileDialog component will be opened, which will use the. gif,. jpeg, and. bmp type of file filters. Once the user selects this type of file through the Save file dialog box, the picture on the button is saved.

This example assumes the existence of a Button control named Button2, and its Image property is set to a picture file with a. gif,. jpeg, or. bmp extension.

' Visual Basic
' note:you must import the following namespaces:
' Imports System.IO
' Imports System.dra Wing. Imaging
' Without these import statements at the beginning of your code,
' The code example would not function. br> Private Sub button2_click (ByVal sender as System.Object, _
ByVal e as System.EventArgs) Handles Button2.click
' Display an SaveFileDialog so the user can save the Image
' assigned to Button2.
Dim SaveFileDialog1 as New SaveFileDialog ()
Savefiledialog1.filter = "JPeg image|*.jpg| Bitmap image|*.bmp| Gif image|*.gif "
Savefiledialog1.title =" Save an Image File
Savefiledialog1.showdialog ()

' If ' is not ' a empty string open it for saving.
If savefiledialog1.filename <> "" Then
' Save the Image via a FileStream created by the OpenFile method.
Dim fs as FileStream = CType (Savefiledialog1.openfile (), FileStream)
' Save the ' Image in the appropriate imageformat based upon the
' File type selected in the dialog box.
' The FilterIndex ' is one-based.
Select Case Savefiledialog1.filterindex
Case 1
Me.button2.Image.Save (FS, imageformat.jpeg)

Case 2
Me.button2.Image.Save (FS, imageformat.bmp)

Case 3
Me.button2.Image.Save (FS, imageformat.gif)
End Select

Fs. Close ()
End If
End Sub

//C #
//Note:you must import the following namespaces:
//Using System.IO;
//using System.Drawing.Imaging;
//Without this import statements at the beginning of your code,
//The code example would not function.
private void Button2_Click (object sender, System.EventArgs e)
{
//Display a SaveFileDialog so the user CA n Save the Image
//Assigned to Button2.
SaveFileDialog saveFileDialog1 = new SaveFileDialog ();
Savefiledialog1.filter = "JPeg image|*.jpg| Bitmap image|*.bmp| Gif Image|*.gif ";
Savefiledialog1.title = "Save an Image File";
Savefiledialog1.showdialog ();

If the file name is not a empty string open it for saving.
if (Savefiledialog1.filename!= "")
{
Save the Image via a FileStream created by the OpenFile method.
FileStream fs = (FileStream) savefiledialog1.openfile ();
Save the Image in the appropriate imageformat based upon the
The File type selected in the dialog box.
The FilterIndex property is one-based.
Switch (SAVEFILEDIALOG1.FILTERINDEX)
{
Case 1:
This.button2.Image.Save (Fs,imageformat.jpeg);
Break

Case 2:
This.button2.Image.Save (fs,imageformat.bmp);
Break

Case 3:
This.button2.Image.Save (fs,imageformat.gif);
Break
}

Fs. Close ();
}
}

For further information on writing to the file stream, see the FileStream.BeginWrite method.

ColorDialog Components

This dialog box displays a list of colors and returns the selected color.

Unlike the first two dialog boxes, the ColorDialog component can easily achieve its primary function (pick colors). The selected color becomes the set value of the Color property. Therefore, using color is as simple as setting a property value. In the following example, the button-controlled Click event will open a ColorDialog component. Once the user selects a color and clicks OK, the background of the button is set to the selected color. This example assumes the presence of a Button component named Button1 and a ColorDialog component named ColorDialog1.

' Visual Basic
Private Sub button1_click (ByVal sender as System.Object, _
ByVal e as System.EventArgs) Handles Button1.Click
If colordialog1.showdialog () = DialogResult.OK Then
Button1.backcolor = colorDialog1.Color
End If
End Sub

C#
private void Button1_Click (object sender, System.EventArgs e)
{
if (colordialog1.showdialog () = = DialogResult.OK)
{
Button1. BackColor = colorDialog1.Color;
}
}

The ColorDialog component has a Allowfullopen attribute. When it is set to False, the Define Custom Colors button will fail, at which point the user can only use a predefined palette. In addition, it has a solidcoloronly property, and when set to true, the user will not be able to use the dither color.

FontDialog Components

This dialog box allows the user to select a font to change properties such as weight and size.

The selected font becomes the set value of the Font property. As a result, using fonts is as simple as setting property values. In this example, the FileDialog component is invoked through the Click event of the Button control. When the user selects a font and clicks OK, the Font property of the TextBox control is set to the selected font. This example assumes the presence of a Button control named Button1, a TextBox control named TextBox1, and a FontDialog component named FontDialog1.

' Visual Basic
Private Sub button1_click (ByVal sender as System.Object, _
ByVal e as System.EventArgs) Handles Button1.Click
If fontdialog1.showdialog () = DialogResult.OK Then
Textbox1.font = Fontdialog1.font
End If
End Sub

C#
private void Button1_Click (object sender, System.EventArgs e)
{
if (fontdialog1.showdialog () = = DialogResult.OK)
{
Textbox1.font = Fontdialog1.font;
}
}

The FontDialog component also includes the MinSize and MaxSize properties, which determine the minimum and maximum number of fonts to allow the user to select, and a Showcolor property that allows the user to select the color of the font from the Drop-down list in the dialog box when it is set to True.

PrintDocument class

The following three session boxes, PrintDialog components, PageSetupDialog components, and PrintPreviewDialog controls, are related to the PrintDocument class. The PrintDocument class is used before the document is printed: Set its properties to change how the document looks and how it is printed, and then output its instance to the printer. The usual steps are:
(1) Generating an instance of the PrintDocument class;
(2) Setting the properties of the PageSetupDialog component;
(3) Using PrintPreviewDialog control to preview;
(4) printed through the PrintDialog component.

For further information on the PrintDocument class, see PrintDocument Class.

PrintDialog components

This dialog box allows the user to specify the document that will be printed. In addition, it can be used to select a printer, decide on a printed page, and set print-related properties. It gives users more flexibility in printing their documents: they can print the entire document, print a fragment, and print the selected area.

When using the PrintDialog component, be aware of how it interacts with the PrinterSettings class. The PrinterSettings class is used to set printer feature attributes such as paper source, resolution, and doubling magnification. Each setting is an attribute of the PrinterSettings class. The PrintDialog class allows you to change the value of a feature property that is associated with the Printersetting class instance (specified by printdocument.printersettings) that is related to the document.

The PrintDialog component submits an instance of the PrintDocument class that contains the feature property settings to the printer. For an example of document printing using the PrintDialog component, see Creating Standard Windows Forms print Jobs.

PageSetupDialog Components

The PageSetupDialog component is used to display print layouts, paper sizes, and other page options. As with other dialog boxes, you can invoke the PageSetupDialog component by ShowDialog method. In addition, you must generate an instance of the PrintDocument class, the document that is printed, and a local or remote printer must be installed, otherwise the PageSetupDialog component will not be able to obtain a print format for the user to choose from.

You must be aware of how it interacts with the PageSettings class when you use the PageSetupDialog component. The PageSettings class determines how pages are printed, such as print orientation, page size, and margins. Each setting is an attribute of the PageSettings class. The PageSetupDialog class can change the above options for the PageSettings class instance (specified by Printdocument.defaultpagesettings).

In the following code, the Click event handler for the Button control opens a PageSetupDialog component; Its document property is set to a document that exists, and its Color property is set to False.

This example assumes the presence of a Button control named Button1, a PrintDocument control named MyDocument, and a PageSetupDialog component named PageSetupDialog1.

Visual Basic
Private Sub button1_click (ByVal sender as System.Object, _
ByVal e as System.EventArgs) Handle S Button1.Click
' the print Document ' MyDocument ' used below
' is merely for a example.
' You'll have to specify your own print document.
Pagesetupdialog1.document = myDocument
' Set the print Document ' s color setting to False,
' so ' pag E won't is printed in color.
PageSetupDialog1.Document.DefaultPageSettings.Color = False
Pagesetupdialog1.showdialog ()
End Sub

C#
private void Button1_Click (object sender, System.EventArgs e)
{
The print document ' MyDocument ' used below
is merely to an example.
You'll have to specify your own print document.
Pagesetupdialog1.document = myDocument;
Set the print document ' s color setting to False,
So this page won't be printed in color.
PageSetupDialog1.Document.DefaultPageSettings.Color = false;
Pagesetupdialog1.showdialog ();
}

PrintPreviewDialog Control

Unlike other dialog boxes, the PrintPreviewDialog control has no effect on the entire application or other controls, because it only displays content in a dialog box. This dialog box is used to display the document, primarily as a preview before printing.

Call the PrintPreviewDialog control, also using the ShowDialog method. At the same time, you must generate an instance of the PrintDocument class, the document that is printed.

Note: When you use the PrintPreviewDialog control, you must already have a local or remote printer installed, or the PrintPreviewDialog component will not be able to get the appearance of the printed document.

The PrintPreviewDialog control is set by the PrinterSettings class and the PageSettings class, which is similar to the Pagedialog component and the PageSetupDialog component, respectively. In addition, the printed document specified by the document property of the PrintPreviewDialog control, acting on both the PrinterSettings class and the PageSettings class, is displayed in the preview window.

In the following code, the PrintPreviewDialog control is invoked through the Click event of the Button control. The printed document is specified in the Documents property. Note: The printed document is not specified in the code.

This example assumes a Button control named Button1, a PrintDocument component named MyDocument, and a PrintPreviewDialog control named PrintPreviewDialog1.

' Visual Basic
Private Sub button1_click (ByVal sender as System.Object, _
ByVal e as System.EventArgs) Handles Button1.Click
' The print document ' MyDocument ' used below
' is merely for a example.
' You'll have to specify your own print document.
Printpreviewdialog1.document = MyDocument
Printpreviewdialog1.showdialog ()
End Sub

C#
private void Button1_Click (object sender, System.EventArgs e)
{
The print document ' MyDocument ' used below
is merely to an example.
You'll have to specify your own print document.
Printpreviewdialog1.document = myDocument;
Printpreviewdialog1.showdialog ()
}

Summary
The. NET Framework contains various common dialog boxes familiar to Windows users, so it becomes easier to provide interactivity in your application. Often, dialog boxes are used in a variety of ways; the. NET Framework provides open support for this, and you can choose the best solution to suit the needs of your application. We introduced some simple applications related to dialog box components. You can use this code directly, or you can modify it slightly for your application.




Related Article

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.