AllowDrop 属性不适用于 ToolStripItems
我已成功使用用户控件创建了 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是我为我工作的代码。
我在表单构造函数中分配 DragDrop 属性,因为它们隐藏在设计器中。
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.