본문 바로가기
C# 언어

C#으로 메모장(Notepad) 만들기

by treeCoder 2021. 9. 19.

C#으로 메모장(Notepad) 만들기이다.

 

출처는 다음과 같다. 아래의 링크를 따라가면, Youtube 동영상에 만드는 방법이 나와 있다.

 

https://foxlearn.com/articles/how-to-make-a-notepad-in-csharp-152.html

 

Windows Forms: How to make a Notepad in C#

How to Make a simple Notepad in C# Windows Form Application using MenuStrip, OpenFileDialog, SaveFileDialog, StreamReader, StreamWriter, TextBox, Button and Label

foxlearn.com

 

Code는 메인 폼에 대한 코드만 있다.

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();
            }
        }
    }

댓글