在Powershell中启用和禁用文本框

时间:2018-04-19 12:51:11

标签: powershell powershell-v2.0 powershell-v3.0 freshdesk

我只是在字段优先完成后才尝试在表单中启用文本框。我被卡住了,因为我仍然可以在我的主题文本框中输入信息,即使它之前的组合框是空的。我发现的一切都是如何禁用按钮而不是单个文本框或组合框。我认为这一切都会起作用。

使用代码进行编辑

<# This form was created using POSHGUI.com  a free online gui designer for PowerShell
.NAME
    Ticket Request
#>

Add-Type -AssemblyName System.Windows.Forms
[System.Windows.Forms.Application]::EnableVisualStyles()

#region begin GUI{ 

$Form                            = New-Object system.Windows.Forms.Form
$Form.ClientSize                 = '600,400'
$Form.text                       = "Ticket Request Form"
$Form.TopMost                    = $false

$image = [System.Drawing.Image]::Fromfile('G:\My Drive\CLASD Tech\CL Logos\CL_Lion.png')     
$pictureBox = new-object Windows.Forms.PictureBox 
$pictureBox.width=320
$pictureBox.height=240
$pictureBox.top=10
$pictureBox.left=375
#$pictureBox.Right=5
$pictureBox.Image=$image

$Label1                          = New-Object system.Windows.Forms.Label
$Label1.text                     = "Email"
$Label1.AutoSize                 = $true
$Label1.width                    = 25
$Label1.height                   = 10
$Label1.location                 = New-Object System.Drawing.Point(25,78)
$Label1.Font                     = 'Microsoft Sans Serif,10'

$Label2                          = New-Object system.Windows.Forms.Label
$Label2.text                     = "Request Type"
$Label2.AutoSize                 = $true
$Label2.width                    = 25
$Label2.height                   = 10
$Label2.location                 = New-Object System.Drawing.Point(25,114)
$Label2.Font                     = 'Microsoft Sans Serif,10'

$Label3                          = New-Object system.Windows.Forms.Label
$Label3.text                     = "Subject"
$Label3.AutoSize                 = $true
$Label3.width                    = 25
$Label3.height                   = 10
$Label3.location                 = New-Object System.Drawing.Point(25,152)
$Label3.Font                     = 'Microsoft Sans Serif,10'

$Label4                          = New-Object system.Windows.Forms.Label
$Label4.text                     = "Description"
$Label4.AutoSize                 = $true
$Label4.width                    = 25
$Label4.height                   = 10
$Label4.location                 = New-Object System.Drawing.Point(25,195)
$Label4.Font                     = 'Microsoft Sans Serif,10'

$TextBox1                        = New-Object system.Windows.Forms.TextBox
$TextBox1.multiline              = $false
$TextBox1.width                  = 100
$TextBox1.height                 = 20
$TextBox1.location               = New-Object System.Drawing.Point(-12,-19)
$TextBox1.Font                   = 'Microsoft Sans Serif,10'


$email                           = New-Object system.Windows.Forms.TextBox
$email.multiline                 = $false
$email.width                     = 267
$email.height                    = 20
$email.location                  = New-Object System.Drawing.Point(122,74)
$email.Font                      = 'Microsoft Sans Serif,10'
$email.Text                      = (Get-ADUser $env:USERNAME -Properties mail).mail
#$email.AppendText("@clasd.net")
$email.ReadOnly                 = 'true'

$subject                         = New-Object system.Windows.Forms.TextBox
$subject.multiline               = $false
$subject.width                   = 267
$subject.height                  = 20
$subject.location                = New-Object System.Drawing.Point(122,147)
$subject.Font                    = 'Microsoft Sans Serif,10'

$description                        = New-Object system.Windows.Forms.TextBox
$description.multiline              = $true
$description.width                  = 267
$description.height                 = 163
$description.location               = New-Object System.Drawing.Point(122,190)
$description.Font                   = 'Microsoft Sans Serif,10'

