오랜만에 작성하는 포스팅이다.
회사생활을 하던 중 필요할것같은 프로그램을 만들자라는 생각이 들어 오늘부터 시작했다.
(사실 전체 디자인도 안했다.......망했다...)
아무튼 Rich Text Box에서 File Drag & Drop을 구현을 하였다.
원래 C#에서 쉽게 구현할 수 있을 것이라 생각하면서 시작하였으나, 바로 문제점이 보이기 시작하였다..
AllowDrop이라는 멤버변수를 찾지 못하겠는것이다..

봐라... 없다....
내 Development환경이 이상한건지 아니면 원래 없는것인지 모르겠으나, 아무튼 꼭 Design 창에서만 Allow Drop을 설정할수 있는것은 아니기에, Code에서 직접 넣기로하였다.
Setting 할것은 3가지이다(Design창에서 AllowDrop설정을 하였다면 2가지..)
첫째. AllowDrop 활성화 및 이벤트 연결
둘째. DragEnter 이벤트 입력
셋째. DragDrop 이벤트 입력
그럼 바로 Code 공개
1. AllowDrop 활성화 및 이벤트 연결
public frmMain()
{
InitializeComponent();
txtFileList.AllowDrop = true;
txtFileList.DragDrop += new DragEventHandler(txtFileList_DragDrop);
txtFileList.DragEnter += new DragEventHandler(txtFileList_DragEnter);
}
2. DragEnter 이벤트 입력
void txtFileList_DragEnter(object sender, DragEventArgs e)
{
if (e.Data.GetDataPresent(DataFormats.FileDrop))
{
e.Effect = DragDropEffects.Copy | DragDropEffects.Scroll;
}
}
3. DragDrop 이벤트 입력
void txtFileList_DragDrop(object sender, DragEventArgs e)
{
if (e.Data.GetDataPresent(DataFormats.FileDrop))
{
string[] strFiles = (string[])e.Data.GetData(DataFormats.FileDrop);
foreach (string strFile in strFiles)
{
txtFileList.Text += strFile + "\r\n";
}
}
}
사실 Code는 정말 쉽다.. 그래서 주석도 없다(변명이다.).. 그리고 중간 부분에 Rich Text Box에서 구현하였다고하였으나,,왠만한 모든 Form Control에는 Drag Drop을 할수있도록 멤버변수 및 메서드가 있으니, 가져다 쓰기만 하면 될것이다.
또한 Drag Drop 이벤트 입력에서 foreach 문을 써 String 하나씩 입력하였으나, 이것은 앞으로의 내 프로그램의 확장성을 고려하여 이렇게 입력하였고, "txtFileList.Lines = strFiles;" <--이렇게 입력하여도 된다.
아무튼 성의를 보이고 싶어도 보일 수 없는 File Drag&Drop 이다.