Social Icons

Saturday, 13 July 2013

Dialog Controls in Windows Forms, How to use ColorDialog,FolderBrowserDialog,FontDialog,OpenFileDialog,SaveFileDialog example.

Dialog Controls in Windows Forms Application:

Introduction:

In my previous articles  i explained various concepts on Asp.Net,WPF and Oops Concepts like Asp.Net Validation Controls Tutorial, Asp.Net Ajax Tutorial, WPF Features, WPF Architecture etc.

        Now we will see what are the different types of DialogControls available in Windows Forms and How to work with these Dialog Controls in Our Applications with an example.

Description:

 Dialog Controls in Window Forms help us to do some work quickly and easily and also provides us many options. In Visual Studio toolbox there is separate Tab available for these Dialog Controls in Windows Forms.



A Dialog Control Even if added to a form will not be displayed directly on the form. You will see them
in the bottom of the studio. To make them visible in run-time on the form we need to call a method
ShowDialog().

  A Dialog Control will never perform any actions to use directly. We are only interfaces to perform the actions.They are responsible in displaying the available values to choose form to end users and returns back those values to application developer. Now it is the responsibility of application developer to perform
the necessary actions with values chosen by end users.

     To capture the values that are chosen by end users application developers can make use of the following Properties.

Dialog Controls:

  • ColorDialog -  Returns the Selected Color
  • FoloderBrowserDialog - Returns the Selected Folder Path.
  • FontDialog - Returns the Selected Font.
  • OpenFileDialog - Returns the Filename
  • SaveFileDialog - Returns the Filename.

 Dialog Controls usage with an example:

  1. Take a Form and add ColorDialog,FontDialog,OpenFileDailog,SaveFileDialog, Four Buttons and a PictureBox to form. Design them as shown in below.

    Dailog Controls Example
    2. Set the Text for four buttons as shown in above image i.e Change Color, Change Font, Load Image,Save Image. 
    3. Now in the Generate the Click event of Change Color Button write below Code.

     This code will open the ColorDialog Box, end user will chose the one color then when he press ok , then we are applying that color to Button BackColor. If you Observe the code to open ColorDialog Box we are calling ShowDialog() method.

    4. Now generate the Click event of Change Font Button and write below code.

    Here also same in click event of Change Font Button we are opening FontDialog Box, User will chose one Font and Press Ok then we are applying selected Font to button Font.

    5. Now generate the click event for Load Image Button and write below code.
     
            Here we are opening OpenFileDailog Box by calling its ShowDialog() method. If you observe the we are filtering the type of files it needs to show when open using Filter Property of OpenFileDialog Control. In this button Click event user will Select an image then we are applying Selected Filename to Picturbox ImageLocation Property.

    6. Now generate click event for Save Button and Write below Code.



Here we are giving a prompt to user whether you want to save this image? If user press ok then we are opening SaveFileDialog Box then User will save the file. That's it simple.

    Whatever the windows application whether it is windows forms or wpf application, these dialog controls and their functionality is common. But in WPF applications we can't find these controls available in toolbox.

     To add these controls in WPF we have to add namespace System.Windows.Control reference to project and Import the namespace in Corresponding Window or Page.

Important Points Regarding Dialog Controls:

  • To open a DialogControl we have to Call ShowDialog() method.
  • Every DialogBox will return a value of type DialogResult, based on this value we have to go further action.
  •  For OpenFileDialog Box and SaveFileDialog Boxes Filter Property available to Filter type of documents to show.
Apart from this PrintPreviewDialog box available to show preview of your document when it available for printing. You can find other controls of Windows Forms Here.

Conclusion:

In this way we can work with or use Dialog Controls in Windows Foms.

      Don't forget to Comment and share for our friends if you like this post.


No comments:

Post a comment

Please Give your Valuable Suggestions,Questions and Comments to improve my blog.