从子控件中检测表单移动事件

时间:2010-07-08 18:31:51

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

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

  1. 不会移动表单上的其他控件但显示在
  2. 它可以在不必提前预留空间的情况下显示用户控件本身范围之外的细节
  3. 这是一些代码

    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不会相对移动。我明白那个。当我尝试捕获用户控件的move事件时,当整个表单移动时事件不会触发。必须有一些我可以捕获的东西,因为表单容器中的控件移动相对,驱动它的是什么?我尝试了wndproc,但是在表单移动期间什么都不会触发,除非重新绘制表单。

    谢谢

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

4 个答案:

答案 0 :(得分:2)

在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中使用。

答案 1 :(得分:2)

您可以使用 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

答案 2 :(得分:1)

我认为这样做了

 AddHandler Me.ParentForm.Move, AddressOf Popup_Move

在加载事件中。我必须在load事件中执行它,因为ParentForm之前不可用。我也尝试过ParentChange事件,但是如果我在Panel

中则不起作用
Me.Parent would equal Panel but
Me.Parent.Parent would equal nothing (also .ParentForm was Nothing)

答案 3 :(得分:1)

据我所知,你有一个工具条,只要用户移动它或你控制的某个事件,它就需要与表单一起移动。如果是这种情况,我会做类似以下的事情:

'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