Windows Forms: How to make a Notepad in C#
By FoxLearn 6/2/2017 10:31:55 PM 7.11K
How to Make a simple Notepad in C# Windows Form Application using MenuStrip, OpenFileDialog, SaveFileDialog, StreamReader, StreamWriter, TextBox, Button and Label
Step 1: Click New Project, then select Visual C# on the left, then Windows and then select Windows Forms Application. Name your project "Notepad" and then click OK
Step 2: Design your form as below
Main Form Design
About Form Design
Add code to handle main form
public partial class Form1 : Form { string path; public Form1() { InitializeComponent(); } private void newToolStripMenuItem_Click(object sender, EventArgs e)//New file { path = string.Empty; textBox.Clear();//Clear data in textbox } private void openToolStripMenuItem_Click(object sender, EventArgs e)//Open file { using (OpenFileDialog ofd = new OpenFileDialog() { Filter = "Text Documents|*.txt", ValidateNames = true, Multiselect = false }) { if (ofd.ShowDialog() == DialogResult.OK) { try { using (StreamReader sr = new StreamReader(ofd.FileName)) { path = ofd.FileName; Task<string> text = sr.ReadToEndAsync();//Read data from text file textBox.Text = text.Result;//Set data to textbox } } catch (Exception ex) { MessageBox.Show(ex.Message, "Message", MessageBoxButtons.OK, MessageBoxIcon.Error); } } } } private async void saveToolStripMenuItem_Click(object sender, EventArgs e)//Save file { if (string.IsNullOrEmpty(path)) { using (SaveFileDialog sfd = new SaveFileDialog() { Filter = "Text Documents|*.txt", ValidateNames = true }) { if (sfd.ShowDialog() == DialogResult.OK) { try { path = sfd.FileName; using (StreamWriter sw = new StreamWriter(sfd.FileName)) { await sw.WriteLineAsync(textBox.Text);//Write data to text file } } catch (Exception ex) { MessageBox.Show(ex.Message, "Message", MessageBoxButtons.OK, MessageBoxIcon.Error); } } } } else { try { using (StreamWriter sw = new StreamWriter(path)) { await sw.WriteLineAsync(textBox.Text);//Write data to text file } } catch (Exception ex) { MessageBox.Show(ex.Message, "Message", MessageBoxButtons.OK, MessageBoxIcon.Error); } } } private async void saveAsToolStripMenuItem_Click(object sender, EventArgs e)//Save as { //Open save file dialog using (SaveFileDialog sfd = new SaveFileDialog() { Filter = "Text Documents|*.txt", ValidateNames = true }) { if (sfd.ShowDialog() == DialogResult.OK) { try { using (StreamWriter sw = new StreamWriter(sfd.FileName)) { await sw.WriteLineAsync(textBox.Text);//Write data to text file } } catch (Exception ex) { MessageBox.Show(ex.Message, "Message", MessageBoxButtons.OK, MessageBoxIcon.Error); } } } } private void exitToolStripMenuItem_Click(object sender, EventArgs e) { Application.Exit();//Exit your program } private void aboutToolStripMenuItem_Click(object sender, EventArgs e) { using (frmAbout frm = new frmAbout())//Open about form { frm.ShowDialog(); } } }
VIDEO TUTORIALS
- Windows Forms: Text to speech in C#
- Windows Forms: Print Picture Box in C#
- Windows Forms: Advanced Filter DataGridView in C#
- Windows Forms: Progress Bar in C#
- How to create a Lucky Draw games in C#
- Windows Forms: Youtube Search with Paging in C#
- Windows Forms: How to Create a Random Password Generator in C#
- Windows Forms: Tag Control in C#
Categories
Popular Posts
Visual Studio: Auto increment version
04/01/2024
What is ARM architecture?
04/01/2024