I'm trying to enable textboxes in my form only if the field prior has been completed. I'm stuck as I can still enter information in my subject textbox even though the combo box before it is empty. Everything I'm finding is how to disable a button and not individual textboxes or comboboxs. I assume it would all work the same.
Edit With Code
<# 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()
First, it looks like you never attached the Validate function to any event on the combo box. So for example you could use the TextChanged or SelectedIndexChanged event on the combo box.
You would attach it like this:
$request.add_TextChanged({Validate})
or
$request.add_SelectedIndexChanged({Validate})
Then in your Validate method you can check the index like this:
function Validate{
if($request.SelectedIndex -ne -1){
$subject.Enabled = $true
}
else{
$subject.Enabled = $false
}
}
Negative 1 is the value when no index is selected. If you prefer you can check the length of the text. This depends on whether the user will type in this combo, or just select pre-existing items from the list. As was already pointed out in comments, you want to disable the text box from the start using
$request.Enabled = $false
Here is a stripped down example based on your code. This version assumes that the user will be selecting pre-existing values from the combo:
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()