0

How can I get a form to reset/close after the user has successfully submitted their request? I'm testing using Powershell ise and the script never closes until I actually X out. Here are my current functions.

My form is working like it should thanks to the folks on this forum. I have another issue I'm struggling with. How do I get the form to reset after the user hits the submit button? I currenlty have to exit out of the form for the script to end.

#region gui events {
$btn1.Add_Click({ sendRequest; thankyou })
#endregion events }

#endregion GUI }
function sendRequest()
{
    # API Key
    $FDApiKey="api key"
    #################################################

    # Force TLS1.2 as Powershell defaults to TLS 1.0 and Freshdesk will fail connections 
    [Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::TLS12

    # Prep
    $pair = "$($FDApiKey):$($FDApiKey)"
    $bytes = [System.Text.Encoding]::ASCII.GetBytes($pair)
    $base64 = [System.Convert]::ToBase64String($bytes)
    $basicAuthValue = "Basic $base64"
    $FDHeaders = @{ Authorization = $basicAuthValue }
    ##################################################

    $Body = @{
        description = $description.Text
        email = $email.Text
        subject = $subject.Text
        type = $request.Text
        priority = 1
        status = 2
    }

    Invoke-WebRequest "https://clasd.freshdesk.com/api/v2/tickets/" `
        -Headers $FDHeaders `
        -ContentType "application/json" `
        -Method Post `
        -Body ($Body | ConvertTo-JSON)
}

function thankyou ()
{
    [System.Windows.Forms.MessageBox]::Show("Your ticket has been submitted!" , "Status") 
}

#Write your logic code here

[void]$Form.ShowDialog()
JohnLBevan
  • 22,735
  • 13
  • 96
  • 178
Justin Merwin
  • 93
  • 2
  • 8
  • 16
  • I'm confused. Do you want the form to close Or do you want it to just clear all the fields and still be open? – EBGreen Apr 17 '18 at 12:37
  • I want the user to get notified that the form has been submitted, and when he/she closes the dialog box it clears the form so they could essentially create another submission – Justin Merwin Apr 17 '18 at 13:13
  • Aaahh...gimme a minute to get back to my desk and I will amend my answer – EBGreen Apr 17 '18 at 13:31

2 Answers2

3

Without seeing all the controls that are on your form it is not really possible to give you complete code but this should give you the basic idea.

function thankyou{
  [System.Windows.Forms.MessageBox]::Show("Your ticket has been submitted!" , "Status")
  $description.Text = ''
  $email.Text = ''
  $subject.Text = ''
  $request.Text = ''
}
EBGreen
  • 36,735
  • 12
  • 65
  • 85
0

Here's a simple example of closing a form after a button's clicked.

$form = [System.Windows.Forms.Form]::new()

$button = [System.Windows.Forms.Button]::new()
$button.Text = "click me quick"
$button.Add_Click({$form.Close()}) #add any other logic you require to the button click's anonymous function
$form.Controls.Add($button)

$form.ShowDialog()

To dynamically access the button's form, rather than passing an actual reference to the form, you could do something like this:

function OnButtonClick {
    $theForm = $this.Parent
    $theForm.DialogResult = [System.Windows.Forms.DialogResult]::OK
    $theForm.Close()
}

$form = [System.Windows.Forms.Form]::new()

$button = [System.Windows.Forms.Button]::new()
$button.Text = "click me quick"
$button.Add_Click({OnButtonClick})
$form.Controls.Add($button)

$form.ShowDialog()
JohnLBevan
  • 22,735
  • 13
  • 96
  • 178