How to make a Notepad in C#
By FoxLearn 10/6/2024 3:33:49 AM 7.37K
How to make a Notepad in C#
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
Drag and drop a MenuStrip
onto the form.
Add the following items to the MenuStrip
:
- File
- New
- Open
- Save
- Save As
- Exit
- Help
- About
Add a TextBox
to the form. Set its properties:
Multiline
:True
ScrollBars
:Both
Dock
:Fill
(to make it fill the form)
Main Form Design
About Form Design
Double-click on the Open menu item to create the Click
event handler.
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); } } } }
Double-click on the New menu item to create the Click
event handler.
private void newToolStripMenuItem_Click(object sender, EventArgs e)//New file { path = string.Empty; textBox.Clear();//Clear data in textbox }
Double-click on the Save menu item to create the Click
event handler.
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); } } }
Double-click on the SaveAs menu item to create the Click
event handler.
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); } } } }
Double-click on the Exit menu item to create the Click
event handler.
private void exitToolStripMenuItem_Click(object sender, EventArgs e) { Application.Exit();//Exit your program }
Double-click on the About menu item to create the Click
event handler.
private void aboutToolStripMenuItem_Click(object sender, EventArgs e) { using (frmAbout frm = new frmAbout())//Open about form { frm.ShowDialog(); } }
Your main form
public partial class Form1 : Form { string path; public Form1() { InitializeComponent(); } }
VIDEO TUTORIAL
- How to Receive SMS from WhatsApp in C#
- How to Add Combobox to DataGridView in C#
- How to Create Multiple pages on the Form using Panel control in C#
- How to insert Math Equation to RichTextBox in C#
- How to Send and Receive email in Microsoft Outlook using C#
- How to Print Windows Form in C#
- How to Use Form Load and Button click Event in C#
- How to use Advanced Filter DataGridView in C#