在 Powershell 中启用和禁用文本框
Enable and Disable textboxes in Powershell
我试图仅在完成之前的字段后才在我的表单中启用文本框。我被卡住了,因为我仍然可以在我的主题文本框中输入信息,即使它之前的组合框是空的。我发现的一切都是如何禁用按钮而不是单个文本框或组合框。我认为它们都可以正常工作。
使用代码编辑
<# 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()
首先,您似乎从未将 Validate 函数附加到组合框上的任何事件。例如,您可以在组合框上使用 TextChanged 或 SelectedIndexChanged 事件。
您可以这样附加它:
$request.add_TextChanged({Validate})
或
$request.add_SelectedIndexChanged({Validate})
然后在您的验证方法中,您可以像这样检查索引:
function Validate{
if($request.SelectedIndex -ne -1){
$subject.Enabled = $true
}
else{
$subject.Enabled = $false
}
}
负1是没有索引select时的值。如果您愿意,可以检查文本的长度。这取决于用户是要输入这个组合,还是只输入列表中的 select 个预先存在的项目。正如评论中已经指出的那样,您想从一开始就使用
禁用文本框
$request.Enabled = $false
这是一个基于您的代码的精简示例。此版本假定用户将 selecting 来自组合的预先存在的值:
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()
我试图仅在完成之前的字段后才在我的表单中启用文本框。我被卡住了,因为我仍然可以在我的主题文本框中输入信息,即使它之前的组合框是空的。我发现的一切都是如何禁用按钮而不是单个文本框或组合框。我认为它们都可以正常工作。
使用代码编辑
<# 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()
首先,您似乎从未将 Validate 函数附加到组合框上的任何事件。例如,您可以在组合框上使用 TextChanged 或 SelectedIndexChanged 事件。
您可以这样附加它:
$request.add_TextChanged({Validate})
或
$request.add_SelectedIndexChanged({Validate})
然后在您的验证方法中,您可以像这样检查索引:
function Validate{
if($request.SelectedIndex -ne -1){
$subject.Enabled = $true
}
else{
$subject.Enabled = $false
}
}
负1是没有索引select时的值。如果您愿意,可以检查文本的长度。这取决于用户是要输入这个组合,还是只输入列表中的 select 个预先存在的项目。正如评论中已经指出的那样,您想从一开始就使用
禁用文本框$request.Enabled = $false
这是一个基于您的代码的精简示例。此版本假定用户将 selecting 来自组合的预先存在的值:
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()