Alexander Sinno
Alexander Sinno

Reputation: 564

Create a save button in winforms

Goal:

I am attempting to create a button that would save a file to desktop. The incoming file is fetched with an Invoke-WebRequest using the GET method. I want the save button to be in my pop-up window.

Here is an example:

Side note:

This code is sitting in a switch with a variable split three ways.

switch (...) {
  p {
    if ($second -match 'RegexMatch') {
      $resource = $second
      $fileResult = Invoke-WebRequest -Uri https://url.com/$resource/file -WebSession $currentsession
      # End API Call
      Write-Host
      Write-Host '------------' -ForegroundColor Green
      Write-Host 'FILE Results' -ForegroundColor Green
      Write-Host '------------' -ForegroundColor Green
       # Create Window
      Add-Type -AssemblyName System.Windows.Forms
      $form = New-Object -TypeName System.Windows.Forms.Form
      $form.StartPosition = 'CenterScreen'
      $form.KeyPreview = $true
      $form.Add_KeyDown {
        if ($_.Control -and $_.KeyCode -eq 'F') {
          Add-Type -AssemblyName Microsoft.VisualBasic
          $stringToFind = [Microsoft.VisualBasic.Interaction]::InputBox('Please enter your search terms', 'Find')
          $pos = $textBox.Text.IndexOf($stringToFind)

          if ($pos -ne -1) {
            $textBox.SelectionStart = $pos
            $textBox.SelectionLength = $stringToFind.Length
          }
        }
      }
      # Textbox
      $textBox = New-Object -TypeName System.Windows.Forms.TextBox
      $textBox.Dock = [Windows.Forms.DockStyle]::Fill
      $textBox.ReadOnly =$true
      $textBox.Multiline = $true
      $textBox.ScrollBars = 'Vertical'
      $textBox.Font = New-Object -TypeName System.Drawing.Font -ArgumentList ('Arial',12)
      $textBox.ForeColor = 'White'
      $textBox.Text = $fileResult
      $textBox.BackColor = 'Black'
      $textBox.ShortcutsEnabled = $true
      $Form.Controls.Add($textBox)
        # Button
      $btn = New-Object -TypeName System.Windows.Forms.Button
      $btn.Text = 'Finish'
      $btn.DialogResult = 'Ok'
      $btn.Dock = 'bottom'
      $form.Controls.Add($btn)
      if ($form.ShowDialog() -eq 'Ok') {
        $tb.lines
      }
    } else {
      Write-Host
      Write-Warning -Message 'Please enter a valid FILE ID'
      Write-Host
    }
    break
  }
  ...
}

Purpose:

I want to add an option for the user to download the file for a closer look in a different application.

Question:

How would I begin to create a button utilizing winforms in powershell to save this file to disk?

Here is what I have tried:

      $BtnSave=New-Object -TypeName System.Windows.Forms.Button
      $BtnSave.Text='Save'
      $BtnSave.Dock='bottom'
      $btnSave.DialogResult='Ok'
      $form.Controls.Add($BtnSave)
      $BtnSave.Add_Click({
          $SaveFileDialog = New-Object 'System.Windows.Forms.SaveFileDialog'
          if ($SaveFileDialog.ShowDialog() -eq 'Ok')
          {
            $textBox.Text = $SaveFileDialog.FileName
            Write-Information 'File Saved'
          }
      })

New Problem:

File is not saving to disk still, but the save file dialog does show up on click. In addition, using Switch -OutFile with my Invoke-WebRequest is shooting me an error.

Error:

Invoke-WebRequest : Missing an argument for parameter 'OutFile'. Specify a parameter of type 'System.String' and try again.

Upvotes: 2

Views: 1127

Answers (1)

sodawillow
sodawillow

Reputation: 13176

After adding a button where you want in the form, the Add_Click() method will allow you to handle its click event and run any scriptblock you want (when button is clicked).

At this point, the -OutFile argument for Invoke-WebRequest will help with saving the downloaded file to disk (pass it the desired path to the file).

Upvotes: 2

Related Questions