Zkouším tip využít ToolStripDropDown, ale při více formulářích mám problém udržet dropdown část aniž by focus nepřebral jiný formulář, nastavit Parent na rodičovský form controlu nepomohlo, kdyby někdo věděl, jak s tím, tak by to pro moji potřebu zcela uspokojivě splnilo požadavek pro začátek, je dost věcí, co tam lze ještě doplnit, nebude-li lepší tip, zkusím s tím dál pracovat, je to jen prvopokus. Díky za další tip, radu i projevy soustrasti.
Public Class DatePickerBox
Inherits Label
Private WithEvents ddb As New DropDownButton
Private WithEvents mc As New MonthCalendar
Private WithEvents popup As New ToolStripDropDown
Class DropDownButton
Inherits Control
Public Event DropDown(ByVal sender As Object)
Private buttonState As System.Windows.Forms.VisualStyles.ComboBoxState = _
System.Windows.Forms.VisualStyles.ComboBoxState.Normal
Public Sub New()
MyBase.New()
SetStyle(ControlStyles.OptimizedDoubleBuffer _
Or ControlStyles.UserPaint Or ControlStyles.Opaque, True)
End Sub
Protected Overrides Sub OnKeyDown(ByVal e As System.Windows.Forms.KeyEventArgs)
MyBase.OnKeyDown(e)
If e.KeyCode = Keys.F4 Then
RaiseEvent DropDown(Me)
End If
End Sub
Protected Overrides Sub OnMouseLeave(ByVal e As System.EventArgs)
MyBase.OnMouseLeave(e)
buttonState = System.Windows.Forms.VisualStyles.ComboBoxState.Normal
Invalidate()
End Sub
Protected Overrides Sub OnMouseDown(ByVal e As MouseEventArgs)
MyBase.OnMouseDown(e)
If Me.Enabled Then
If e.Button = MouseButtons.Left AndAlso Me.ClientRectangle.Contains(e.Location) Then
buttonState = System.Windows.Forms.VisualStyles.ComboBoxState.Pressed
Invalidate()
End If
End If
End Sub
Protected Overrides Sub OnMouseEnter(ByVal e As EventArgs)
MyBase.OnMouseEnter(e)
If Me.Enabled Then buttonState = System.Windows.Forms.VisualStyles.ComboBoxState.Hot
Invalidate()
End Sub
Protected Overrides Sub OnMouseUp(ByVal e As MouseEventArgs)
MyBase.OnMouseUp(e)
buttonState = System.Windows.Forms.VisualStyles.ComboBoxState.Normal
Invalidate()
End Sub
' Detect when the cursor leaves the button area while it is pressed
Protected Overrides Sub OnMouseMove(ByVal e As MouseEventArgs)
MyBase.OnMouseMove(e)
' Detect when the left mouse button is down and
' the cursor has left the pressed button area.
If (e.Button And MouseButtons.Left) = MouseButtons.Left And Not _
Me.ClientRectangle.Contains(e.Location) And _
buttonState = System.Windows.Forms.VisualStyles.ComboBoxState.Pressed Then
OnMouseLeave(e)
End If
End Sub
Protected Overrides Sub OnMouseClick(ByVal e As System.Windows.Forms.MouseEventArgs)
MyBase.OnMouseClick(e)
If Me.Enabled Then RaiseEvent DropDown(Me)
End Sub
Protected Overrides Sub OnPaint(ByVal e As System.Windows.Forms.PaintEventArgs)
e.Graphics.DrawString(Me.Text, Me.Font, SystemBrushes.WindowText, 0, 0)
MyBase.OnPaint(e)
If Not ComboBoxRenderer.IsSupported Then
ControlPaint.DrawComboButton(e.Graphics, Me.ClientRectangle, Windows.Forms.ButtonState.Normal)
Else
ComboBoxRenderer.DrawDropDownButton(e.Graphics, Me.ClientRectangle, buttonState)
End If
End Sub
End Class
Public Sub New()
MyBase.New()
Dim w As Integer = SystemInformation.VerticalScrollBarWidth
ddb.Size = New Size(w, Me.Height)
ddb.Dock = DockStyle.Right
Me.Controls.Add(ddb)
mc.MaxSelectionCount = 1
Me.TextAlign = ContentAlignment.MiddleLeft
Me.BorderStyle = Windows.Forms.BorderStyle.Fixed3D
Me.BackColor = SystemColors.Window
Me.Text = mc.SelectionStart.ToShortDateString
popup.Margin = Padding.Empty
popup.Padding = Padding.Empty
Dim host As ToolStripControlHost = New ToolStripControlHost(mc)
host.Margin = Padding.Empty
host.Padding = Padding.Empty
popup.Items.Add(host)
popup.BackColor = Color.AliceBlue
'popup.Parent = Me.FindForm '?
End Sub
Private Sub OnShowDropDown()
If popup.Visible Then
popup.Hide()
Else
Dim o As Object = Me.Parent
Dim p As Point = New Point(Me.Left, Me.Bottom)
While Not Me.FindForm Is o
p.X = p.X + o.Left
p.Y = p.Y + o.Top
o = o.Parent
End While
popup.Show(Me.FindForm.PointToScreen(p))
End If
End Sub
Private Sub mc_DateChanged(ByVal sender As Object, ByVal e As System.Windows.Forms.DateRangeEventArgs) Handles mc.DateChanged
Me.Text = mc.SelectionStart.ToShortDateString
Me.Invalidate()
End Sub
Private Sub mc_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles mc.KeyDown
If Not (e.Alt Or e.Control Or e.Shift) And e.KeyCode = Keys.Enter Then
popup.Hide()
e.Handled = True
ElseIf e.Control And e.KeyCode = Keys.Home Then
mc.SelectionStart = Now.Date
e.Handled = True
End If
End Sub
Private Sub mc_LostFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles mc.LostFocus
Me.OnShowDropDown()
End Sub
Protected Overrides Sub Finalize()
popup.Dispose()
mc.Dispose()
MyBase.Finalize()
End Sub
Private Sub popup_Closed(ByVal sender As Object, ByVal e As System.Windows.Forms.ToolStripDropDownClosedEventArgs) Handles popup.Closed
ddb.Enabled = True
End Sub
Private Sub popup_Opened(ByVal sender As Object, ByVal e As System.EventArgs) Handles popup.Opened
ddb.Enabled = False
mc.Focus()
End Sub
Private Sub ddb_DropDown(ByVal sender As Object) Handles ddb.DropDown
Me.OnShowDropDown()
End Sub
Private Sub ddb_GotFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles ddb.GotFocus
SendKeys.Send("{F4}")
End Sub
End Class
|