【发布时间】:2011-03-13 12:23:21
【问题描述】:
我正在创建一个用户控件,当用户单击按钮时,将显示一个弹出窗口,其中包含信息。弹出窗口由 toolStripDropDown 驱动,所以当它出现时会做两件事
- 不移动窗体上的其他控件,而是显示在它们上方
- 它可以在用户控件本身的范围之外显示细节,而无需提前预留空间
这是一些代码
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