Procedimiento para mostrar la hora con el control DateTimePickerHow to: Display Time with the DateTimePicker Control

Si quiere la aplicación para permitir a los usuarios seleccionar una fecha y hora, y mostrar esa fecha y hora en el formato especificado, utilice el control DateTimePicker.If you want your application to enable users to select a date and time, and to display that date and time in the specified format, use the DateTimePicker control. El siguiente procedimiento muestra cómo utilizar el control DateTimePicker para mostrar la hora.The following procedure shows how to use the DateTimePicker control to display the time.

Para mostrar la hora con el control DateTimePickerTo display the time with the DateTimePicker control

  1. Establezca la propiedad Format en Time.Set the Format property to Time

    timePicker.Format = DateTimePickerFormat.Time;
    
    timePicker.Format = DateTimePickerFormat.Time
    
  2. Establezca la propiedad ShowUpDown para el elemento DateTimePicker en true.Set the ShowUpDown property for the DateTimePicker to true.

    timePicker.ShowUpDown = true;
    
    timePicker.ShowUpDown = True
    

EjemploExample

El ejemplo de código siguiente muestra cómo crear un DateTimePicker que permita a los usuarios elegir solo una vez.The following code sample shows how to create a DateTimePicker that enables users to choose a time only.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace TimePickerApplication
{
    public class Form1 : Form
    {
        public Form1()
        {
            InitializeTimePicker();
        }
        private DateTimePicker timePicker;

        private void InitializeTimePicker()
        {
            timePicker = new DateTimePicker();
            timePicker.Format = DateTimePickerFormat.Time;
            timePicker.ShowUpDown = true;
            timePicker.Location = new Point(10, 10);
            timePicker.Width = 100;
            Controls.Add(timePicker);
        }
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.Run(new Form1());
        }
        
    }
}
Imports System.Collections.Generic
Imports System.ComponentModel
Imports System.Drawing
Imports System.Text
Imports System.Windows.Forms

Public Class Form1
    Inherits Form

    Public Sub New()
        InitializeTimePicker()

    End Sub
    Private timePicker As DateTimePicker


    Private Sub InitializeTimePicker()
        timePicker = New DateTimePicker()
        timePicker.Format = DateTimePickerFormat.Time
        timePicker.ShowUpDown = True
        timePicker.Location = New Point(10, 10)
        timePicker.Width = 100
        Controls.Add(timePicker)

    End Sub

    <STAThread()> _
    Shared Sub Main()
        Application.EnableVisualStyles()
        Application.Run(New Form1())

    End Sub
End Class

Compilar el códigoCompiling the Code

Para este ejemplo se necesita:This example requires:

  • Referencias a los ensamblados System, System.Data, System.Drawing y System.Windows.Forms.References to the System, System.Data, System.Drawing and System.Windows.Forms assemblies.

Vea tambiénSee also