FlowLayoutPanels 中的 C# 拖放标签

2024-06-28

我有一个小问题。我想要制作一个程序,可以在多个 FlowLayoutPanel 之间拖动生成的标签。但最近几天我尝试让拖放工作。我尝试了很多教程、示例等,但总是有些不同,而且我无法仅提取基本代码。

它类似于这个程序 https://social.msdn.microsoft.com/Forums/en-US/c3ddb8d3-cea6-41d9-9276-94ddba49dc5d/drag-and-drop-groupbox-panels但它是 Visual Basic 中的,我需要 C# 中的它。我知道这可能很简单,但我是新手。

谢谢你的帮助。


Real Drag&Drop在应用程序之间或者在应用程序之间也是最有用的Forms.

假设你想拖动Labels之间FLPs一样的Form,下面的代码应该可以帮助你..

需要两个FlowLayoutPanels called FLP1 and FLP2并首先用一些初始化它们Labels.

注意三点鼠标事件我添加到每个Label模拟一个Drag&Drop行动!

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;

}

这允许您拖放Lables两个人之间来回FLPs并且还在FLPs通过下降onto Labels.

请注意,如果您想允许删除,则需要一些额外的行between Labels并仍然在那里..

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

FlowLayoutPanels 中的 C# 拖放标签 的相关文章

随机推荐

  • Numpy 矩阵到 tkinter 画布

    如何将 Numpy 矩阵作为位图显示到 Tkinter 画布中 更准确地说 如何填写PhotoImage来自矩阵的内容 photo ImageTk PhotoImage self canvas create image 0 0 image
  • 改变了 (un)serialize() 的行为?

    编辑 问题是现在已记录的 php 错误 https bugs php net bug php id 71617 https bugs php net bug php id 71617感谢您找到那个 Danack 我刚刚将应用程序从 PHPH
  • Shapeless 中 TypeClass 特征的 emptyCoproduct 和 coproduct 方法的用途是什么

    我并不完全清楚这样做的目的是什么emptyCoProduct and coproduct的方法TypeClass无形中的特质 什么时候会使用TypeClass特质而不是ProductTypeClass 这两种方法的实施方式有哪些示例 假设我
  • 使用php从图像中获取第一个像素

    我正在尝试获取图像的第一个像素 最好是最左上角或最右上角的一个像素 我看到了这个问题 它有最接近我的问题的答案 获取图像颜色 https stackoverflow com questions 1746530 get image color
  • 在 AngularJs 中设置动态作用域变量 -scope.

    我有一根绳子 是从routeParam或指令属性或其他什么 我想基于此在范围上创建一个变量 所以 scope
  • 如何使用Caliper进行基准测试?

    我试图弄清楚如何使用 Caliper 在 Eclipse 中进行基准测试 但一无所获 我尝试按照此处找到的 26 分钟教程进行操作 https code google com p caliper https code google com
  • 表示区间或范围? [关闭]

    Closed 这个问题是基于意见的 help closed questions 目前不接受答案 一般来说 每当您表示任何类型的范围时 您都可以选择为范围的开始和结束选择哪些类型的值 例如 如果您想要一个包含整数 1 2 3 4 5 的范围
  • 如何读取Flask函数中隐藏的表单数据[重复]

    这个问题在这里已经有答案了 我正在为 Flask 应用程序做一个作业 该应用程序的函数根据 index html 页面上表单中隐藏字段的值执行不同的操作 我只有两条路线 index html 和 process 对index html执行操
  • 如何使用javascript将div从左向右移动

    I have div named movingImage每次单击按钮时我都想向右移动 50px 这是我的 JavaScript function moving Image document getElementById movingImag
  • 为什么 Xcode 会在钥匙串中自动安装(重复且过期的)证书? [复制]

    这个问题在这里已经有答案了 可能的重复 xCode 4 重新安装我删除的钥匙串证书 https stackoverflow com questions 5264481 xcode 4 reinstalls keychain certs th
  • MATLAB:检测并删除 2 列矩阵中的镜像对

    我有一个矩阵 1 2 3 6 7 1 2 1 并想删除镜像对 即输出将是 1 2 3 6 7 1 or 3 6 7 1 2 1 有没有一种简单的方法可以做到这一点 我可以想象一个复杂的 for 循环 例如 或不会删除原始对的版本 仅删除重复
  • swift - 仅在落地时跳跃

    我希望限制我的角色 猫 使其仅在地面 虚拟 SKNode 或树上 treeP SKNode 时跳跃 目前我没有任何限制touchesBegan因此 如果用户快速连续点击 猫就能够在空中飞翔 虽然这在其他游戏中可能很有用 但在这里不受欢迎 如
  • 如何处理 Volley StringRequest 中的空值

    我有一个返回 2 组不同值的响应 在每种情况下 都会有一个值always为空 另一个将为空always包含一个值 问题是 Volley 不允许我分配 null JSON 值或在 if 语句中检查 JSON 值 它只是指 catch 块 我正
  • Chrome getUserMedia 未在本地请求权限

    我正在尝试在 Chrome 中使用 navigator getUserMedia 但是 它在本地提供时不会请求权限 file whatever index html 但在 JSFiddle 上会请求权限 http jsfiddle net
  • flex 4 tabbar - 禁用选项卡

    有没有一种常见的方法可以在 Flex 4 中禁用 Spark TabBar 组件的选项卡 使用 mx tabnavigator 组件 您可以禁用与选项卡相对应的内容 然后选项卡也会被禁用 但是使用 Spark 选项卡栏组件执行此操作只会禁用
  • 如何正确处理聊天消息应用程序的“已读”-“未读”状态?

    我目前正在开发一个反应本机应用程序 该应用程序使用 socket io 提供聊天室功能 我目前愿意处理消息的未读 已读状态 但我不知道该逻辑应位于何处 在客户端代码中还是在服务器端 这是我的代码组件的基本实现
  • Flutter中如何获取当前路由路径?

    在实施的同时持久底部栏 https stackoverflow com questions 45511549 permanent view with navigation bar in flutter 以前的路线需要恢复 https git
  • 将最新的 R 安装路径从注册表添加到 PATH windows 7/8/10

    嗨 我是 Windows 批处理新手 我想向打电话的同事分发一个 runMe bat 文件Rscript myRfile R处理一些数据文件 但众所周知 我的同事在不同地方安装了 R 我不能指望他们知道如何将 Rscript 添加到 PAT
  • appcompat-v7:27.1.1 与 play-services:11.0.1 冲突

    我正在开发一个新应用程序 目前我正在尝试添加依赖项 implementation com google android gms play services 11 0 1 当我这样做时 我在实现 com android support app
  • FlowLayoutPanels 中的 C# 拖放标签

    我有一个小问题 我想要制作一个程序 可以在多个 FlowLayoutPanel 之间拖动生成的标签 但最近几天我尝试让拖放工作 我尝试了很多教程 示例等 但总是有些不同 而且我无法仅提取基本代码 它类似于这个程序 https social