0

这是我的课,我的面板总是被设置为空......

有人可以给我一个关于如何做到这一点的提示吗?

    [Serializable]
public class DragDropBlock : Panel
{
    public DragDropBlock()
        {
            this.MouseDown += new MouseEventHandler(Mouse_Down);
            this.MouseUp += new MouseEventHandler(Mouse_Up);
        }

        void Mouse_Down(object sender, System.Windows.Forms.MouseEventArgs e)
        {

           Clipboard.SetData("DragDropBlock", this);
        }

        void Mouse_Up(object sender, System.Windows.Forms.MouseEventArgs e)
        {
            IDataObject IBlock = Clipboard.GetDataObject();
            DragDropBlock Block = (DragDropBlock)IBlock.GetData(typeof(DragDropBlock));

        }
}
4

2 回答 2

0

给定一个类:

[Serializable]
class Test
{
    public string Data
    {
        get;
        set;
    }
}

这有效:

Test t = new Test()
{
     Data = "DERP!"
};

Clipboard.SetData("Test", t);

Test newT = (Test)Clipboard.GetData("Test");

Console.WriteLine(newT.Data);

如果你想使用数据对象:

Test t = new Test()
{
    Data = "DERP!"
};

Clipboard.SetDataObject(new DataObject("Test", t));

Test newT = (Test)Clipboard.GetDataObject().GetData("Test");

Console.WriteLine(newT.Data);

两者的输出是:

DERP!

于 2013-06-13T00:06:30.160 回答
0

这是我班的更正:工作!!!

         [Serializable]
class DragBlock
{
    public string Data
    {
        get;
        set;
    }
}

public class DragDropBlock : Panel
{
    DragBlock Block;

    public DragDropBlock()
        {
            this.MouseDown += new MouseEventHandler(Mouse_Down);
            this.MouseUp += new MouseEventHandler(Mouse_Up);

            Block = new DragBlock()
            {
                Data = "TEST!"
            };
        }

        void Mouse_Down(object sender, System.Windows.Forms.MouseEventArgs e)
        {
            Clipboard.SetDataObject(new DataObject("DragBlock", Block));
        }

        void Mouse_Up(object sender, System.Windows.Forms.MouseEventArgs e)
        {
            DragBlock newBlock = (DragBlock)Clipboard.GetDataObject().GetData("DragBlock");

            Console.WriteLine(newBlock.Data);
    }
}
于 2013-06-13T00:54:55.400 回答