c# winform 드래그앤드롭

STUDY/C#|2022. 7. 25. 00:28
public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }

    private void Form1_DragDrop(object sender, DragEventArgs e)
    {
        if (e.Data.GetDataPresent(DataFormats.FileDrop))
        {
            try
            {
                var directoryName = (string[])e.Data.GetData(DataFormats.FileDrop);
                var temp = e.Data.GetDataPresent(DataFormats.FileDrop);

                Text = directoryName[0].Substring(directoryName[0].LastIndexOf('\\') + 1);
                this.textBox1.Clear();
                this.textBox1.Text = directoryName[0].ToString();
            }
            catch (System.Exception a)
            {
                MessageBox.Show(a.Message);
            }
        }
    }

    private void Form1_QueryContinueDrag(object sender, QueryContinueDragEventArgs e)
    {
        if (e.EscapePressed)
        {
            e.Action = DragAction.Cancel;
        }
    }

    private void Form1_DragEnter(object sender, DragEventArgs e)
    {
        DragDropEffects effects = DragDropEffects.None;
        if (e.Data.GetDataPresent(DataFormats.FileDrop))
        {
            var path = ((string[])e.Data.GetData(DataFormats.FileDrop))[0];
            if (Directory.Exists(path))
                effects = DragDropEffects.Copy;
        }

        e.Effect = effects;
    }
}

'STUDY > C#' 카테고리의 다른 글

특정 키워드 모음  (0) 2024.07.07
대리자(Delegate)  (0) 2024.07.07
메소드(Method)  (0) 2024.07.07
Null 조건부, 병합 연산자  (0) 2023.02.20
C# Nullable Type  (0) 2023.02.20

댓글()