简体   繁体   English

C#(WinForm)中的DragDrop按钮文本

[英]DragDrop button text in C# (WinForm)

My goal is to have multiple buttons whose text can be switched on the screen through dragging the text from one button to another (it's not necessary that the buttons themselves be switched, only the text on them, although if it's easier to switch the buttons themselves, that's fine). 我的目标是要有多个按钮,通过将文本从一个按钮拖到另一个按钮,可以在屏幕上切换文本(不必切换按钮本身,只需要切换按钮上的文本即可,尽管这样更容易切换按钮本身, 没关系)。 I have tried to follow this MSDN article but as soon as I actually begin to drag, I get a "no" symbol (an O with a / through it). 我试图按照这篇MSDN文章进行操作,但是实际上我一开始拖动它,就会得到一个“否”符号(带有/的O)。 Am I missing something? 我想念什么吗? (Code below) (下面的代码)

Thanks in advance! 提前致谢!

public partial class Form1 : Form
{
    Button button1 = new Button();
    Button button2 = new Button();

    public Form1()
    {
        InitializeComponent();

        button1.Text = "Button 1";
        button2.Text = "Button 2";

        button2.Location = new Point(100, 0);

        this.Controls.Add(button1);
        this.Controls.Add(button2);

        button1.MouseDown += new MouseEventHandler(button_MouseDown);
        button2.MouseDown += new MouseEventHandler(button_MouseDown);

        button1.DragEnter += new DragEventHandler(button_DragEnter);
        button2.DragEnter += new DragEventHandler(button_DragEnter);

        button1.DragDrop += new DragEventHandler(button_DragDrop);
    }

    private void button_MouseDown(object sender, MouseEventArgs e)
    {
        ((Button)sender).DoDragDrop(((Button)sender).Text, DragDropEffects.Move);
    }

    private void button_DragEnter(object sender, DragEventArgs e)
    {
        e.Effect = DragDropEffects.Move;
    }

    private void button_DragDrop(object sender, DragEventArgs e)
    {
        //I'm not sure what goes here, but I can figure that out through experimentation
    }
}

So it turns out I just had to do some more research. 事实证明,我只需要做更多研究。 Adding button.AllowDrop = true for both buttons solved the issue 为两个按钮添加button.AllowDrop = true解决了该问题

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM