DateTimePicker_Change

I’ve been pulling my hair out for like 3 hours.

 

I have to detect when the date was changed in a PowerShell form.

I’ve tried tons of thing, but nothing works.

 

Someone posted on here

 

$datetimepicker1_ValueChanged = {

$textbox1.Text = $dateTimePicker1.Value.ToString("yyyy/MM/dd")
$datetimepicker2.MinDate = $datetimepicker1.Value.AddDays(1)

}

 

$datetimepicker2_ValueChanged = {

$textbox2.Text = $datetimepicker2.Value.ToString("yyyy/MM/dd")

}

This doesn’t seem to work either.

 

Thanks

There are many ways to build forms in Powershell and depending on the example you started with the answer is going to vary. GUI interfaces have objects, such as a datetime picker, button, label, etc. and each control object has events. The datetime picker object needs to be named $datetimepicker2 and then you are triggering on event _ValueChanged. Where that function is defined is important as well, so you need to post the form with the PRE tags so that we can assist you.

I use the following to check the state change of a CheckBox, maybe this will work for you?

$ChkBox.Add_CheckStateChanged({your code goes here})

Something like ??

$datetimepicker2.Add_CheckStateChanged({your code goes here})

Thanks for replying,

This is what I get when I try that

Method invocation failed because [System.Windows.Forms.DateTimePicker] does
not contain a method named 'Add_CheckStateChanged'.

I’m using a Winforms I think.

Here is my code. CodePile | Easily Share Piles of Code

I’m also trying to get the datetimepicker to display nothing on startup, but that doesn’t work either.

Thanks

This basic example works:

Add-Type -AssemblyName System.Windows.Forms
Add-Type -AssemblyName System.Drawing

$form = New-Object Windows.Forms.Form -Property @{
    StartPosition = [Windows.Forms.FormStartPosition]::CenterScreen
    Size          = New-Object Drawing.Size 500, 500
    Text          = 'Select a Date'
    Topmost       = $true
}

$DatePicker = New-Object System.Windows.Forms.DateTimePicker  
 #$DatePicker.Format = [windows.forms.datetimepickerFormat]::custom
 #$DatePicker.CustomFormat = “MM-dd-yyyy”
$form.Controls.Add($DatePicker)  


$OKButton = New-Object Windows.Forms.Button -Property @{
    Location     = New-Object Drawing.Point 200, 400
    Size         = New-Object Drawing.Size 100, 23
    Text         = 'OK'
    DialogResult = [Windows.Forms.DialogResult]::OK
}
$form.AcceptButton = $OKButton
$form.Controls.Add($OKButton)

$CancelButton = New-Object Windows.Forms.Button -Property @{
    Location     = New-Object Drawing.Point 300, 400
    Size         = New-Object Drawing.Size 100, 23
    Text         = 'Cancel'
    DialogResult = [Windows.Forms.DialogResult]::Cancel
}
$form.CancelButton = $CancelButton
$form.Controls.Add($CancelButton)

$DatePicker.Add_ValueChanged({
    $OKButton.Text = 'Test'
    $OkButton.BackColor = 'Red'
})

$result = $form.ShowDialog()

if ($result -eq [Windows.Forms.DialogResult]::OK) {
    $date = $calendar.SelectionStart
    Write-Host "Date selected: $($date.ToShortDateString())"
}