Juan Manuel Sanchez
Juan Manuel Sanchez

Reputation: 79

Select date range calendar powershell

I want to be able to select a range of dates in the calendar (and highlight the selection)

So far, Im only getting 1 date in the SelectionRange instead of the whole selection array. I dont know why

Function Pick-Date {
  $Cal = New-Object System.Windows.Forms.MonthCalendar
  $Cal.ShowWeekNumbers = $true
  $Cal.MaxSelectionCount = 365
  $Cal.Dock = 'Fill'
  $Form = New-Object Windows.Forms.Form
  $Form.text = "Colocar fecha que desea enviar. Y presiona Enter"
  $Form.Size = New-Object Drawing.Size @(800,600)
  $btnSelect = New-Object System.Windows.Forms.Button
  $btnSelect.Size = "80,80"
  $Dates = $Cal.SelectionRange
  $btnSelect.add_Click( { busca_tiquete ($Dates)} )
  #$btnSelect.add_Click( { busca_tiquete (Get-Date($Cal.SelectionStart))} )
  $btnSelect.Location = New-Object System.Drawing.Point(230,480)
  $btnSelect.Text="ENTER"
  $CancelButton = New-Object System.Windows.Forms.Button
  $CancelButton.Location = New-Object System.Drawing.Point(330,480)
  $CancelButton.Size = "80,80"
  $CancelButton.Text = 'Cancel'
  $CancelButton.DialogResult = [System.Windows.Forms.DialogResult]::Cancel
  $Form.CancelButton = $CancelButton
  $Form.Controls.Add($CancelButton)
  $Form.Controls.Add($btnSelect )
  $Form.AcceptButton = $btnSelect
  $Form.Controls.Add($Cal)
  $Form.Add_Shown({$Form.Activate()})
  [void]$Form.ShowDialog()
  #return (Get-Date($Cal.SelectionStart))
}

Any help is appreciated.

THanks in advance !

Upvotes: 0

Views: 1033

Answers (1)

user6811411
user6811411

Reputation:

It's unclear what your busca_tiquete ($Dates) does, but it is placed wrongly inside the function. $cal.SelectionRange is populated after the command [void]$Form.ShowDialog()

So this might do:

Function Pick-Date {
    $Cal = New-Object System.Windows.Forms.MonthCalendar
    $Cal.ShowWeekNumbers = $true
    $Cal.MaxSelectionCount = 365
    $Cal.Dock = 'Fill'
    $Form = New-Object Windows.Forms.Form
    $Form.text = "Colocar fecha que desea enviar. Y presiona Enter"
    $Form.Size = New-Object Drawing.Size @(800,600)
    $btnSelect = New-Object System.Windows.Forms.Button
    $btnSelect.Size = "80,80"
    $btnSelect.Location = New-Object System.Drawing.Point(230,480)
    $btnSelect.Text="ENTER"
    $CancelButton = New-Object System.Windows.Forms.Button
    $CancelButton.Location = New-Object System.Drawing.Point(330,480)
    $CancelButton.Size = "80,80"
    $CancelButton.Text = 'Cancel'
    $CancelButton.DialogResult = [System.Windows.Forms.DialogResult]::Cancel
    $Form.CancelButton = $CancelButton
    $Form.Controls.Add($CancelButton)
    $Form.Controls.Add($btnSelect )
    $Form.AcceptButton = $btnSelect
    $Form.Controls.Add($Cal)
    $Form.Add_Shown({$Form.Activate()})
    [void]$Form.ShowDialog()
    ## $$Dates will have start and end property of [datetime] type.
    $Dates = $Cal.SelectionRange
    ## unclear what the next command does/returns
    busca_tiquete $Dates
}

Upvotes: 2

Related Questions