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:
- There are five DialogBox controls:
Take all the five Dialog Controls from
the Toolbox which present the design window.
- 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:
Select
the color and click OK. Color of label1
changes to red.
- 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.
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);
}
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.
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.
The
file you saved before “TutorialsCode.txt” select it, click on
the open button and you will get the output in the textbox.
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();}
If you need any Help feel free to leave a comment (or) contact us.
The Application
No comments:
Post a Comment