104. 초간단 노트패드(메모장) 만들기
.NET프로그래밍/WinForm 2009. 8. 24. 15:54 |
==> MainForm.cs
// 초간단 노트패드
private void miNotepad_Click(object sender, EventArgs e)
{
FrmNotepad fn = new FrmNotepad();
fn.MdiParent = this;
fn.Show();
}
==============================================================================================
==> FrmNotepad.cs
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; // 파일 처리
namespace MyWinForms.Sample
{
public partial class FrmNotepad : Form
{
public FrmNotepad()
{
InitializeComponent();
}
private void btnOpen_Click(object sender, EventArgs e)
{
openFileDialog1.Filter = "텍스트파일|*.txt"; // 코드레벨 설정
if (openFileDialog1.ShowDialog() == DialogResult.OK) {
// 텍스트 파일 열기
StreamReader sr = new StreamReader(
openFileDialog1.FileName, Encoding.Default);
// 데이터 읽어오기
txtNote.Text = sr.ReadToEnd(); // 텍스트 모두 열기
// 파일 닫기
sr.Close();
}
}
private void btnSave_Click(object sender, EventArgs e)
{
saveFileDialog1.Filter = "텍스트파일|*.txt";
if (saveFileDialog1.ShowDialog() != DialogResult.Cancel) {
StreamWriter sw = new StreamWriter(
saveFileDialog1.FileName, false, Encoding.Default);
sw.Write(txtNote.Text);
sw.Close();
}
}
}
}
< 실행결과 >
--> '초간단노트패드' 클릭후 화면('파일 열기'와 '파일 저장' 버튼 아래있는 텍스트박스는 'FrmNotepad.cs [디자인]' 에서 속성을 Dock ->Bottom , Multiline -> True로 설정해 주었다.)
--> 파일형식이 "텍스트파일"인 파일만 열 때 화면("텍스트파일"로 필터링 설정했음). '파일열기'버튼을 누른 후 '텍스트파일'을 선택하여 열면 해당 텍스트파일의 텍스트(내용)이 버튼 아래있는 '텍스트박스'에 열린다.
--> 파일형식이 "텍스트파일"인 파일만 저장할 때 화면("텍스트파일"로 필터링 설정했음). '파일열기' 및 '파일저장'버튼 아래에 있는 '텍스트박스'에 텍스트를 입력한 후 '파일저장'버튼을 누르면 '텍스트파일'로 입력한 내용이 저장된다.
'.NET프로그래밍 > WinForm' 카테고리의 다른 글
107. StatusStrip에서 NotifyIcon(트레이에 작은 아이콘 표시) / Timer(시간표시) / StatusLabel(레이블로 텍스트 작성) (0) | 2009.08.27 |
---|---|
(테스트) 급여 처리 프로그램 (0) | 2009.08.27 |
103. 파일열기(OpenFileDialog) / 파일저장(SaveFileDialog) / 폴더열기(FolderBrowserDialog) 대화상자 (0) | 2009.08.24 |
102. 글꼴대화상자(FontDialog) / 색상대화상자(ColorDialog) (0) | 2009.08.24 |
101. DialogResult (부모폼과 자식폼의 값 전달) (0) | 2009.08.24 |