【问题标题】:Dynamically generated controls issue动态生成的控件问题
【发布时间】:2010-06-14 12:35:45
【问题描述】:

我有一个带有停靠在其中的面板的表单。然后我在主面板(命名为 ContainerPanel)上动态创建 15 个面板(命名为:panel_n)和 15 个图片框(命名为:picturebox_n)。

在使用相关鼠标事件创建的面板 (panel_n) 上拖动任意图片框时。我想获取图片框被拖过的面板的名称。鼠标光标似乎被捕获了。

我已经尝试创建一个 IMessageFilter 界面,但是当将其中一个图片框拖到任何一个面板上时仍然没有触发事件。

ClientRectangle.IntersectsWith 函数也不起作用,因为坐标始终为 0,0。

我只需要拖动图片框的面板名称(最好在 mouseup 事件上)

【问题讨论】:

    标签: c# panel picturebox


    【解决方案1】:

    如果你给图片框一个 OnMouseDown 事件,它会这样说:

    (sender as PictureBox).DoDragDrop(sender, DragDropEffects.Copy);
    

    然后您可以将面板的 AllowDrop 属性设置为 true,并且在其 OnDragDrop 事件中,您可以像这样获取它们的名称:

    string myName = (sender as Panel).Name;
    

    编辑:另外,您需要像这样为面板提供 OnDragEnter 事件:

    e.Effect = DragDropEffects.Copy;
    

    当然,您可以将Copy 更改为MoveLink 或任何适合您正在做的事情。它只是改变了使用的鼠标指针图标。

    【讨论】:

    • 谢谢。我似乎无法让您的建议发挥作用我已将以下内容添加到我在 mousedown 事件中创建的 Pic 控件中。 pb.DoDragDrop(sender, DragDropEffects.Copy);我还在我的面板中添加了以下事件 ctl.DragDrop += new DragEventHandler(ctl_DragDrop);这是事件函数 private void ctl_DragDrop(object sender, DragEventArgs e) { string myName = (sender as Panel).Name; MessageBox.Show("拖到面板:" + myName); } // private void ctl_DragDrop(object sender, DragEventArgs e) 当我拖动任何 pciturebox 时,它会显示“无条目”鼠标光标。
    • 我还添加了 PanelBeat[i].AllowDrop = true;在我动态生成的面板上
    • @Thomas 抱歉,遗漏了一些代码。编辑了我的答案。顺便说一句,下次您应该编辑您的问题以添加类似上述评论的内容 - 格式化后更容易阅读代码:)
    • 谢谢。将更新我的代码并让您知道。 (将按照指示更改我对 cme​​ts 的关注)
    猜你喜欢
    • 2011-03-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-09-04
    相关资源
    最近更新 更多