프로그램/C# - Study / / 2010. 10. 26. 09:17

C# - Ex05 - 메모장

반응형







































using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;

namespace Ex05
{
    public partial class Form1 : Form
    {
        private String currentFile = string.Empty;

        public Form1()
        {
            InitializeComponent();
            if (File.Exists("settings.dat"))
            {
                FileStream stream = new FileStream("settings.dat", FileMode.Open);
                BinaryFormatter formatter = new BinaryFormatter();
                Font font = (Font)formatter.Deserialize(stream);
                stream.Close();

                txtContent.Font = font;
                
            }
        }
        //새로만들기메뉴
        private void miNew_Click(object sender, EventArgs e)
        {
            if (txtContent.Modified)
            {
                DialogResult result = MessageBox.Show(
                    "파일의 내용이 변경되었습니다.\r\n\r\n" +
                    "변경된 내용을 저장하시겠습니까?", //표시문자열
                    "메모장", // 제목
                    MessageBoxButtons.YesNoCancel);//버튼종류
                if (result == DialogResult.Yes)
                {
                    if (string.IsNullOrEmpty(currentFile))
                    {
                        //파일저장
                        SaveFileDialog dlg2 = new SaveFileDialog();
                        dlg2.FileName = "*.txt"; // 파일 초기 이름
                        //표시될문자열|필터링파일종류;필터링파일종류;....
                        dlg2.Filter = "텍스트문서 (*.txt)|*.txt|모든 파일(*.*)|*.*";
                        if (dlg2.ShowDialog() == DialogResult.OK)
                        {
                            currentFile = dlg2.FileName;
                        }
                        else
                        {
                            return;
                        }
                    }
                    StreamWriter writer = new StreamWriter(currentFile);
                    writer.Write(txtContent.Text);
                    writer.Close();
                }
                else if (result == DialogResult.Cancel)
                {
                    return;
                }
            }
            txtContent.Clear();
            currentFile = string.Empty;
        }
        //열기메뉴
        private void miOpen_Click(object sender, EventArgs e)
        {
            if (txtContent.Modified) // 텍스트박스가 수정 되었는지 확인
            {
                DialogResult result = MessageBox.Show(
                    "파일의 내용이 변경되었습니다.\r\n\r\n" +
                    "변경된 내용을 저장하시겠습니까?", //표시문자열
                    "메모장", // 제목
                    MessageBoxButtons.YesNoCancel);//버튼종류
                if (result == DialogResult.Yes)
                {
                    if (string.IsNullOrEmpty(currentFile))
                    {
                        //파일저장
                        SaveFileDialog dlg2 = new SaveFileDialog();
                        dlg2.FileName = "*.txt"; // 파일 초기 이름
                        //표시될문자열|필터링파일종류;필터링파일종류;....
                        dlg2.Filter = "텍스트문서 (*.txt)|*.txt|모든 파일(*.*)|*.*";
                        if (dlg2.ShowDialog() == DialogResult.OK)
                        {
                            currentFile = dlg2.FileName;
                        }
                        else
                        {
                            return;
                        }
                    }
                    StreamWriter writer = new StreamWriter(currentFile);
                    writer.Write(txtContent.Text);
                    writer.Close();
                }
                else if (result == DialogResult.Cancel)
                {
                    return;
                }

                miSave_Click(this, EventArgs.Empty);
            }
            OpenFileDialog dlg = new OpenFileDialog();
            dlg.FileName = "*.txt";
            dlg.Filter = "텍스트문서 (*.txt)|*.txt|모든 파일(*.*)|*.*";

            if (dlg.ShowDialog() == DialogResult.OK)
            {
                StreamReader reader = new StreamReader(dlg.FileName);
                txtContent.Text = reader.ReadToEnd();
                reader.Close();

                currentFile = dlg.FileName;
            }
        }
        //저장메뉴
        private void miSave_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(currentFile))
            {
                //파일저장
                SaveFileDialog dlg = new SaveFileDialog();
                dlg.FileName = "*.txt"; // 파일 초기 이름
                //표시될문자열|필터링파일종류;필터링파일종류;....
                dlg.Filter = "텍스트문서 (*.txt)|*.txt|모든 파일(*.*)|*.*";
                if (dlg.ShowDialog() == DialogResult.OK)
                {
                    currentFile = dlg.FileName;
                }
                else
                {
                    return;
                }
            }
            StreamWriter writer = new StreamWriter(currentFile);
            writer.Write(txtContent.Text);
            writer.Close();
        }
        //다른이름으로 저장
        private void miSaveAs_Click(object sender, EventArgs e)
        {
            SaveFileDialog dlg = new SaveFileDialog();
            dlg.FileName = "*.txt"; // 파일 초기 이름
            //표시될문자열|필터링파일종류;필터링파일종류;....
            dlg.Filter = "텍스트문서 (*.txt)|*.txt|모든 파일(*.*)|*.*";
            if (dlg.ShowDialog() == DialogResult.OK)
            {
                currentFile = dlg.FileName;
            }
            else
            {
                return;
            }
            StreamWriter writer = new StreamWriter(currentFile);
            writer.Write(txtContent.Text);
            writer.Close();
        }
        // 끝내기
        private void miEx_Click(object sender, EventArgs e)
        {
            if (txtContent.Modified)
            {
                DialogResult result = MessageBox.Show(
                    "파일의 내용이 변경되었습니다.\r\n\r\n" +
                    "변경된 내용을 저장하시겠습니까?", //표시문자열
                    "메모장", //제목
                    MessageBoxButtons.YesNoCancel);//버튼종류
                if (result == DialogResult.Yes)
                {
                    if (string.IsNullOrEmpty(currentFile))
                    {
                        SaveFileDialog dlg2 = new SaveFileDialog();
                        dlg2.FileName = "*.txt";
                        dlg2.Filter =
                            //표시될문자열|필터링파일종류;필터링파일종류;...
                            "텍스트문서(*.txt)|*.txt|모든 파일(*.*)|*.*";
                        if (dlg2.ShowDialog() == DialogResult.OK)
                        {
                            currentFile = dlg2.FileName;
                        }
                        else
                        {
                            return;
                        }
                    }
                    StreamWriter writer =
                            new StreamWriter(currentFile);
                    writer.Write(txtContent.Text);
                    writer.Close();
                }
                else if (result == DialogResult.Cancel)
                    return;
            }

            Close();
        }
        //줄 바꿈
        private void miWrap_Click(object sender, EventArgs e)
        {
            txtContent.WordWrap = !txtContent.WordWrap;
        }
        //글꼴
        private void miFont_Click(object sender, EventArgs e)
        {
            FontDialog dlg = new FontDialog();
            dlg.Font = txtContent.Font;
            if (dlg.ShowDialog() == DialogResult.OK)
            {
                txtContent.Font = dlg.Font;
                FileStream stream = new FileStream("settings.dat", FileMode.Create);
                BinaryFormatter formatter = new BinaryFormatter();
                formatter.Serialize(stream, dlg.Font);
                stream.Close();
            }
        }
        //폼이 꺼질떄
        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
              if (txtContent.Modified)
            {
                DialogResult result = MessageBox.Show(
                    "파일의 내용이 변경되었습니다.\r\n\r\n" +
                    "변경된 내용을 저장하시겠습니까?", //표시문자열
                    "메모장", //제목                                 //아이콘
                    MessageBoxButtons.YesNoCancel, MessageBoxIcon.Exclamation);//버튼종류
                if (result == DialogResult.Yes)
                {
                    if (string.IsNullOrEmpty(currentFile))
                    {
                        SaveFileDialog dlg2 = new SaveFileDialog();
                        dlg2.FileName = "*.txt";
                        dlg2.Filter =
                            //표시될문자열|필터링파일종류;필터링파일종류;...
                            "텍스트문서(*.txt)|*.txt|모든 파일(*.*)|*.*";
                        if (dlg2.ShowDialog() == DialogResult.OK)
                        {
                            currentFile = dlg2.FileName;
                        }
                        else
                        {
                            e.Cancel = true;//종료 작업 중지
                            return;
                        }
                    }
                    StreamWriter writer =
                            new StreamWriter(currentFile);
                    writer.Write(txtContent.Text);
                    writer.Close();
                }
                else if (result == DialogResult.Cancel)
                    e.Cancel = true;//종료 작업 중지
            }

            //Close();      
        }
    }
}

반응형

'프로그램 > C# - Study' 카테고리의 다른 글

C# - Ex06 - 그림판2  (3) 2010.10.27
C# - Ex06 - 그림판  (0) 2010.10.27
C# - Ex04 - 탐색기 만들기  (3) 2010.10.22
EX13 - Microsoft Enterprise Library 사용해 간단하게 DB 자료 가져오기  (0) 2010.10.21
EX12  (0) 2010.10.21
  • 네이버 블로그 공유
  • 네이버 밴드 공유
  • 페이스북 공유
  • 카카오스토리 공유