Quantcast
Channel: Windows PowerShell forum
Viewing all articles
Browse latest Browse all 21975

System.Windows.Forms - If ($button.click -eq "ok") {do this}

$
0
0

Hei,

I am wondering how to make this work:

    If($button_ok)
        {"you pressed the ok button"
        $process.kill()
        }
    ElseIf($button_avbryt)
        {"you pressed the cancel button"
        break
        }

Inside a script like this:

  [reflection.assembly]::loadwithpartialname("System.Windows.Forms") | Out-Null
  [reflection.assembly]::loadwithpartialname("System.Drawing") | Out-Null

# Timer
    $timer1 = New-Object System.Windows.Forms.Timer
    $progressBar1Max = $Seconds
    
  
 # Form
    $form_main = New-Object System.Windows.Forms.Form
    $form_main.Text = $Title
    $form_main.StartPosition = "CenterScreen"
    $form_main.MinimizeBox = $false
    $form_main.MaximizeBox = $false
    $form_main.Size = New-Object -TypeName System.Drawing.Size(400,250)
    $form_main.TopMost = $true

# OK button
    $button_ok = New-Object -TypeName System.Windows.Forms.Button
    $button_ok.Size = New-Object -TypeName System.Drawing.Size(75,23)
    $button_ok.Location = New-Object -TypeName System.Drawing.Size(125,180)
    $button_ok.Text = 'OK'
    $button_ok.Add_Click({$Result=$button_ok.Text;$form_main.Close()})
    $form_main.Controls.Add($button_ok)
    $form_main.AcceptButton = $button_ok

# Cancel knapp (avbryt=cancel)
    $button_avbryt = New-Object -TypeName System.Windows.Forms.Button
    $button_avbryt.Size = New-Object -TypeName System.Drawing.Size(75,23)
    $button_avbryt.Location = New-Object -TypeName System.Drawing.Size(210,180)
    $button_avbryt.Text = 'Avbryt'
    $button_avbryt.Add_Click({$Result=$button_avbryt.Text;$form_main.Close()})
    $form_main.Controls.Add($button_avbryt)
    $form_main.CancelButton = $button_avbryt
    
# Message
    $L = New-Object -TypeName System.Windows.Forms.Label
    $L.Size = New-Object -TypeName System.Drawing.Size(280,100) 
    $L.Location = New-Object -TypeName System.Drawing.Size(10,15) 
    $L.Text = $Message
    $form_main.Controls.Add($L)

# Time left
    $time_label = New-Object -TypeName System.Windows.Forms.Label
    $time_label.Size = New-Object -TypeName System.Drawing.Size(30,15) 
    $time_label.Location = New-Object -TypeName System.Drawing.Size(170,130) 
    $form_main.Controls.Add($time_Label)


$timer1_OnTick = {
    $progressBar1.PerformStep()
   
    $time = $progressBar1Max - $progressBar1.Value
    [char[]]$mins = "{0}" -f ($time / 60)
    $secs = "{0:00}" -f ($time % 60)

    $time_label.Text = "{0}:{1}" -f $mins[0], $secs
    # this doesnt work:
    if ($progressBar1.Value -eq $progressBar1.Maximum) {
      $timer1.Enabled = $false
      #Write-Host "Time is up!"
    }
  }


# Progressbar
    $progressBar1 = New-Object System.Windows.Forms.ProgressBar
    $progressBar1.Value = 1
    $progressBar1.DataBindings.DefaultDataSourceUpdateMode = 0
    $progressBar1.Maximum = $progressBar1Max
    $System_Drawing_Size = New-Object System.Drawing.Size
    $System_Drawing_Size.Width = 250
    $System_Drawing_Size.Height = 30
    $progressBar1.Size = $System_Drawing_Size
    $progressBar1.Step = 1
    $progressBar1.TabIndex = 0
    $System_Drawing_Point = New-Object System.Drawing.Point
    $System_Drawing_Point.X = 45
    $System_Drawing_Point.Y = 146
    $progressBar1.Location = $System_Drawing_Point
    $progressBar1.Style = 1
    $progressBar1.Name = 'progressBar1'
  
    $form_main.Controls.Add($progressBar1)
  
    $timer1.Interval = 1000
    $timer1.add_tick($timer1_OnTick)
    $timer1.Enabled = $True
    $timer1.Start()


    $InitialFormWindowState = $form_main.WindowState
    $form_main.add_Load($OnLoadForm_StateCorrection)
    $form_main.ShowDialog()| Out-Null 

This doesn't work either:

    if ($progressBar1.Value -eq $progressBar1.Maximum) {
      $timer1.Enabled = $false
      #Write-Host "Time is up!"

Any help would be appreciated!




Viewing all articles
Browse latest Browse all 21975

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>