How to use Dialog Box Controls

In this tutorial we are going to show you how to use DialogBox control with VisualC#.NET language. 

Create a new project in Visual Studio 2010 choose Windows Forms Application, name it  and add five buttons, two labels and one textbox. Now change the name and text for the controls.


The project should look like this:


Five DialogBox Controls


  • There are five DialogBox controls:

How to use DialogBox Controls C# Tutorial


Take all the five Dialog Controls from the Toolbox which present the design window.

How to use DialogBox Controls C# Tutorial


  • ColorDialog: Double click on Choose Color  button and write the code.

private void btnColorDialog_Click(object sender, EventArgs e)
{
     colorDialog1.ShowDialog();
     lblColor1.BackColor = colorDialog1.Color;

}









Click over the button Choose Color and a new windows tab will appear to choose the colors:

How to use DialogBox Controls C# Tutorial


Select the color and click OKColor of label1 changes to red.   

                 
How to use DialogBox Controls C# Tutorial

  • FontDialog: Double click on the button Font Dialog and write the code.
private void btnFontDialog_Click(object sender, EventArgs e)
{
    fontDialog1.ShowDialog();
    Font font = fontDialog1.Font;
    lblColor2.Font = font;

}











Run the project when you click on the button Font Dialog and you will get this window.

How to use DialogBox Controls C# Tutorial

Select the required font and size and then click OK.

·         SaveFile: In the button event write the code.
private void btnSaveFile_Click(object sender, EventArgs e)
{
   saveFileDialog1.ShowDialog();

}









Now double click on the Save File Dialog Controls and write the code below:
private void saveFileDialog1_FileOk(object sender,CancelEventArgs e)
{
    string name = saveFileDialog1.FileName;
    File.WriteAllText(name, textBox1.Text);

}



How to use DialogBox Controls C# Tutorial

Click on Save File button after writing in the textbox and you will get:

How to use DialogBox Controls C# Tutorial


Name the file with .txt extension and click on save.

You will get the .txt file on your desktop and open the file you will get the output.

How to use DialogBox Controls C# Tutorial


OpenFileDialog: You want to open the saved file, write the code in the button Folder Browser:


private void btnFolderBrowser_Click(object sender, EventArgs e)
{
    openFileDialog1.ShowDialog();
    string file = openFileDialog1.FileName;
    string text = File.ReadAllText(file);
    textBox1.Text = text;

}











Run the project click on the button Folder Browser you will get the window.

How to use DialogBox Controls C# Tutorial



The file you saved before “TutorialsCode.txt” select it, click on the open button and you will get the output in the textbox.

How to use DialogBox Controls C# Tutorial


FolderBrowser: To browse the folder, double click in Open Dialog button and write this code.

private void btnOpenDialog_Click(object sender, EventArgs e)
{
    folderBrowserDialog1.ShowDialog();
}

How to use DialogBox Controls C# Tutorial


If you need any Help feel free to leave a comment (or) contact us.





The Application


No comments:

Post a Comment