Advertisement
Combreal

MSDatePicker.ps1

Dec 22nd, 2022
792
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. Add-Type -AssemblyName System.Windows.Forms
  2. Add-Type -AssemblyName System.Drawing
  3.  
  4. $form = New-Object Windows.Forms.Form -Property @{
  5.     StartPosition = [Windows.Forms.FormStartPosition]::CenterScreen
  6.     Size          = New-Object Drawing.Size 243, 230
  7.     Text          = 'Select a Date'
  8.     Topmost       = $true
  9. }
  10.  
  11. $calendar = New-Object Windows.Forms.MonthCalendar -Property @{
  12.     ShowTodayCircle   = $false
  13.     MaxSelectionCount = 1
  14. }
  15. $form.Controls.Add($calendar)
  16.  
  17. $okButton = New-Object Windows.Forms.Button -Property @{
  18.     Location     = New-Object Drawing.Point 38, 165
  19.     Size         = New-Object Drawing.Size 75, 23
  20.     Text         = 'OK'
  21.     DialogResult = [Windows.Forms.DialogResult]::OK
  22. }
  23. $form.AcceptButton = $okButton
  24. $form.Controls.Add($okButton)
  25.  
  26. $cancelButton = New-Object Windows.Forms.Button -Property @{
  27.     Location     = New-Object Drawing.Point 113, 165
  28.     Size         = New-Object Drawing.Size 75, 23
  29.     Text         = 'Cancel'
  30.     DialogResult = [Windows.Forms.DialogResult]::Cancel
  31. }
  32. $form.CancelButton = $cancelButton
  33. $form.Controls.Add($cancelButton)
  34.  
  35. $result = $form.ShowDialog()
  36.  
  37. if ($result -eq [Windows.Forms.DialogResult]::OK) {
  38.     $date = $calendar.SelectionStart
  39.     Write-Host "Date selected: $($date.ToShortDateString())"
  40. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement