https://www.c-sharpcorner.com/article/dialog-boxes-in-c-sharp/
아놔... 기본적인것도 모르고 지금껏 잘도 짜왔구나... ㅠ
What is a dialog box?
- A dialog box is a type of window, which is used to enable common communication or dialog between a computer and its user.
- A dialog box is most often used to provide the user with the means for specifying how to implement a command or to respond to a question.
- Windows.Form is a base class
Sometimes, in a graphical user interface, a window is used to communicate with the user or establish a dialog between the user and the application. This additional window is called a dialog box. It may communicate information to the user; prompt the user for a response or both.
The simplest type of dialog box is the warning which displays a message and may require the user to acknowledge that the message has been read, usually by clicking “OK” or a decision as to whether or not an action should continue by clicking “OK” or “Cancel”.
Some dialog boxes are standard like the warning message or error message. Save the file and enter the password. These are called standard dialog boxes.
A dialog box can also be customized. Such a dialog box is called a custom dialog box.
Dialog boxes are special forms that are non-resizable. They are also used to display the messages to the user. The messages can be error messages, confirmation of the password, confirmation for the deletion of a particular record, Find-Replace utility of the word etc. There are standard dialog boxes to open and save a file, select a folder, print the documents, set the font or color for the text, etc.
MessageBox class is used to display messages to the user. The show() method is used to display a message box with the specified text, caption, buttons and icon. There are other overloads also available.
For example
- DialogResult res = MessageBox.Show("Are you sure you want to Delete", "Confirmation", MessageBoxButtons.OKCancel, MessageBoxIcon.Information);
- if (res == DialogResult.OK) {
- MessageBox.Show("You have clicked Ok Button");
- //Some task…
- }
- if (res == DialogResult.Cancel) {
- MessageBox.Show("You have clicked Cancel Button");
- //Some task…
- }
Type of buttons and icons can be changed by using the appropriate enumerated values.
Dialog boxes are of two types, which are given below.
- Modal dialog box
- Modeless dialog box
A dialog box that temporarily halts the application and the user cannot continue until the dialog has been closed is called modal dialog box. The application may require some additional information before it can continue or may simply wish to confirm that the user wants to proceed with a potentially dangerous course of action. The application continues to execute only after the dialog box is closed; until then the application halts. For example, when saving a file, the user gives a name of an existing file; a warning is shown that a file with the same name exists, whether it should be overwritten or be saved with different name. The file will not be saved unless the user selects “OK” or “Cancel”.
Another type of dialog box, which is used is a modeless dialog box. It is used when the requested information is not essential to continue, so the Window can be left open, while work continues somewhere else. For example, when working in a text editor, the user wants to find and replace a particular word. This can be done, using a dialog box, which asks for the word to be found and replaced. The user can continue to work, even if this box is open.
Dialog box can be configured to constant by setting FormBorderStyle property to FormBorderStyle.FixedDialog, setting MinimizeBox and MaximizeBox property to false. Framework Class Library(FCL) does not provide class as Dialog. The developer creates custom dialog classes by deriving type from System.Windows.Form base class.
Model dialog is displayed, using ShowDialog() method. Modeless dialog boxes are displayed, using Show() method.
Common Dialog Box
The dialog boxes that are used, which are common to all Windows Application. It performs the common tasks like saving a file, choosing a font etc. This provides a standard way to the Application interface.
The examples are given below.
- FontDialog
- ColorDialog
- OpenDialog
- SaveDialog
These dialog boxes are implemented by an operating system, so they can be shared across all the Application that runs on that operating system (Windows).
Steps to use common dialog box
- Instantiate the required common dialog box.
- Set the properties of common dialog box, if required.
- Call its ShowDialog() method to invoke the dialog box.
ShowDialog() returns an enumerated type called DialogResult. It defines the identifiers, which indicates which button was clicked. For example, DialogResult.OK and DialogResul.Cancel are some values that indicates OK or Cancel button were clicked respectively.
Open File Dialog Box
The OpenFileDialog allows you to choose a file to be opened in an Application.
For example, the code is given below.
- OpenFileDialog ofd = new OpenFileDialog();
- ofd.Title = "Open a Text File";
- ofd.Filter = "Text Files (*.txt) | *.txt | All Files(*.*) | *.*"; //Here you can filter which all files you wanted allow to open
- DialogResult dr = ofd.ShowDialog();
- if (dr == DialogResult.OK) {
- StreamReader sr = new StreamReader(ofd.FileName);
- txtEx.Text = sr.ReadToEnd();
- sr.Close();
- }
Save File Dialog Box
The SaveFileDialog box is used to allow the user to select the destination and name of the file to be saved.
For example, the code is given below.
- SaveFileDialog sfdlg = new SaveFileDialog();
- sfdlg.Filter = "Text Files (*.txt) | *.txt"; //Here you can filter which all files you wanted allow to open
- if (sfdlg.ShowDialog() == DialogResult.OK) {
- // Code to write the stream goes here.
- }
The content can be saved to the file, using appropriate class like StreamWriter class in the case of Text Editor Application.
Font and Color Dialog Boxes
FontDialogBox is used to allow the user to select font settings. The ColorDialogBox is used to allow the user to select a color.
For example, the code is given below.
- //Font Dialog
- FontDialog fdlg = new FontDialog();
- fdlg.ShowDialog();
- txtEx.Font = fdlg.Font;
- //Color Dialog
- ColorDialog cdlg = new ColorDialog();
- cdlg.ShowDialog();
- txtEx.ForeColor = cdlg.color;
- Font Dialog is given below.
- Color Dialog is given below.
In either case, the properties and methods are available. They can be used to get the selected values or set the values for the dialog box.
Custom Dialog Box
Even though common dialog boxes are useful, they do not support the requirements of domain-specific dialog boxes. Developer need to create their own dialog boxes.
Following steps represent the process of creating Custom Dialog Box
- Add a form to your project by right clicking the project in Solution Explorer, point to Add and then click Windows Form.
- In the properties Window, change the FormBorderStyle property to FixedDialog.
- Customize the appearance of the form, as required.
- Add controls into this form.
Note
Use ShowDialog() method to open the form as modal dialog box. This method can be used for the custom dialog boxes too. Show() method is used to open the model's dialog box.
'개발 > C#' 카테고리의 다른 글
[팁] 델리게이트, 폼간 데이터 공유(주거니 받거니) (0) | 2018.04.16 |
---|---|
[팁] 이벤트, 폼간 데이터 공유 (0) | 2018.04.16 |
[팁] 폼(클래스)간 변수공유 (0) | 2018.04.16 |
C# Resource 이미지 로드 (0) | 2018.04.03 |
[DataGirdview] 더블 버퍼링 (0) | 2018.02.23 |
[이미지] C# Picture 박스에서 다른 Picture 박스로 이미지 끌어다 놓기 (0) | 2018.02.13 |
[이미지] C# 불러온 이미지 위치 조정후 저장하기 (0) | 2018.02.07 |
[이미지] C# Picturebox 내에서 이미지 움직이기 (1) | 2018.02.07 |