Skip to content

DateTimePicker

In a ScriptoForm, the Microsoft .NET DateTimePicker class is a control that allows the user to select a date and a time in a specified format. This selection can be used to control or provide a parameter to actions performed by a ScriptoForm. The date/time value can be manually typed in by the user or the date portion can be selected by clicking an attached button which displays a small calendar view.

ScriptoForm with example DateTimePick control in the default state ScriptoForm with example DateTimePick control in the calendar view state

In the ScriptoForm PowerShell script file, a DateTimePicker control should be instantiated in the Controls region and then defined within the main forms script block. A DateTimePicker is usually paired with a matching Label control. If using the ScriptoForm VS Code snippets, the ScriptoForm Controls: Label & DateTimePicker snippet can be used to instantiate this control pair and the ScriptoForm Properties: Label & DateTimePicker snippet can be used to assign a default set of properties to them. By convention, this control pair should be nested in the $GroupBoxMain GroupBox (as displayed in the above image) and is added to it rather than the main Form.

The steps needed to implement a DateTimePicker control with a ScriptoForm include:

  • Instantiate a DateTimePicker object and an associated Label object in the Controls region.
    Suggested name format: $LabelName | $DateTimePickerName
  • Assign the required and any optional properties to the DateTimePicker object in the Forms region.
  • Add the control pair to a GroupBox object in the Forms region.
  • Utilize the value of the DateTimePicker Text field with appropriate event handlers in the Handlers region, as needed.
  1. Instantiate a new System.Windows.Forms.DateTimePicker and System.Windows.Forms.Label control pair:
    Controls Region
    $LabelDate = New-Object -TypeName System.Windows.Forms.Label
    $DateTimePickerDate = New-Object -TypeName System.Windows.Forms.DateTimePicker
  2. Assign properties to the pair and add them to the GroupBox object:
    Forms Region | $ShowMainForm
    $LabelDate.Location = New-Object -TypeName System.Drawing.Point(15,15)
    $LabelDate.AutoSize = $true
    $LabelDate.Text = "Restart Date:"
    $GroupBoxMain.Controls.Add($LabelDate)
    $DateTimePickerDate.Location = New-Object -TypeName System.Drawing.Point(15,35)
    $DateTimePickerDate.Size = New-Object -TypeName System.Drawing.Size(($FormWidth - 50),20)
    $DateTimePickerDate.TabIndex = 2
    $DateTimePickerDate.Format = [System.Windows.Forms.DateTimePickerFormat]::Custom
    $DateTimePickerDate.CustomFormat = "MM/dd/yyyy"
    $DateTimePickerDate.MinDate = [System.DateTime]::Today
    $DateTimePickerDate.Text = ((Get-Date).AddDays(1)).ToString("MM/dd/yyyy")
    $GroupBoxMain.Controls.Add($DateTimePickerDate)
  3. Use the Text property of DateTimePicker object to retrive the current value:
    Handlers Region
    $RestartDate = $DateTimePickerDate.Text

DateTimePicker Class (System.Windows.Forms) | Microsoft Learn