DateTimePicker_Change

Welcome Forums General PowerShell Q&A DateTimePicker_Change

Viewing 4 reply threads
  • Author
    Posts
    • #202115
      Participant
      Topics: 1
      Replies: 1
      Points: 19
      Rank: Member

      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

      • This topic was modified 4 months, 4 weeks ago by AcadianMan96.
      • This topic was modified 4 months, 4 weeks ago by AcadianMan96.
      • This topic was modified 4 months, 4 weeks ago by kvprasoon. Reason: code formatting
      • This topic was modified 4 months, 4 weeks ago by AcadianMan96. Reason: Forgot to mention that the code found does nothing for me
    • #202202
      Participant
      Topics: 12
      Replies: 1642
      Points: 2,660
      Helping Hand
      Rank: Community Hero

      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.

    • #202229
      Participant
      Topics: 6
      Replies: 104
      Points: 486
      Helping Hand
      Rank: Contributor

      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})

    • #202406
      Participant
      Topics: 1
      Replies: 1
      Points: 19
      Rank: Member

      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.  https://www.codepile.net/pile/qgQDZvNZ

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

      Thanks

      • This reply was modified 4 months, 3 weeks ago by grokkit.
      • This reply was modified 4 months, 3 weeks ago by grokkit. Reason: merged double post and fixed formatting
    • #202493
      Participant
      Topics: 12
      Replies: 1642
      Points: 2,660
      Helping Hand
      Rank: Community Hero

      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())"
      }
      
Viewing 4 reply threads
  • The topic ‘DateTimePicker_Change’ is closed to new replies.