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

标签 c# vb.net winforms user-controls

我正在创建一个用户控件,当用户单击按钮时,将显示一个弹出窗口,其中包含信息。弹出窗口由 toolStripDropDown 驱动,因此当它显示时,它会执行 2 件事

  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

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

谢谢

当前代码是用 VB 编写的,但我可以处理两者。

最佳答案

在 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中是否可行。

关于c# - 从子控件中检测表单移动事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3206783/

相关文章:

javascript - asp.net按钮点击不会通过jquery在隐藏字段中触发大量数据

c# - IShellLinkW::GetPath( SLGP_RAWPATH ) 不返回 lnk 文件的 'raw' 目标

c# - 来自枚举的单选按钮组

c# - VS Designer中的自定义控件: Avoid serialization of Collection properties

vb.net - 我需要更好地理解我的 if 语句

vb.net - 从字符串 "C:\Mediamemebuilderpro\MDAL1Imag"到类型 'Double' 的转换无效。”

c# - 如何在 C# winform 应用程序中写入非缓存文件

c# - 使用现有控件向窗体添加选项卡控件

c# - .NET 最小化到托盘并最小化所需资源

c# - 用作参数时无法推断 Action 委托(delegate)的返回类型