How to handle drag and drop tags in C #?

I am trying to create a form where a user can drag a shortcut and put it in a text box. I can find AllowDrop in the text box, but there is no property in the shortcut like "AllowDrag". In addition, I created methods for all drag and drop events for the label (DragEnter, DragLeave, etc.), but none of them work. I can’t figure out how to drag. How to do it?

        private void label1_Click(object sender, EventArgs e)
    {

        // This one works
        status.Text = "Click";
    }

        private void label1_DragOver(object sender, DragEventArgs e)
    {

        // this and the others do not
        status.Text = "DragOver";
    }

    private void label1_GiveFeedback(object sender, GiveFeedbackEventArgs e)
    {
        status.Text = "GiveFeedback";
    }

    private void label1_DragDrop(object sender, DragEventArgs e)
    {
        status.Text = "DragDrop";
    }

    private void label1_DragEnter(object sender, DragEventArgs e)
    {
        status.Text = "DragEnter";
    }

    private void label1_DragLeave(object sender, EventArgs e)
    {
        status.Text = "DragLeave";
    }

    private void label1_QueryContinueDrag(object sender, QueryContinueDragEventArgs e)
    {
        status.Text = "QueryContinueDrag";
    }
+3
source share
2 answers

There is no AllowDrag property, you are actively starting D + D using the DoDragDrop () method. And event handlers should be on the D + D target, not the source. Sample form, it needs a label and a text field:

  public partial class Form1 : Form {
    public Form1() {
      InitializeComponent();
      label1.MouseDown += new MouseEventHandler(label1_MouseDown);
      textBox1.AllowDrop = true;
      textBox1.DragEnter += new DragEventHandler(textBox1_DragEnter);
      textBox1.DragDrop += new DragEventHandler(textBox1_DragDrop);
    }

    void label1_MouseDown(object sender, MouseEventArgs e) {
      DoDragDrop(label1.Text, DragDropEffects.Copy);
    }
    void textBox1_DragEnter(object sender, DragEventArgs e) {
      if (e.Data.GetDataPresent(DataFormats.Text)) e.Effect = DragDropEffects.Copy;
    }
    void textBox1_DragDrop(object sender, DragEventArgs e) {
      textBox1.Text = (string)e.Data.GetData(DataFormats.Text);
    }
  }
+16

, bool, , , false, , mousemove , bool - true.

.

+2

Source: https://habr.com/ru/post/1722852/


All Articles