Search Chirayu Software Solutions Blog

Thursday, September 13, 2012

Dilaog box in .net


Standalone applications typically have a main window that both displays the main data over which the application operates and exposes the functionality to process that data through user interface (UI) mechanisms like menu bars, tool bars, and status bars. A non-trivial application may also display additional windows to do the following:
  • Display specific information to users.
  • Gather information from users.
  • Both display and gather information.
These types of windows are known as dialog boxes, and there are two types: modal and modeless.

modal dialog box is displayed by a function when the function needs additional data from a user to continue. Because the function depends on the modal dialog box to gather data, the modal dialog box also prevents a user from activating other windows in the application while it remains open. In most cases, a modal dialog box allows a user to signal when they have finished with the modal dialog box by pressing either an OK or Cancel button. Pressing the OK button indicates that a user has entered data and wants the function to continue processing with that data. Pressing the Cancel button indicates that a user wants to stop the function from executing altogether. The most common examples of modal dialog boxes are shown to open, save, and print data.
modeless dialog box, on the other hand, does not prevent a user from activating other windows while it is open. For example, if a user wants to find occurrences of a particular word in a document, a main window will often open a dialog box to ask a user what word they are looking for. Since finding a word doesn't prevent a user from editing the document, however, the dialog box doesn't need to be modal. A modeless dialog box at least provides a Close button to close the dialog box, and may provide additional buttons to execute specific functions, such as a Find Next button to find the next word that matches the find criteria of a word search.


1. Font Dialog


FontDialog presents a font selection dialog box.Itallows the user to select a font from the list of installed fonts in the C# Windows Forms program. With the FontDialog control, we quickly add the ability for users to select their favorite fonts.
FontDialog screenshot


Get startedTo start using the FontDialog in a program, add a FontDialog instance, and also a Button and a TextBox to an empty Windows Form. Then, double-click on the Button to insert the button1_Click event handler. In the code for button1_Click, we call the ShowDialog method on the FontDialog instance.
Example program that opens FontDialog [C#]

using System;
using System.Drawing;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
 public Form1()
 {
     InitializeComponent();
 }

 private void button1_Click(object sender, EventArgs e)
 {
     // Show the dialog.
     DialogResult result = fontDialog1.ShowDialog();
     // See if OK was pressed.
     if (result == DialogResult.OK)
     {
  // Get Font.
  Font font = fontDialog1.Font;
  // Set TextBox properties.
  this.textBox1.Text = string.Format("Font is: {0}", font.Name);
  this.textBox1.Font = font;
     }
 }
    }
}
2.Color Dialog
ColorDialog displays a color selection window. By using the ColorDialog, you can enable a visual interface for selecting a specific color. Some options are provided through properties on this control.
ColorDialog screenshot
ExampleTo begin, we will add the ColorDialog instance to the form and then use it in some C# code. Most of the work you do with the ColorDialog will require the ShowDialog method and also the Color property. When you invoke ShowDialog, please remember to check the DialogResult; you do not want to take an action when the dialog was canceled.
Example that uses ColorDialog control [C#]

using System;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
 public Form1()
 {
     InitializeComponent();
 }

 private void Form1_Load(object sender, EventArgs e)
 {
     // Show the color dialog.
     DialogResult result = colorDialog1.ShowDialog();
     // See if user pressed ok.
     if (result == DialogResult.OK)
     {
  // Set form background to the selected color.
  this.BackColor = colorDialog1.Color;
     }
 }
    }
}
3.Save File Dialog
The save file dialog box, shown in the following figure, is used by file saving functionality to retrieve the name of a file to save.
Save As dialog box
Example

Microsoft.Win32.SaveFileDialog dlg = new Microsoft.Win32.SaveFileDialog();
dlg.FileName = "Document"; // Default file name
dlg.DefaultExt = ".text"; // Default file extension
dlg.Filter = "Text documents (.txt)|*.txt"; // Filter files by extension 

// Show save file dialog box
Nullable<bool> result = dlg.ShowDialog();

// Process save file dialog box results 
if (result == true)
{
    // Save document 
    string filename = dlg.FileName;
}



Post a Comment