I am creating a user control where when a user clicks a button a popup window will show up with information. The popup window is driven by a toolStripDropDown so when it shows up it does 2 things
- Does not move the other controls on the form around but displays over them
- That it can show the details outside the bounds of the user control itself without having to reserve the space ahead of time
Here is some code
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
And here is the init of the control
'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.Te开发者_开发知识库xt = "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
Now my issue is as the form moves or resizes the Tooldropdown does not move relative. I understand that. When I try to capture the move event of the user control that event does not fire when the entire form moves. There has to be something I can capture because the controls in the container of the form move relative, what drives that? I tried wndproc but nothing fires during form move unless the form is repainted.
Thank you
Current code is in VB but I can handle both.
In C# :
Move and Resize events:
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
Just add in your .Designer.cs
this.Move +=new System.EventHandler(Form_Changed);
this.Resize += new System.EventHandler(Form_Changed);
I don't know if its possible in VB.
You can suscribe Parent Events with AddHandler
But you do it on Parent_changed events. Note that Parent is nothing until control is added to other control or Form.
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
I think this did it
AddHandler Me.ParentForm.Move, AddressOf Popup_Move
In the load event. I had to do it in the load event because ParentForm is not available earlier. I also tried the ParentChange event but that did not work if I was in a Panel
Me.Parent would equal Panel but
Me.Parent.Parent would equal nothing (also .ParentForm was Nothing)
From what I understand, you have a toolstrip that needs to move in conjunction with the form whenever it is moved by the user or some event that you control. If that be the case, I would do something like the following:
'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
精彩评论