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.

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
Textfield with appropriate event handlers in the Handlers region, as needed.
Examples
Section titled “Examples”- Instantiate a new
System.Windows.Forms.DateTimePickerandSystem.Windows.Forms.Labelcontrol pair:Controls Region #region Controls$FormMain = New-Object -TypeName System.Windows.Forms.Form$GroupBoxMain = New-Object -TypeName System.Windows.Forms.GroupBox<#Instantiate control objects here...#>$LabelDate = New-Object -TypeName System.Windows.Forms.Label$DateTimePickerDate = New-Object -TypeName System.Windows.Forms.DateTimePicker$ButtonRun = New-Object -TypeName System.Windows.Forms.Button$ButtonClose = New-Object -TypeName System.Windows.Forms.Button$StatusStripMain = New-Object -TypeName System.Windows.Forms.StatusStrip$ToolStripStatusLabelMain = New-Object -TypeName System.Windows.Forms.ToolStripStatusLabel#endregion - Assign properties to the pair and add them to the GroupBox object:
Forms Region | $ShowMainForm #region Forms$ShowFormMain ={$FormWidth = 330$FormHeight = 260$FormMain.Icon = [System.Drawing.Icon]::ExtractAssociatedIcon((Get-Process -Id $PID).Path)$FormMain.Text = "Title"$FormMain.Font = New-Object -TypeName System.Drawing.Font("MS Sans Serif",8)$FormMain.ClientSize = New-Object -TypeName System.Drawing.Size($FormWidth,$FormHeight)$FormMain.StartPosition = [System.Windows.Forms.FormStartPosition]::CenterScreen$FormMain.FormBorderStyle = [System.Windows.Forms.FormBorderStyle]::FixedSingle$FormMain.MaximizeBox = $false$FormMain.AcceptButton = $ButtonRun$FormMain.CancelButton = $ButtonClose$FormMain.Add_Shown($FormMain_Shown)$GroupBoxMain.Location = New-Object -TypeName System.Drawing.Point(10,5)$GroupBoxMain.Size = New-Object -TypeName System.Drawing.Size(($FormWidth - 20),($FormHeight - 80))$FormMain.Controls.Add($GroupBoxMain)<#Assign control properties and event handlers here and add to the GroupBox...#>$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)$ButtonRun.Location = New-Object -TypeName System.Drawing.Point(($FormWidth - 175),($FormHeight - 60))$ButtonRun.Size = New-Object -TypeName System.Drawing.Size(75,25)$ButtonRun.TabIndex = 100$ButtonRun.Enabled = $false$ButtonRun.Text = "Run"$ButtonRun.Add_Click($ButtonRun_Click)$FormMain.Controls.Add($ButtonRun)$ButtonClose.Location = New-Object -TypeName System.Drawing.Point(($FormWidth - 85),($FormHeight - 60))$ButtonClose.Size = New-Object -TypeName System.Drawing.Size(75,25)$ButtonClose.TabIndex = 101$ButtonClose.Text = "Close"$ButtonClose.DialogResult = [System.Windows.Forms.DialogResult]::Cancel$FormMain.Controls.Add($ButtonClose)$StatusStripMain.SizingGrip = $false$StatusStripMain.Font = New-Object -TypeName System.Drawing.Font("MS Sans Serif",8)[void]$StatusStripMain.Items.Add($ToolStripStatusLabelMain)$FormMain.Controls.Add($StatusStripMain)[void]$FormMain.ShowDialog()$FormMain.Dispose()}#endregion - Use the
Textproperty of DateTimePicker object to retrive the current value:Handlers Region | $ButtonRun_Click $ButtonRun_Click ={Invoke-FormAction -Action {try{ScheduleRestart($TextBoxServerName.Text, $DateTimePickerDate.Text)}catch{[void][System.Windows.Forms.MessageBox]::Show($PSItem.Exception.Message + "`n`nPlease contact $SUPPORT_CONTACT for technical support.","Exception",[System.Windows.Forms.MessageBoxButtons]::OK,[System.Windows.Forms.MessageBoxIcon]::Warning)}} -Reset {$TextBoxServerName.Clear()$TextBoxServerName.Focus()}}
References
Section titled “References”DateTimePicker Class (System.Windows.Forms) | Microsoft Learn