$request                         = New-Object system.Windows.Forms.ComboBox
$request.width                   = 267
$request.height                  = 20
@('Maintenance','Technology') | ForEach-Object {[void] $request.Items.Add($_)}
$request.location                = New-Object System.Drawing.Point(122,113)
$request.Font                    = 'Microsoft Sans Serif,10'

$btn1                            = New-Object system.Windows.Forms.Button
$btn1.text                       = "Send Request"
$btn1.width                      = 98
$btn1.height                     = 40
$btn1.location                   = New-Object System.Drawing.Point(450,323)
$btn1.Font                       = 'Microsoft Sans Serif,10'

$btn2                            = New-Object system.Windows.Forms.Button
$btn2.text                       = "View My Tickets"
$btn2.width                      = 98
$btn2.height                     = 40
$btn2.location                   = New-Object System.Drawing.Point(450,200)
$btn2.Font                       = 'Microsoft Sans Serif,10'

$Form.controls.AddRange(@($Label1,$Label2,$Label3,$Label4,$TextBox1,$email,$subject,$description,$request,$btn1,$btn2))
$Form.Controls.add($picturebox)

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

#endregion GUI }

function sendRequest()
{
# API Key
$FDApiKey="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 ()
{
New-BurntToastNotification -Text 'Request Sent' -AppLogo 'Y:\JustLion.jpg'
$description.Text = ''
$email.Text = ''
$subject.Text = ''
$request.Text = ''
}

<#function opentickets()
{
# API Key
$FDApiKey="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 }
##################################################

$requester = $email.Text
}

Invoke-RestMethod "https://clasd.freshdesk.com/api/v2/tickets?email='$requester'" -Headers $FDHeaders  -ContentType "application/json" -Method Get 
#>
function validate(){
    if($request.SelectedIndex -ge 1){
        $subject.Enabled = $true
    }
    else{
        $subject.Enabled = $false
    }
}

#Write your logic code here


[void]$Form.ShowDialog()

1 个答案:

答案 0 :(得分:0)

首先,您似乎从未将Validate函数附加到组合框上的任何事件。例如,您可以在组合框上使用TextChanged或SelectedIndexChanged事件。

你会像这样附上它:

$request.add_TextChanged({Validate})

$request.add_SelectedIndexChanged({Validate})

然后在您的Validate方法中,您可以检查索引:

function Validate{
    if($request.SelectedIndex -ne -1){
        $subject.Enabled = $true
    }
    else{
        $subject.Enabled = $false
    }
}

负1是未选择索引时的值。如果您愿意,可以查看文本的长度。这取决于用户是键入此组合,还是仅从列表中选择预先存在的项目。正如评论中已经指出的那样,您想要从一开始就使用

禁用文本框
$request.Enabled = $false

这是一个基于您的代码的精简示例。此版本假定用户将从组合中选择预先存在的值:

function Validate{
    if($request.SelectedIndex -ne -1){
        $subject.enabled = $true
    }
    else{
        $subject.Enabled = $false
    }
}

$Form = New-Object system.Windows.Forms.Form
$Form.ClientSize = '600,400'
$Form.text = "Ticket Request Form"

$request = New-Object system.Windows.Forms.ComboBox
$request.width = 267
$request.height = 20
$request.location = New-Object System.Drawing.Point(122,113)
$request.Font = 'Microsoft Sans Serif,10'
$request.Items.Add('1')
$request.Items.Add('2')
$request.add_TextChanged({Validate})


$subject = New-Object system.Windows.Forms.TextBox
$subject.multiline = $false
$subject.width = 267
$subject.height = 20
$subject.location = New-Object System.Drawing.Point(122,147)
$subject.Font = 'Microsoft Sans Serif,10'
$subject.Enabled = $false

$Form.Controls.Add($request)
$Form.Controls.Add($subject)

$form.ShowDialog()
相关问题