STUDY/C#

c# winform 드래그앤드롭

빵아찌 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;
    }
}