117. 초간단 메모장에 복사(Copy) / 붙여넣기(Paste) 기능 추가하기
.NET프로그래밍/WinForm 2009. 8. 31. 11:00 |
==> Program.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
namespace MyWinForms
{
static class Program
{
/// <summary>
/// 해당 응용 프로그램의 주 진입점입니다.
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Sample.FrmNotepad());
}
}
}
==============================================================================================
==> 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();
}
}
private void btnClick(object sender, EventArgs e)
{
Button btn = sender as Button;
if (btn == btnCopy) // 복사 버튼이라면
{
// 클립보드에 선택된 텍스트를 복사(저장)
//Clipboard.SetText(this.txtNote.SelectedText);
// 객체형으로 저장
Clipboard.SetDataObject(txtNote.SelectedText, true);
}
else
{
// 현재 클립보드의 텍스트를 붙여넣기
//this.txtNote.Text = Clipboard.GetText();
IDataObject ido = Clipboard.GetDataObject();
if (ido.GetDataPresent(typeof(string)))
{
txtNote.Text = (string)ido.GetData(typeof(string));
}
}
}
}
}
< 실행결과 >
--> 아래 텍스트에서 특정부분을 선택하여 "복사"버튼을 누름.
--> "붙여넣기"버튼을 눌렀을 때 복사하였던 부분이 출력됨.
'.NET프로그래밍 > WinForm' 카테고리의 다른 글
119. 초간단 메모장에서 "인쇄 미리 보기" 기능 구현하기 (0) | 2009.08.31 |
---|---|
118. 초간단 메모장에 파일드롭(Drag & Drop) 기능 추가하기 (0) | 2009.08.31 |
116. 툴팁(ToolTip) (0) | 2009.08.31 |
115. 탭 컨트롤(TabControl) (0) | 2009.08.31 |
114. 리스트뷰(ListView)와 트리뷰(TreeView)를 이용해서 간단한 윈도우탐색기 만들기 (0) | 2009.08.28 |