AllowDrop 属性不适用于 ToolStripItems

发布于 2024-12-15 11:10:39 字数 1148 浏览 4 评论 0原文

我已成功使用用户控件创建了 DragDrop 功能。现在我尝试在某些组件(例如 ToolStripButton)上允许 DragDrop 功能。

基类 ToolStripItem 支持AllowDrop 和DragEnter/DragDrop 事件...

ToolStripButton 在设计器中隐藏这些属性,但它们是可公开访问的。

最初,我尝试对每个 ToolStripButton 执行以下操作:

button.AllowDrop = true;
button.DragEnter += new DragEventHandler(button_DragEnter);
button.DragDrop += new DragEventHandler(button_DragDrop);

但是,事件从未触发。这些按钮包含在 MenuStrip 中,因此我将 MenuStrip.AllowDrop 更改为 true。然后我开始获取 DragEnter 和 DragDrop 事件,但由于访问 ToolStripItem 的 Tag 属性时出现线程/调用问题,DragDrop 事件将失败。

无法调用 ToolStripItems。所以我尝试调用他们的容器,MenuStrip,具有相同的功能。我仍然遇到线程/调用问题,一旦我尝试访问 ToolStripItem,线程就会停止运行。

以下是我用来在调用后检索标签信息的代码:

void button_DragDrop(object sender, DragEventArgs e)
{
    menuStrip.Invoke(new DragEventHandler(MyDragFunction), new object[] { sender, e });
}

void MyDragFunction(object sender, DragEventArgs e)
{
    int id = (int)((ToolStripButton)sender).Tag;
    // Debugging never reaches this line
    int dragId = (int)e.Data.GetData(DataFormatName, false);

    MoveItem(id, dragId);
}

拖放到 ToolStripItem 之类的组件根本不可能吗?或者我做错了什么?

I have successfully created DragDrop functionality with user controls. Now I am trying to allow DragDrop functionality on some components such as ToolStripButton.

The base class, ToolStripItem, supports AllowDrop and the DragEnter/DragDrop events...

ToolStripButton hides these properties in the designer, but they are publicly accessable.

Originally I tried doing the following for each ToolStripButton:

button.AllowDrop = true;
button.DragEnter += new DragEventHandler(button_DragEnter);
button.DragDrop += new DragEventHandler(button_DragDrop);

However, the events were not ever firing. These buttons are contained within a MenuStrip, so I changed the MenuStrip.AllowDrop to true. Then I started getting DragEnter and DragDrop events, but the DragDrop event would fail due to a threading/invoke problem when accessing the Tag property of the ToolStripItem.

The ToolStripItems cannot be invoked upon. So I have tried invoking their container, the MenuStrip, with the same function. I am still getting a threading/invoke problem where the thread stops running as soon as I try to access the ToolStripItem.

Here is the code I'm using to retrieve the Tag information after invoke:

void button_DragDrop(object sender, DragEventArgs e)
{
    menuStrip.Invoke(new DragEventHandler(MyDragFunction), new object[] { sender, e });
}

void MyDragFunction(object sender, DragEventArgs e)
{
    int id = (int)((ToolStripButton)sender).Tag;
    // Debugging never reaches this line
    int dragId = (int)e.Data.GetData(DataFormatName, false);

    MoveItem(id, dragId);
}

Is Drag and Drop to a component like a ToolStripItem simply not possible? Or am I doing something wrong?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

瞎闹 2024-12-22 11:10:39

这是我为我工作的代码。

我在表单构造函数中分配 DragDrop 属性,因为它们隐藏在设计器中。

foreach (object o in menuStrip.Items)
{
    if (o is ToolStripButton)
    {
        ToolStripItem item = (ToolStripItem)o;

        item.AllowDrop = true;
        item.DragEnter += new DragEventHandler(item_DragEnter);
        item.DragOver += new DragEventHandler(item_DragOver);
        item.DragDrop += new DragEventHandler(item_DragDrop);
    }
}

private void item_DragEnter(object sender, DragEventArgs e)
{
    if (e.Data.GetDataPresent(DataFormatName, false))
    {
        e.Effect = DragDropEffects.Move;
    }
}

private void item_DragOver(object sender, DragEventArgs e)
{
    if (e.Data.GetDataPresent(DataFormatName, false))
    {
        e.Effect = DragDropEffects.Move;
    }
}

private void item_DragDrop(object sender, DragEventArgs e)
{
    int id = (int)e.Data.GetData(DataFormatName, false);
    int category = Convert.ToInt32((sender as ToolStripButton).Tag);

    MyFunction(category, id);
}

Here is the code I got to work for me.

I assign the DragDrop properties in the form constructor since they are hidden in the designer.

foreach (object o in menuStrip.Items)
{
    if (o is ToolStripButton)
    {
        ToolStripItem item = (ToolStripItem)o;

        item.AllowDrop = true;
        item.DragEnter += new DragEventHandler(item_DragEnter);
        item.DragOver += new DragEventHandler(item_DragOver);
        item.DragDrop += new DragEventHandler(item_DragDrop);
    }
}

private void item_DragEnter(object sender, DragEventArgs e)
{
    if (e.Data.GetDataPresent(DataFormatName, false))
    {
        e.Effect = DragDropEffects.Move;
    }
}

private void item_DragOver(object sender, DragEventArgs e)
{
    if (e.Data.GetDataPresent(DataFormatName, false))
    {
        e.Effect = DragDropEffects.Move;
    }
}

private void item_DragDrop(object sender, DragEventArgs e)
{
    int id = (int)e.Data.GetData(DataFormatName, false);
    int category = Convert.ToInt32((sender as ToolStripButton).Tag);

    MyFunction(category, id);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文