【问题标题】:Detecting the form move event from within a child control从子控件中检测表单移动事件
【发布时间】:2011-03-13 12:23:21
【问题描述】:

我正在创建一个用户控件,当用户单击按钮时,将显示一个弹出窗口,其中包含信息。弹出窗口由 toolStripDropDown 驱动,所以当它出现时会做两件事

  1. 不移动窗体上的其他控件,而是显示在它们上方
  2. 它可以在用户控件本身的范围之外显示细节,而无需提前预留空间

这是一些代码

Public Class Popup
Private treeViewHost As ToolStripControlHost
Private Shadows dropDown As ToolStripDropDown

Public Sub New()
    InitializeComponent()
    Dim treeView As New TreeView()

    treeView.BorderStyle = BorderStyle.None
    treeViewHost = New ToolStripControlHost(treeView)
    treeViewHost.Padding = New Padding(6)

    dropDown = New ToolStripDropDown()
    dropDown.AutoClose = False
    dropDown.AutoSize = True
    dropDown.BackColor = Color.LemonChiffon
    dropDown.Items.Add(treeViewHost)
End Sub

Public Sub ShowDropDown()      
    If dropDown.Visible = False Then
        dropDown.Show(Me, Button1.Left + Button1.Height + 5, Button1.Top)
    Else
        dropDown.Close()
    End If

End Sub    

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    ShowDropDown()
End Sub

Private Sub Popup_Move(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Move, Button1.Move
    If (dropDown IsNot Nothing AndAlso Button1 IsNot Nothing AndAlso dropDown.Visible) Then
        dropDown.Left = Button1.Left + Button1.Height + 5
        dropDown.Top = Button1.Top
    End If
End Sub
End Class

这里是控件的初始化

'Required by the Windows Form Designer
Private components As System.ComponentModel.IContainer

'NOTE: The following procedure is required by the Windows Form Designer
'It can be modified using the Windows Form Designer.  
'Do not modify it using the code editor.
<System.Diagnostics.DebuggerStepThrough()> _
Private Sub InitializeComponent()
    Me.Button1 = New System.Windows.Forms.Button
    Me.SuspendLayout()
    '
    'Button1
    '
    Me.Button1.Location = New System.Drawing.Point(4, 4)
    Me.Button1.Name = "Button1"
    Me.Button1.Size = New System.Drawing.Size(27, 23)
    Me.Button1.TabIndex = 0
    Me.Button1.Text = "Button1"
    Me.Button1.UseVisualStyleBackColor = True
    '
    'Popup
    '
    Me.AutoScaleDimensions = New System.Drawing.SizeF(6.0!, 13.0!)
    Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font
    Me.Controls.Add(Me.Button1)
    Me.Name = "Popup"
    Me.Size = New System.Drawing.Size(39, 35)
    Me.ResumeLayout(False)

End Sub
Friend WithEvents Button1 As System.Windows.Forms.Button

现在我的问题是表单移动或调整大小时 Tooldropdown 不会相对移动。我明白那个。当我尝试捕获用户控件的移动事件时,该事件不会在整个表单移动时触发。必须有一些我可以捕获的东西,因为表单容器中的控件相对移动,是什么驱动?我尝试了 wndproc,但在表单移动期间没有任何触发,除非重新绘制表单。

谢谢

当前代码在 VB 中,但我可以同时处理。

【问题讨论】:

    标签: c# vb.net winforms user-controls


    【解决方案1】:

    在 C# 中:

    移动和调整事件大小:

    http://msdn.microsoft.com/en-us/library/system.windows.forms.control.move.aspx http://msdn.microsoft.com/en-us/library/system.windows.forms.control.move.aspx

    只需添加您的 .Designer.cs

    this.Move +=new System.EventHandler(Form_Changed);
    this.Resize += new System.EventHandler(Form_Changed);
    

    我不知道在VB中是否可行。

    【讨论】:

      【解决方案2】:

      我认为它做到了

       AddHandler Me.ParentForm.Move, AddressOf Popup_Move
      

      在加载事件中。我不得不在加载事件中这样做,因为 ParentForm 之前不可用。我也尝试了 ParentChange 事件,但如果我在面板中,那将不起作用

      Me.Parent would equal Panel but
      Me.Parent.Parent would equal nothing (also .ParentForm was Nothing)
      

      【讨论】:

        【解决方案3】:

        据我了解,您有一个工具条,每当用户或您控制的某些事件移动它时,它都需要与表单一起移动。如果是这样的话,我会做如下的事情:

        'Function within the form that is being moved and governs the location of the ToolStrip
        Private Sub Form1_LocationChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.LocationChanged
            'lastLocation is a global variable that is reset every time the form is moved
            Dim offsetX As Integer = lastLocationX.X - Location.X
            Dim offsetY As Integer = lastLocationX.Y - Location.Y
            ToolStrip1.show(lastXcoord - offsetX, lastYcoord - offsetY)
            lastLocation = Location
        End Sub
        

        【讨论】:

          【解决方案4】:

          您可以使用 AddHandler

          订阅父事件

          但是您在 Parent_changed 事件上执行此操作。请注意,在将控件添加到其他控件或表单之前,Parent 什么都不是。

          Private Sub GantCtl_ParentChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.ParentChanged
             Dim mParent = DirectCast(Parent, Form)
             AddHandler mParent.KeyDown, AddressOf Parent_KeyDown
             AddHandler mParent.MouseWheel, AddressOf Parent_MouseWheel
             AddHandler mParent.Resize, AddressOf parent_Resize
          End Sub
          

          【讨论】:

            猜你喜欢
            • 2023-03-10
            • 1970-01-01
            • 2020-12-09
            • 2020-12-18
            • 2015-04-11
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多