C#拖放标签FlowLayoutPanels内 [英] C# Drag and Drop labels within FlowLayoutPanels

查看:153
本文介绍了C#拖放标签FlowLayoutPanels内的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有个小问题。我希望做节目,我可以多FlowLayoutPanels之间拖动生成的标签。但最近几天我曾试图弥补拖放工作。我试过很多教程,范例等,但它总是有一些有点diferent,我不能提取物只有基本的代码。

I have small problem. I want make program where I can drag generated labels between multiple FlowLayoutPanels. But last few days I have tried to make drag and drop working. I tried many tutorials, examples etc. but it is always something bit diferent and I am not able extract only basic code.

这是一个类似的这个计划,但它是在Visual Basic中,我需要它在C#。我知道这也许是很简单,但我是新手。

It is similar to this program but it is in Visual Basic and I need it in C#. I know it is maybe very simple, but I am newbie.

感谢您的帮助。

推荐答案

真正的拖放放大器;掉落北京时间大多数应用程序之间以及表格之间有用的,也许还

Real Drag&Drop ist most useful between applications and maybe also between Forms.

假设你想要的一串FLP 拖动标签的同样表格,下面的代码应该让你去..

Assuming you want to drag Labels between FLPs on the same Form, the code below should get you going..

这需要两个 FlowLayoutPanels 名为 FLP1 FLP2 用几标签。

It takes two FlowLayoutPanels called FLP1 and FLP2 and starts by initializing them with a few Labels.

请注意三个鼠标事件我添加到每个标签来模仿拖放放大器;掉落行动吧!

Note the three mouse events I add to each Label to emulate a Drag&Drop action!

private void Form1_Load(object sender, EventArgs e)
{
    // create a few Label with varying Colors for testing..
    fillFLP(FLP1, 88);
    fillFLP(FLP2, 111);
}

void fillFLP(FlowLayoutPanel FLP, int cc)
{
    for (int i = 0; i < 24; i++)
    {
        Label l = new Label();
        // the next 3 lines optional and only are there for testing!
        l.AutoSize = false;      
        l.Text = FLP.Name + " " +  i.ToString("00");
        l.BackColor = Color.FromArgb(255, cc * 2 - i, 255 - 5 * i, cc + 5 * i); 
        // add controls and set mouse events:
        FLP.Controls.Add(l);
        l.MouseDown += l_MouseDown;
        l.MouseMove += l_MouseMove;
        l.MouseUp += l_MouseUp;
    }
}


// the currently moved Label:
Label mvLabel = null;

void l_MouseDown(object sender, MouseEventArgs e)
{
    // keep reference
    mvLabel = (Label)sender;
}

void l_MouseMove(object sender, MouseEventArgs e)
{
    // if we are dragging a label:
    if (mvLabel != null)
    {
        // mouse pos in window coords
        Point  mvPoint = this.PointToClient(Control.MousePosition);
        // the label is still in the FLP, so we start the drg action:
        if (mvLabel.Parent != this)
        {
            mvLabel.Parent = this;
            mvLabel.Location = mvPoint;
            mvLabel.BringToFront();
        }
        else
        {   
            // we are already in the form, so we just move
            mvLabel.Location = mvPoint;
        }
    }
}

void l_MouseUp(object sender, MouseEventArgs e)
{
    // are we over a FLP? and if so which?
    Point MP = Control.MousePosition;
    FlowLayoutPanel FLP = null;

    Point mLoc1 = FLP1.PointToClient(MP);
    Point mLoc2 = FLP2.PointToClient(MP);

    if (FLP1.ClientRectangle.Contains(mLoc1)) FLP = FLP1;
    else if (FLP2.ClientRectangle.Contains(mLoc2)) FLP = FLP2;
    else return;  // no! nothing we can do..

    // yes, now find out if we are over a label..
    // ..or over an empty area
    mvLabel.SendToBack();
    Control cc = FLP.GetChildAtPoint(FLP.PointToClient(MP));
    // if we are over the FLP we can insert at the beginning or the end:
    // int mvIndex = 0; // to the beginning
    int mvIndex = FLP.Controls.Count; // to the end
    // we are over a Label, so we insert before it:
    if (cc != null) mvIndex = FLP.Controls.IndexOf(cc);

    // move the Label into the FLP
    FLP.Controls.Add(mvLabel);
    // move it to the right position:
    FLP.Controls.SetChildIndex(mvLabel, mvIndex);
    // let go of the reference
    mvLabel = null;

}

这可以让你拖放标贴来来回回两者之间的一串FLP ,并在一串FLP 通过删除 标签

This lets you drag and drop Lables to and fro between two FLPs and also within the FLPs by dropping onto Labels.

请注意,您将需要一些额外的行,如果你想允许下探 标签,仍然定位那里..

Note that you will need a few extra lines if you want to allow dropping between Labels and still position there..

这篇关于C#拖放标签FlowLayoutPanels内的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