Como veran en la imagen, pude lograrlo con este codigo pero con ese codigo solo me figura el control al momento de correr la aplicacion, yo lo quiero ver desde la ventana de diseño para poder cambiar su ubicacion y no salir en un formulario en una ubicacion estandar
En el código que has mostrado, lo que estás haciendo es añadir un control al Form en tiempo de ejecución, evidentemente no lo puedes manipular en tiempo de diseño.
Para hacer lo que quieres, necesitas crear un control de usuario desde cero (heredando la clase
UserControl), o bien extender un control por defecto. Esto último se hace declarando una clase que herede del control que quieres modificar/extender, en este caso
DateTimePicker. Tu código adaptado quedaría más o menos de la siguiente manera:
Imports System
Imports System.ComponentModel
Imports System.Diagnostics
Imports System.Runtime.InteropServices
Imports System.Windows.Forms
<ClassInterface(ClassInterfaceType.AutoDispatch)>
<ComVisible(True)>
<DefaultBindingProperty("Value")>
<DefaultEvent("ValueChanged")>
<DefaultProperty("Value")>
Public Class TimePicker : Inherits DateTimePicker
<Bindable(True)>
<Browsable(True)>
<EditorBrowsable(EditorBrowsableState.Always)>
<RefreshProperties(RefreshProperties.All)>
Public Overloads Property Value As TimeSpan
Get
Return MyBase.Value.TimeOfDay
End Get
Set(value As TimeSpan)
MyBase.Value = DateTime.Today + value
End Set
End Property
Public Sub New()
Me.Format = DateTimePickerFormat.Time
Me.ShowUpDown = True
Me.Value = TimeSpan.Zero ' Me.Value = MyBase.Value.TimeOfDay
' Me.Width = 100
End Sub
End Class
Cuando hayas compilado esa clase por primera vez (y solo despues de haberla compilado) te aparecerá un control con nombre "TimePicker" arriba del todo junto a los demás controles que puedes arrastrar al Form.
Si quieres un control más personalizado, esto de aquí abajo lo acabo de hacer hoy (no lo he testeado en profundidad)...
Es un código muy simple, este código es practicamente lo mismo que el anterior que mostré, solo que denota una mejor representación del tiempo ya que no se puede representar una fecha en este control, tiene las propiedades de calendario eliminadas, y el formato personalizado de representación de fecha y hora (propiedad
CustomFormat) está "bloqueado" a formato de 24 horas.
ElektroTimePicker.vb' ***********************************************************************
' Author : Elektro
' Modified : 06-December-2017
' ***********************************************************************
#Region " Public Members Summary "
#Region " Constructors "
' New()
#End Region
#Region " Properties "
' Value As TimeSpan
#End Region
#End Region
#Region " Option Statements "
Option Strict On
Option Explicit On
Option Infer Off
#End Region
#Region " Imports "
Imports System
Imports System.ComponentModel
Imports System.Diagnostics
Imports System.Drawing
Imports System.Runtime.InteropServices
Imports System.Windows.Forms
Imports System.Windows.Forms.Design
' Imports ElektroKit.UserControls.Designers
#End Region
#Region " ElektroTimePicker "
' Namespace ElektroKit.UserControls
''' ----------------------------------------------------------------------------------------------------
''' <summary>
''' Represents a control that allows the user to select an hour,
''' and to display the time with a specified format.
''' </summary>
''' ----------------------------------------------------------------------------------------------------
<DisplayName("ElektroTimePicker")>
<Description("A extended DateTimePicker control that only displays the time.")>
<DesignTimeVisible(True)>
<DesignerCategory("UserControl")>
<ToolboxBitmap(GetType(DateTimePicker), "DateTimePicker.bmp")>
<ToolboxItemFilter("System.Windows.Forms", ToolboxItemFilterType.Require)>
<ClassInterface(ClassInterfaceType.AutoDispatch)>
<ComVisible(True)>
<DefaultBindingProperty("Value")>
<DefaultEvent("ValueChanged")>
<DefaultProperty("Value")>
<Designer(GetType(ElektroTimePickerDesigner))>
Public Class ElektroTimePicker : Inherits DateTimePicker
#Region " Properties "
''' ----------------------------------------------------------------------------------------------------
''' <summary>
''' Gets or sets the time value assigned to the control.
''' </summary>
''' ----------------------------------------------------------------------------------------------------
''' <value>
''' The time value assigned to the control.
''' </value>
''' ----------------------------------------------------------------------------------------------------
<Bindable(True)>
<Browsable(True)>
<EditorBrowsable(EditorBrowsableState.Always)>
<RefreshProperties(RefreshProperties.All)>
Public Overloads Property Value As TimeSpan
Get
Return MyBase.Value.TimeOfDay
End Get
Set(value As TimeSpan)
MyBase.Value = DateTime.Today + value
End Set
End Property
#End Region
#Region " Constructors "
''' ----------------------------------------------------------------------------------------------------
''' <summary>
''' Initializes a new instance of the <see cref="ElektroTimePicker"/> class.
''' </summary>
''' ----------------------------------------------------------------------------------------------------
Public Sub New()
Me.CustomFormat = "HH:mm:ss" ' 24-hour format.
Me.Format = DateTimePickerFormat.Custom
Me.ShowUpDown = True
Me.Value = TimeSpan.Zero
' Me.Value = MyBase.Value.TimeOfDay
Me.Width = Me.MinimumSize.Width
Me.AdjustControlWidth()
End Sub
#End Region
#Region " Event Invocators "
''' ----------------------------------------------------------------------------------------------------
''' <summary>
''' Raises the <see cref="ElektroTimePicker.FontChanged"/> event.
''' </summary>
''' ----------------------------------------------------------------------------------------------------
''' <param name="e">
''' An <see cref="EventArgs"/> that contains the event data.
''' </param>
''' ----------------------------------------------------------------------------------------------------
Protected Overrides Sub OnFontChanged(ByVal e As EventArgs)
If (Me.DesignMode) Then
Me.AdjustControlWidth()
End If
MyBase.OnFontChanged(e)
End Sub
#End Region
#Region " Overridable Methods "
''' ----------------------------------------------------------------------------------------------------
''' <summary>
''' Grows the control width to fit the text displayed.
''' </summary>
''' ----------------------------------------------------------------------------------------------------
Protected Overridable Sub AdjustControlWidth()
Using g As Graphics = Me.CreateGraphics()
Dim textSize As SizeF = g.MeasureString(Me.Text, Me.Font)
If (textSize.Width > Me.Width) Then
Me.Width = CInt(Math.Ceiling(Size.Width)) * 2
End If
End Using
End Sub
#End Region
End Class
' End Namespace
#End Region
+
ElektroTimePickerDesigner.vb' ***********************************************************************
' Author : Elektro
' Modified : 06-December-2017
' ***********************************************************************
#Region " Option Statements "
Option Strict On
Option Explicit On
Option Infer Off
#End Region
#Region " Imports "
Imports System
Imports System.Collections
Imports System.Diagnostics
Imports System.Windows.Forms.Design
#End Region
#Region " ElektroTimePickerDesigner "
' Namespace ElektroKit.UserControls.Designers
''' ----------------------------------------------------------------------------------------------------
''' <summary>
''' Extends the design mode behavior of a <see cref="ElektroTimePicker"/> control.
''' </summary>
''' ----------------------------------------------------------------------------------------------------
''' <seealso cref="ElektroTimePicker"/>
''' ----------------------------------------------------------------------------------------------------
<DebuggerStepThrough>
Public NotInheritable Class ElektroTimePickerDesigner : Inherits ControlDesigner
''' ----------------------------------------------------------------------------------------------------
''' <summary>
''' Provides design-time support for the <see cref="ElektroTimePicker"/> control.
''' </summary>
''' ----------------------------------------------------------------------------------------------------
''' <param name="properties">
''' Ab <see cref="IDictionary"/> that contains the properties for the class of the component.
''' </param>
''' ----------------------------------------------------------------------------------------------------
Protected Overrides Sub PreFilterProperties(ByVal properties As IDictionary)
MyBase.PreFilterAttributes(properties)
Dim propertyNames As String() = {
"CalendarFont",
"CalendarForeColor",
"CalendarMonthBackground",
"CalendarTitleBackColor",
"CalendarTitleForeColor",
"CalendarTrailingForeColor",
"CustomFormat",
"DropDownAlign",
"Format",
"MaxDate",
"MinDate",
"ShowUpDown"
}
For Each propertyName As String In propertyNames
properties.Remove(propertyName)
Next
End Sub
End Class
' End Namespace
#End Region
Saludos