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();
}
}
}
'C# 언어' 카테고리의 다른 글
| C# reading from stdin using readline function (1) | 2022.06.20 |
|---|---|
| C# echo 프로그램 만들기 (0) | 2021.11.12 |
| C# Windows Forms 항상 닫히지 않게 하기 (0) | 2021.09.19 |
| C#으로 만든 스티커 메모 (소스코드 없음) (0) | 2021.09.19 |
| C#으로 메모리 매칭 게임 만들기 (0) | 2021.09.16 |
댓글