如何防止ToolStrip停靠在另一个ToolStripContainer中?

发布于 2024-07-08 08:57:11 字数 1166 浏览 9 评论 0原文

我有一个 MDI 应用程序,允许我打开不同类型的子窗口。 我可以打开同一类型子窗口的多个(但不同)实例。 (例如:我可以打开 3 个子窗口类型 A 的实例和 2 个子窗口类型 B 的实例。所有 5 个窗口都是不同的实体,除非用户明确将相同的数据拖到多个窗口上,否则不会共享数据。)每个子窗口有一个带有一个或多个 ToolStrip 的 ToolStripContainer。 如何防止:

  1. 用户将 ToolStrip 从类型 A 的子窗口拖动到类型 B 子窗口中的 ToolStripContainer 中?
  2. 用户是否可以将 ToolStrip 从子窗口 A 的一个实例拖动到同一类型窗口的另一个实例中的 ToolStripContainer?

我试图阻止用户将 ToolStrip 从类型 A 的实例 1 拖动到类型 A 的实例 2,在实例 2 上选择一些内容,然后单击工具栏上的按钮,结果却在其他窗口中发生奇怪的情况。 同样,将 ToolStrip 从类型 A 的窗口拖动到类型 B 的窗口是没有意义的 - 这些操作不适用于该类型,但对于用户来说,一切都很好,因为我让他们这样做阻力。

是否像为 ControlAdded 事件添加我自己的处理程序一样简单,或者是否有更好的方法来做到这一点? 我在 .NET 3.0 中使用 WinForms。

编辑:重现步骤

  1. 创建一个新的 Windows 应用程序项目。
  2. 添加新的用户控件。 为控件提供一个 ToolStripContainer,其中包含一个带有单个按钮的 ToolStrip。
  3. 重复步骤 2,为您提供一个 UserControl2 类。
  4. 编译解决方案,以便 UserControl1 和 UserControl2 显示在您的工具箱中。
  5. 将 UserControl1 和 UserControl2 拖到窗体上。 设置边界,以便您知道边界在哪里。
  6. 运行应用程序。
  7. 现在可以将 ToolStrip 从 UserControl1 中的容器拖放到 UserControl2 中的容器中(在 UC1 中留下零个 ToolStrip,在 UC2 中留下两个 ToolStrip。)
  8. 现在假设您只能访问 UserControl1 中的代码。 如何防止用户将 ToolStrip 拖出 ToolStripContainer 的该实例?

I have an MDI application that allows me to open different types of child windows. I can open multiple (but different) instances of the same type of child window. (Example: I can open 3 instances of child window type A and 2 instances of child window type B. All 5 windows are distinct entities and do not share data until unless the user explicitly drags the same data onto multiple windows.) Each child window has a ToolStripContainer with one or more ToolStrips. How do I prevent:

  1. the user from dragging a ToolStrip from a child window of type A to a ToolStripContainer in a child window of type B?
  2. the user from dragging a ToolStrip from one instance of child window A to a ToolStripContainer in another instances of the same type of window?

I'm trying to prevent the user from dragging a ToolStrip from instance 1 of type A to instance 2 of type A, selecting some stuff on instance 2, and then clicking a button on the toolbar only to have something weird happen to some other window. Similarly it doesn't make sense to drag a ToolStrip from a window of type A to a window of type B -- the actions don't apply to that type, but to the user it looks like everything is fine because I let them do the drag.

Is it as simple as adding my own handler for the ControlAdded event or is there a better way to do this? I'm using WinForms in .NET 3.0.

edit: Steps to reproduce

  1. Create a new Windows Application project.
  2. Add a new user control. Give the control a ToolStripContainer that contains one ToolStrip with a single button.
  3. Repeat step 2, giving you a UserControl2 class.
  4. Compile the solution so UserControl1 and UserControl2 show up in your toolbox.
  5. Drag UserControl1 and UserControl2 onto the form. Set the borders so you know where the boundaries are.
  6. Run the app.
  7. It's now possible to drag the ToolStrip from the container in UserControl1 and drop it into the container in UserControl2 (leaving zero ToolStrips in UC1 and two ToolStrips in UC2.)
  8. Now imagine you only have access to the code in UserControl1. How do you prevent the user from dragging the ToolStrip out of that instance of the ToolStripContainer?

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

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

发布评论

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

评论(2

自我难过 2024-07-15 08:57:11

这感觉像是一个 hack,但它确实有效(有点)(抱歉,vb.net 不是 c#):

Public Class UserControl2

    Private Sub tsMainMenu_BeginDrag(ByVal sender As Object, ByVal e As System.EventArgs) Handles tsMainMenu.BeginDrag

        tsMainMenu.Tag = tsMainMenu.Parent

    End Sub

    Private Sub ToolStrip1_EndDrag(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tsMainMenu.EndDrag


        If Not tsMainMenu.Parent.Parent.Equals(CType(tsMainMenu.Tag, ToolStripPanel).Parent) Then

            CType(ToolStrip1.Tag, ToolStripPanel).Controls.Add(tsMainMenu)
        End If

    End Sub

End Class

简单地说; 当控件完成拖动时,如果其父 ToolStripContainer 与开始拖动时不同,则将工具条移回原来的位置。

我确信这可以被整合到一个控件中,这样您就不必为每个工具栏编写它。

编辑:
您可以将所有这些代码放入从 ToolStripContainer 继承的控件中,并让它为您完成所有工作,这意味着一个很好的封装解决方案。

This feels like a hack, but it works (kind of) (sorry, vb.net not c#):

Public Class UserControl2

    Private Sub tsMainMenu_BeginDrag(ByVal sender As Object, ByVal e As System.EventArgs) Handles tsMainMenu.BeginDrag

        tsMainMenu.Tag = tsMainMenu.Parent

    End Sub

    Private Sub ToolStrip1_EndDrag(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tsMainMenu.EndDrag


        If Not tsMainMenu.Parent.Parent.Equals(CType(tsMainMenu.Tag, ToolStripPanel).Parent) Then

            CType(ToolStrip1.Tag, ToolStripPanel).Controls.Add(tsMainMenu)
        End If

    End Sub

End Class

simply put; when the control is finished being dragged, if its parent ToolStripContainer is not the same as it was when it started dragging, move the toolstrip back to where it was.

im sure this could be rolled into a control so that you dont have to write it for every toolbar.

Edit:
You could put all this code into a control that inherits from ToolStripContainer, and have it do all the work for you, meaning a nice encapsulated solution.

故人的歌 2024-07-15 08:57:11

您是否尝试过使用 ToolStripContainer 的 DragEnter 和 DragLeave 事件来识别在容器上拖动的 ToolStrip 并接受或拒绝它?

Have you tried to use the DragEnter and DragLeave Events of the ToolStripContainer to identify a ToolStrip dragged over the container an accept or deny it?

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