【问题标题】:Reset Powershell Form after User Submits Request用户提交请求后重置 Powershell 表单
【发布时间】:2018-04-17 12:17:02
【问题描述】:

用户成功提交请求后,如何让表单重置/关闭?我正在使用 Powershell ise 进行测试,直到我真正 X 出来,脚本才关闭。这是我当前的功能。

感谢论坛上的人们,我的表单工作正常。我还有另一个我正在努力解决的问题。用户点击提交按钮后如何让表单重置?我现在必须退出表单才能结束脚本。

#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()

【问题讨论】:

  • 我很困惑。您希望表单关闭还是仅清除所有字段并保持打开状态?
  • 我希望用户收到表单已提交的通知,当他/她关闭对话框时,它会清除表单,以便他们基本上可以创建另一个提交
  • 啊啊...给我一分钟回到我的办公桌,我会修改我的答案

标签: powershell powershell-2.0 powershell-3.0 freshdesk


【解决方案1】:

如果不查看表单上的所有控件,实际上不可能为您提供完整的代码,但这应该可以为您提供基本概念。

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

【讨论】:

  • 这达到了我的预期。
【解决方案2】:

这是一个在单击按钮后关闭表单的简单示例。

$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()

要动态访问按钮的表单,而不是传递对表单的实际引用,您可以执行以下操作:

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

【讨论】:

    猜你喜欢
    • 2021-09-17
    • 2018-03-10
    • 1970-01-01
    • 1970-01-01
    • 2013-03-03
    • 1970-01-01
    • 2018-10-05
    相关资源
    最近更新 更多