Validating Event Not Firing - winforms

I'm having real trouble getting the validating event to fire. I have created a very simple windows form to demonstrate the problem. The form contains two textboxes and I'd expect the validating event to fire when the tab key is pressed in the first textbox, but it isn't. I'm using Powershell V4.0, running under Windows 7 Professional SP1. Code follows:-
Function ValidateField( [string]$FieldValue )
{
Write-Host "ValidateField: Function Entered"
if ( $FieldValue -eq $Null -Or $FieldValue.Trim().Length -eq 0 ) { Return $True } else { Return $False }
}
Function GenerateForm
{
Write-Host "GenerateForm: Function Entered"
[Reflection.Assembly]::LoadWithPartialName( "System.Windows.Forms" ) | Out-Null
[Reflection.Assembly]::LoadWithPartialName( "System.Drawing" ) | Out-Null
$Form = New-Object System.Windows.Forms.Form ; $IFWS = New-Object System.Windows.Forms.FormWindowState
$Code = New-Object System.Windows.Forms.TextBox ; $Desc = New-Object System.Windows.Forms.TextBox
$Form.ClientSize = New-Object System.Drawing.Size( 400,200 )
$Form.StartPosition = "CenterScreen" ; $Form.add_Load( $FormLoad )
$OnLoadForm_StateCorrection = { $Form.WindowState = $IFWS }
$Code.DataBindings.DefaultDataSourceUpdateMode = 0 ; $Code.Location = New-Object System.Drawing.Size( 10,10 )
$Code.Size = New-Object System.Drawing.Size( 40,20 ) ; $Code.CausesValidation = $True
$Code_Validating = [System.ComponentModel.CancelEventHandler] {
{ $Result = ( ValidateField $Code.Text )
if ( $Result -eq $True ) { $_.Cancel = $True } }
}
$Form.Controls.Add( $Code )
$Desc.DataBindings.DefaultDataSourceUpdateMode = 0 ; $Desc.Location = New-Object System.Drawing.Size( 100,10 )
$Desc.Size = New-Object System.Drawing.Size( 169,20 ) ; $Form.Controls.Add( $Desc )
$IFWS = $Form.WindowState ; $Form.add_Load( $OnLoadForm_StateCorrection ) ; $Form.ShowDialog() | Out-Null
}
GenerateForm
It may well be that I'm just missing something very very simple - but this has been driving me mad for 3 days now, so any help gratefully accepted.

I don't see where you are connecting to the Validating event. Replace your $Code_Validating assignment with this:
$Code.add_Validating({
$result = ValidateField $sender.Text
if ($result) {$eventArgs.Cancel = $true}
})

Related

Ping Multiple IP/HostNames

I am just trying to make a simple IP/Hostname checker. The one part I am struggling with is getting it to ping each item from a list. It only shows the result of the first, or last one.
I have tried adding items from the text box into an array and pinging them that way, and it had the same results.
Here is my Current Code.
[System.Windows.Forms.Application]::EnableVisualStyles()
function ping-list{
$IPStatus.Text = ""
$names = #($IPList.text)
$Names | ForEach-Object{
if (Test-Connection -ComputerName $_ -Count 1){
$IPStatus.Text += "$_ is Online"
Write-Host "$_ is Online"
}
else{
$IPStatus.Text += "$_ is Offline"
Write-Host "$_ is Offline"
}
}
}
$Pinger = New-Object system.Windows.Forms.Form
$Pinger.ClientSize = '657,557'
$Pinger.text = "Pinger"
$Pinger.TopMost = $false
$IPStatus = New-Object system.Windows.Forms.TextBox
$IPStatus.multiline = $true
$IPStatus.width = 234
$IPStatus.height = 498
$IPStatus.enabled = $true
$IPStatus.location = New-Object System.Drawing.Point(408,45)
$IPStatus.Font = 'Microsoft Sans Serif,10'
$IPStatus.Text = "Ready"
$IPList = New-Object system.Windows.Forms.TextBox
$IPList.multiline = $true
$IPList.width = 234
$IPList.height = 498
$IPList.enabled = $true
$IPList.location = New-Object System.Drawing.Point(15,45)
$IPList.Font = 'Microsoft Sans Serif,10'
$IPList.Text = #("127.0.0.1")
$PingButton = New-Object system.Windows.Forms.Button
$PingButton.text = "button"
$PingButton.width = 60
$PingButton.height = 30
$PingButton.location = New-Object System.Drawing.Point(298,240)
$PingButton.Font = 'Microsoft Sans Serif,10'
$PingButton.Add_Click({ping-list})
$Pinger.controls.AddRange(#($IPStatus,$IPList,$PingButton))
$Pinger.Add_Shown( {$Pinger.Activate()})
$Pinger.ShowDialog()
$IPList.text gets all the text in the textbox.
Instead use Lines
function ping-list{
$IPStatus.Text = ""
$Names = #($IPList.Lines)
$Names | ForEach-Object{
if (Test-Connection -ComputerName $_ -Count 1){
$IPStatus.Text += "$_ is Online`r`n"
Write-Host "$_ is Online"
}
else{
$IPStatus.Text += "$_ is Offline`r`n"
Write-Host "$_ is Offline"
}
}
}

How to close form GUI after checking existing file in PowerShell?

I want to check an existing file, if the process still waiting for the file, it will display a GUI window. After the file is exist, the window will close automatically.
I tried this code, the window can not close, even the file already exist.
Checking the file:
$SN = "708TSTA"
$MAC = "2E5961370"
function Find {
$n = 0
while (-not (Get-ChildItem -Name "D:\SERVER\" | Where-Object {$_ -like "*$SN-$MAC*"})) {
Start-Sleep -s 1
D:\Auto\GUI.ps1
$n++
(Get-ChildItem -Name "D:\SERVER\" | Where-Object {$_ -like "*$SN-$MAC*"})
Write-Host "Attempt no $n"
}
Write-Host ">>Flag found after $n attempts"
return $true
}
if (Find) {
Write-Host "Found"
}
GUI.ps1:
Add-Type -AssemblyName System.Windows.Forms
[System.Windows.Forms.Application]::EnableVisualStyles()
$Form = New-Object System.Windows.Forms.Form
$Form.ClientSize = '578,400'
$Form.Text = "Form"
$Form.BackColor = "#c1daf7"
$Form.WindowState = 'Maximized'
$Form.FormBorderStyle = "FixedDialog"
$Label1 = New-Object System.Windows.Forms.Label
$Label1.Text = "UNDER PROCESS"
$Label1.AutoSize = $true
$Label1.Width = 25
$Label1.Height = 10
$Label1.Location = New-Object System.Drawing.Point(600,300)
$Label1.Font = 'Microsoft Sans Serif,30,style=Bold,Underline'
$Label1.ForeColor = "#d0021b"
$Label2 = New-Object System.Windows.Forms.Label
$Label2.Text = "WAITING"
$Label2.AutoSize = $true
$Label2.Width = 25
$Label2.Height = 10
$Label2.Location = New-Object System.Drawing.Point(770,500)
$Label2.Font = 'Microsoft Sans Serif,20,style=Bold'
$Label2.ForeColor = "#fb0505"
$Check = Get-ChildItem -Name "D:\SERVER\" | Where-Object {$_ -like "*$SN-$MAC*"}
if($Check) {
Write-Host "File Exist"
$Form.Close()
}
$Form.Controls.AddRange(#($Label1,$Label2))
[void]$Form.ShowDialog()
Instead of doing Start-Sleep inside the GUI, it is better to use a timer so the form stays responsive.
I changed the code of the GUI.ps1 (not the way it looks) like this:
Param (
[string]$Path = '*.*',
[string]$MaxAttempts = 5
)
Add-Type -AssemblyName System.Windows.Forms
[System.Windows.Forms.Application]::EnableVisualStyles()
# set things up for the timer
$script:nAttempts = 0
$timer = New-Object System.Windows.Forms.Timer
$timer.Interval = 1000 # 1 second
$timer.Add_Tick({
$global:Result = $null
$script:nAttempts++
$file = Get-Item -Path $Path
if ($file) {
$global:Result = [PSCustomObject]#{
Exists = $true
FileName = $file.FullName
Attempts = $script:nAttempts
}
$timer.Dispose()
$Form.Close()
}
elseif ($script:nAttempts -ge $MaxAttempts) {
$global:Result = [PSCustomObject]#{
Exists = $false
FileName = ''
Attempts = $script:nAttempts
}
$timer.Dispose()
$Form.Close()
}
})
$Form = New-Object System.Windows.Forms.Form
$Form.ClientSize = '578,400'
$Form.Text = "Form"
$Form.BackColor = "#c1daf7"
$Form.WindowState = 'Maximized'
$Form.FormBorderStyle = "FixedDialog"
$Label1 = New-Object System.Windows.Forms.Label
$Label1.Text = "UNDER PROCESS"
$Label1.AutoSize = $true
$Label1.Width = 25
$Label1.Height = 10
$Label1.Location = New-Object System.Drawing.Point(600,300)
$Label1.Font = 'Microsoft Sans Serif,30,style=Bold,Underline'
$Label1.ForeColor = "#d0021b"
$Label2 = New-Object System.Windows.Forms.Label
$Label2.Text = "WAITING"
$Label2.AutoSize = $true
$Label2.Width = 25
$Label2.Height = 10
$Label2.Location = New-Object System.Drawing.Point(770,500)
$Label2.Font = 'Microsoft Sans Serif,20,style=Bold'
$Label2.ForeColor = "#fb0505"
$Form.Controls.AddRange(#($Label1,$Label2))
# start the timer as soon as the dialog is visible
$Form.Add_Shown({ $timer.Start() })
[void]$Form.ShowDialog()
# clean up when done
$Form.Dispose()
And to call it from your other script, use:
$SN = "708TSTA"
$MAC = "2E5961370"
function Test-FileExists {
$file = Get-Item -Path "D:\*$SN-$MAC*"
if ($file) {
$global:Result = [PSCustomObject]#{
Exists = $true
FileName = $file.FullName
Attempts = 1
}
}
else {
& "D:\GUI.ps1" -Path "D:\*$SN-$MAC*" -MaxAttempts 3
}
}
# call the function that can call the GUI.ps1 script
Test-FileExists
# check the Global result object
if ($global:Result.Exists) {
Write-Host "File '$($global:Result.FileName)' Exists. Found after $($global:Result.Attempts) attempts." -ForegroundColor Green
}
else {
Write-Host "File not found after $($global:Result.Attempts) attempts." -ForegroundColor Red
}
Update
As per your comments, I understand that the calling script should show the form (which does nothing more that show on screen) AND is responsible for closing it after the file has been found.
The code below should do what you ask by defining the $Form as a global variable and by using the .Show() method of the form instead of ShowDialog():
GUI.ps1
Add-Type -AssemblyName System.Windows.Forms
[System.Windows.Forms.Application]::EnableVisualStyles()
$global:Form = New-Object System.Windows.Forms.Form
$global:Form.ClientSize = '578,400'
$global:Form.Text = "Form"
$global:Form.BackColor = "#c1daf7"
$global:Form.WindowState = 'Maximized'
$global:Form.FormBorderStyle = "FixedDialog"
$global:Form.ControlBox = $false # hide sizing and close buttons
$global:Form.TopMost = $true
$Label1 = New-Object System.Windows.Forms.Label
$Label1.Text = "UNDER PROCESS"
$Label1.AutoSize = $true
$Label1.Width = 25
$Label1.Height = 10
$Label1.Location = New-Object System.Drawing.Point(600,300)
$Label1.Font = 'Microsoft Sans Serif,30,style=Bold,Underline'
$Label1.ForeColor = "#d0021b"
$Label2 = New-Object System.Windows.Forms.Label
$Label2.Text = "WAITING"
$Label2.AutoSize = $true
$Label2.Width = 25
$Label2.Height = 10
$Label2.Location = New-Object System.Drawing.Point(770,500)
$Label2.Font = 'Microsoft Sans Serif,20,style=Bold'
$Label2.ForeColor = "#fb0505"
$global:Form.Controls.AddRange(#($Label1,$Label2))
# don't use ShowDialog() here because it will block the calling script
$global:Form.Show()
the calling script
function Test-FileExists {
[CmdletBinding()]
param (
[parameter(Mandatory = $true, ValueFromPipeline = $true, Position = 0)]
[string]$Path,
[string]$Pattern = '*.*'
)
$nAttempts = 1
$file = Get-ChildItem -Path $Path -Filter $Pattern -File | Select-Object -First 1
if (!$file) {
# show the GUI
& "D:\GUI.ps1"
do {
Start-Sleep -Seconds 1
$nAttempts++
Write-Verbose "Attempt No. $nAttempts"
$file = Get-ChildItem -Path $Path -Filter $Pattern -File | Select-Object -First 1
} until ($file)
# clean up the form
$global:Form.Dispose()
$global:Form = $null
}
Write-Verbose "File '$($file.FullName)' Exists. Found after $nAttempts attempt(s)."
return $true
}
$SN = "708TSTA"
$MAC = "2E5961370"
# call the function that can call the GUI.ps1 script
if (Test-FileExists -Path 'D:\SERVER\SHARE' -Pattern "*$SN-$MAC*" -Verbose) {
Write-Host "Found"
}
Hope that helps

Manual Checkbox enable

I have a ComboBox control on my form. What I want is when I change an item in the ComboBox from one to another, the event was handled. It is important that when changing and not when choosing the same element. All this time I used ComboBox.Add_SelectionChangeCommitted($function), but soon I realized that the block that is also executed by the handler when the same (selected) item is selected from the list. A little digging in ComboBox events I am completely confused. Having tried several events (SelectedItemChanged, SelectedIndexChanged) I could never able to achieve the desired result.
An example of what I want to do and what should not be done several times when choosing the same element. When the block of code for Manual is executed several times, the contents of all TextBox are cleared, but I don’t want to.
Add-Type -AssemblyName System.Windows.Forms
[System.Windows.Forms.Application]::EnableVisualStyles()
$Form = New-Object system.Windows.Forms.Form
$Form.ClientSize = '420,240'
$Form.TopMost = $false
$Form.FormBorderStyle = 'Fixed3D'
$Form.MaximizeBox = $false
$ComboBox = New-Object system.Windows.Forms.ComboBox
$ComboBox.width = 391
$ComboBox.height = 47
#('Automatic (DHCP)','Manual Input') | ForEach-Object {[void] $ComboBox.Items.Add($_)}
$ComboBox.location = New-Object System.Drawing.Point(13,57)
$ComboBox.DropDownStyle = 'DropDownList'
$ComboBox.DrawMode = 'OwnerDrawFixed'
$Button = New-Object system.Windows.Forms.Button
$Button.text = "Set"
$Button.width = 60
$Button.height = 30
$Button.visible = $false
$Button.enabled = $false
$Button.location = New-Object System.Drawing.Point(336,126)
$CheckBox = New-Object system.Windows.Forms.CheckBox
$CheckBox.width = 65
$CheckBox.height = 15
$CheckBox.visible = $false
$CheckBox.Checked = $false
$CheckBox.enabled = $true
$CheckBox.location = New-Object System.Drawing.Point(16,210)
$TextBox1 = New-Object system.Windows.Forms.TextBox
$TextBox1.Name = "TextBox1"
$TextBox1.multiline = $false
$TextBox1.width = 40
$TextBox1.height = 20
$TextBox1.visible = $false
$TextBox1.enabled = $true
$TextBox1.location = New-Object System.Drawing.Point(81,100)
$TextBox2 = New-Object system.Windows.Forms.TextBox
$TextBox2.Name = "TextBox2"
$TextBox2.multiline = $false
$TextBox2.width = 40
$TextBox2.height = 20
$TextBox2.visible = $false
$TextBox2.enabled = $false
$TextBox2.location = New-Object System.Drawing.Point(81,208)
$Form.controls.AddRange(#($TextBox1,$TextBox2,$Button,$ComboBox,$CheckBox))
$global:ManualChecked = $null
$global:AutomaticChecked = $null
$ComboBox.Add_SelectionChangeCommitted($methodSelection)
$netwValues = New-Object 'System.Collections.Hashtable'
$methodSelection =
{
switch($ComboBox.Text)
{
"Manual Input"
{
$CheckBox.Visible = $Button.Visible = $true
$Button.Enabled = $false
ForEach ($control in $Form.controls)
{
if ($control -is [System.Windows.Forms.TextBox] )
{
$control.Visible = $control.Enabled = $true
$control.Clear()
if($netwValues.Count -gt 0)
{
$control.Text = $netwValues.Item($control.Name)
$netwValues.Remove($control.Name)
}
}
}
if($global:ManualChecked -eq 1)
{
$CheckBox.Checked = $true
$TextBox2.Enabled = $true
}
else
{
$CheckBox.Checked = $false
$TextBox2.Enabled = $false
}
}
"Automatic (DHCP)"
{
ForEach($control in $Form.controls)
{
if($control -is [System.Windows.Forms.TextBox] -or $control -is [System.Windows.Forms.CheckBox] -or $control -is [System.Windows.Forms.Button])
{
$control.Visible = $control.Enabled = $true
if($control -is [System.Windows.Forms.Label] -or $control -is [System.Windows.Forms.TextBox])
{
$control.Enabled = $false
if($control -is [System.Windows.Forms.TextBox])
{
if($control.Text.Length)
{
$netwValues.Add($control.Name,$control.Text)
$control.Clear()
}
}
}
}
}
if($global:AutomaticChecked -eq 1)
{
$CheckBox.Checked = $true
}
else
{
$CheckBox.Checked = $false
}
}
}
}
You can implement this logic yourself.
# Simple class for using as item for combobox instead of just string.
class CbItem
{
CbItem([string] $value)
{
$this.Value = $value
}
[string] $Value
[string] ToString()
{
return $this.Value
}
}
# Add instances of this class into combobox.
$ComboBox.Items.Add([CbItem]::new('Automatic (DHCP)'))
$ComboBox.Items.Add([CbItem]::new('Manual Input'))
# Create a variable for storing current combobox's item.
$global:CurrentCbItem = $null
# Add logic at the begining of handler for exiting if selected item is the same as the last one.
$methodSelection =
{
if ([Object]::ReferenceEquals($global:CurrentCbItem, $ComboBox.SelectedItem))
{
return
}
$global:CurrentCbItem = $ComboBox.SelectedItem
...
}

PowerShell Winforms Textbox updates on other input but overwriteable

Afternoon all,
I wanna believe this one is pretty easy but hitting a wall and can't figure out why.
I have a PowerShell winform that's going to create a new user account from a template. One of the fields I'm working on now is the line manager; once a template account has been found, auto-fill the line manager field, but allow it to be overwritten if this is incorrect.
For the pre-populated stuff I've been using:
$FORMCONTROL.Add_TextChanged( {
However once the template has been found and the line manager field written too, I can't overwrite it. Is there another Event I should be using to populate the box but allow me to delete the content and add something else?
The code below is a much cut down version of what I'm using. The functions allow for finding a user account and populating the ReadOnly box.
Function FindUser {
IF ( $SEARCHUSER -like $NULL -or $SEARCHUSER -like " *" )
{ }
ELSEIF ( $ACCOUNT = dsquery user -samid $SEARCHUSER )
{ $ACCOUNT = Get-ADUser $SEARCHUSER -Property * }
ELSEIF ( $ACCOUNT = Get-ADUser -Filter { mail -eq $SEARCHUSER } -ea sil )
{ $ACCOUNT = Get-ADUser -Filter { mail -eq $SEARCHUSER } -Property * }
ELSEIF ( dsquery user -name $SEARCHUSER )
{ $ACCOUNT = Get-ADUser -Filter { name -eq $SEARCHUSER } -Property * }
ELSE
{ $( foreach ( $SEARCHUSER in ( Get-ADUser -Filter { ( Surname -like $SEARCHUSER ) -and ( Enabled -eq $TRUE )
} -Properties Mail, Department, Office | sort Name ) )
{ $SEARCHUSER | Select Name, #{ N = "Username" ; E = { $_.SamAccountName } }, Mail, Department, Office
} ) | Out-GridView -Title 'Select the user account' -OutputMode Single | %{
TRY
{ $ACCOUNT = Get-ADUser $_.UserName -Property * }
CATCH
{ } } }
IF ( ( $ACCOUNT.SamAccountName ).count -eq 1 )
{ $Script:ACCOUNT = $ACCOUNT }
ELSE
{ $Script:ACCOUNT = $NULL } }
Function TemplateUser {
IF ( $ACCOUNT -ne $NULL )
{ $TAB1TEMPLATE_5.Text = ( $ACCOUNT.Name ) }
ELSEIF ( $TAB1TEMPLATE_3.Text.Length -lt 4 )
{ $TAB1TEMPLATE_5.Text = $NULL } }
# Creates the parent form and controls
$SDC = New-Object System.Windows.Forms.Form
$SDC.Location = New-Object System.Drawing.Size( 270,175 )
$SDC.Size = New-Object System.Drawing.Size( 900,600 )
$SDC.StartPosition = "CenterScreen"
$SDC.BackColor = "Lavender"
$SDC.Font = "Calibri, 8.5"
$SDC.FormBorderStyle = "Fixed3D"
#Tab 1 Template Account Label
$TAB1TEMPLATE_2 = New-Object System.Windows.Forms.Label
$TAB1TEMPLATE_2.Location = '35,90'
$TAB1TEMPLATE_2.Size = '200,20'
$TAB1TEMPLATE_2.Font = New-Object System.Drawing.Font( "Calibri",10,[System.Drawing.FontStyle]::Bold )
$TAB1TEMPLATE_2.Text = "Who are we using as a template?"
$SDC.Controls.Add( $TAB1TEMPLATE_2 )
#Tab 1 Template Textbox
$TAB1TEMPLATE_3 = New-Object System.Windows.Forms.TextBox
$TAB1TEMPLATE_3.Location = '20,115'
$TAB1TEMPLATE_3.Size = '200,20'
$TAB1TEMPLATE_3.Font = New-Object System.Drawing.Font( "Calibri",9 )
$SDC.Controls.Add( $TAB1TEMPLATE_3 )
#Tab 1 Template Textbox - When hit Return
$TAB1TEMPLATE_3.Add_KeyDown( {
IF ( $_.KeyCode -eq 'Enter' )
{ $SEARCHUSER = $TAB1TEMPLATE_3.Text ; FindUser ; TemplateUser } } )
#Tab 1 Template Account's Full Name
$TAB1TEMPLATE_5 = New-Object System.Windows.Forms.TextBox
$TAB1TEMPLATE_5.Location = '20,150'
$TAB1TEMPLATE_5.Size = '200,20'
$TAB1TEMPLATE_5.ReadOnly = $TRUE
$TAB1TEMPLATE_5.Font = New-Object System.Drawing.Font( "Calibri",9 )
$SDC.Controls.Add( $TAB1TEMPLATE_5 )
#Tab 1 Line Manager Label
$TAB1MANAGER_2 = New-Object System.Windows.Forms.Label
$TAB1MANAGER_2.Location = '35,400'
$TAB1MANAGER_2.Name = "Manager"
$TAB1MANAGER_2.Size = '245,20'
$TAB1MANAGER_2.Font = New-Object System.Drawing.Font( "Calibri",10,[System.Drawing.FontStyle]::Bold )
$TAB1MANAGER_2.Text = "Line Manager"
$SDC.Controls.Add( $TAB1MANAGER_2 )
#Tab 1 Line Manager Textbox
$TAB1MANAGER_3 = New-Object System.Windows.Forms.TextBox
$TAB1MANAGER_3.Location = '20,420'
$TAB1MANAGER_3.Size = '245,20'
$TAB1MANAGER_3.Name = "Manager"
$TAB1MANAGER_3.Font = New-Object System.Drawing.Font( "Calibri",9 )
$SDC.Controls.Add( $TAB1MANAGER_3 )
$TAB1MANAGER_3.Text = $( If ( $TAB1TEMPLATE_3.text -eq $NULL ) { "hi" } )
$SDC.ShowDialog()
I appear to have fixed it by editing the Function TemplateUser:
Function TemplateUser {
IF ( $ACCOUNT -ne $NULL )
{ $TAB1TEMPLATE_5.Text = ( $ACCOUNT.Name )
$TAB1MANAGER_3.Text = ( Get-ADUser $ACCOUNT.Manager ).Name
}
ELSEIF ( $TAB1TEMPLATE_3.Text.Length -lt 4 )
{ $TAB1TEMPLATE_5.Text = $NULL } }
Lord knows what I was doing wrong in the first place as didn't save the changes.
#TheIncorrigible1 - Curious to know what you mean by the formatting. Genuinely. Is there a more efficient way of writing? I'm aware that when complete this script is going to be pretty big so if I'm using unnecessary code I'm all ears.

PowerShell Winforms MoveUp/MoveDown

Have only recently started getting into PowerShell forms and it's proving to be a minefield on support once I hit a wall. There's so much out there for c# but more often than not I struggle to convert it to PowerShell.
The code below is a snippet of a much larger script for Skype stuff (excuse the poor late out, no point adding irreverent code). What I'm struggling to do is create Move Up/Move Down buttons for when my CheckedListBox is populated.
The CheckedListBox is important since everyone within the list will be added to a Skype response group, and everyone checked will be designated an admin of the group. The Move Up/Move Down buttons will be used for priority purposes when this type of routing is selected.
#Function for creating an "open file" button
Function OpenFileDialog
{ [System.Reflection.Assembly]::LoadWithPartialName( "System.Windows.Forms" ) | Out-Null
$OBJFORM = New-Object System.Windows.Forms.OpenFileDialog
$OBJFORM.Filter = "Text files (*.txt)|*.txt"
$TXTIMPORT = $OBJFORM.ShowDialog()
IF ( $TXTIMPORT -eq "OK" )
{ Return $OBJFORM.FileName } }
# ///////////////
#Function for importing users
Function ImportUsers
{ $USERS = gc $TAB2IMPORT
$SIPUSERS = #()
foreach ( $USER in $USERS )
{ $USERSIP = ( "sip:" + $_ )
$SIPUSERS += $USERSIP
IF ( $USERSIP -ne $NULL )
{ [void]$TAB2LISTBOX.Items.Add( $USERSIP ) }
IF ( $SIPUSERS.Count -ge 2 )
{ $TAB2LISTL.Visible = $TRUE } } }
# ///////////////
# Adds .NET assemby's and turns on visual themes in standard PowerShell.
Add-Type -AssemblyName System.Drawing
Add-Type -AssemblyName System.Windows.Forms
[System.Windows.Forms.Application]::EnableVisualStyles()
# ///////////////
# Creates the parent form and controls
$SDC = New-Object System.Windows.Forms.Form
$SDC.Location = '270,175'
$SDC.Size = '900,600'
$SDC.StartPosition = "CenterScreen"
$SDC.BackColor = "Lavender"
$SDC.Font = "Calibri, 8.5"
$SDC.FormBorderStyle = "Fixed3D"
$TABC = New-Object System.Windows.Forms.TabControl
$TABC.Location = '140,20'
$TABC.Size = '720,520'
$TABC.SizeMode = "Fixed"
$SDC.Controls.Add( $TABC )
# ///////////////
#Tab controls
$TAB2 = New-Object System.Windows.Forms.TabPage
$TAB2.Location = '20,40'
$TAB2.Size = '100,100'
$TAB2.Text = "Response Group"
$TAB2.Padding = New-Object System.Windows.Forms.Padding ( 3,3,3,3 )
$TAB2.AutoScroll = $TRUE
$TABC.Controls.Add( $TAB2 )
# ///////////////
#Tab 2 ( Migrate users to Skype )
$TAB2HEADER = New-Object System.Windows.Forms.Label
$TAB2HEADER.Font = New-Object System.Drawing.Font( "Calibri",11,[System.Drawing.FontStyle]::Bold )
$TAB2HEADER.Location = '50,30'
$TAB2HEADER.Size = '620,30'
$TAB2HEADER.Text = "This tab will create a response group."
$TAB2.Controls.Add( $TAB2HEADER )
# ///////////////
#Tab 2 Add Users to Hunt Group
$TABAGENTSL = New-Object System.Windows.Forms.Label
$TABAGENTSL.Font = New-Object System.Drawing.Font( "Calibri",8,[System.Drawing.FontStyle]::Bold )
$TABAGENTSL.Location = '50,420'
$TABAGENTSL.Size = '200,20'
$TABAGENTSL.Text = "Who do you want adding to the group?"
$TAB2.Controls.Add( $TABAGENTSL )
#Tab 2 Open File Button
$TAB2IMPORT = New-Object System.Windows.Forms.Button
$TAB2IMPORT.Location = '50,450'
$TAB2IMPORT.Size = '80,20'
$TAB2IMPORT.Text = "File Import"
$TAB2.Controls.Add( $TAB2IMPORT )
$TAB2IMPORT.Add_Click( { $TAB2IMPORT = OpenFileDialog ; ImportUsers } )
# ///////////////
#Tab 2 ListBox for Users
$TAB2LISTBOX = New-Object System.Windows.Forms.CheckedListBox
$TAB2LISTBOX.Location = '320,120'
$TAB2LISTBOX.Size = '200,200'
$TAB2.Controls.Add( $TAB2LISTBOX )
#Tab 2 Listbox Help
$TAB2LISTL = New-Object System.Windows.Forms.Label
$TAB2LISTL.Font = New-Object System.Drawing.Font( "Calibri",8,[System.Drawing.FontStyle]::Italic )
$TAB2LISTL.Location = '550,120'
$TAB2LISTL.Size = '150,80'
$TAB2LISTL.Text = "All names within the list will be added to the response group. `nThose checked will be designated response group admins"
$TAB2LISTL.Visible = $FALSE
$TAB2.Controls.Add( $TAB2LISTL )
$TAB2LISTMU = New-Object System.Windows.Forms.Button
$TAB2LISTMU.Location = '530,260'
$TAB2LISTMU.Text = "&Move Up"
$TAB2.Controls.Add( $TAB2LISTMU )
$SDC.Add_Shown( { $SDC.Activate() } )
$SDC.ShowDialog()
I bought PowerShell Pro Tools for Visual Studio but hit the same stumbling blocks in that if I don't know what tool it is I need to begin with, I can't figure out from the options available on screen. As a result I've resided myself to ISE until I die!
Any help would be greatly appreciated. Thanks
I found a link on how to do it.
https://www.akaplan.com/blog/2016/05/move-selected-item-up-or-down-in-powershell-listbox/
*edit
I 'cleaned' up the code for my own use as shown below:
#Function for Move Down
Function MoveDown {
IF ( ( $TAB2LISTBOX.SelectedIndex -ne -1 ) -and ( $TAB2LISTBOX.SelectedIndex -lt $TAB2LISTBOX.Items.Count - 1 ) )
{ $TAB2LISTBOX.BeginUpdate()
$SELECTITEM = $TAB2LISTBOX.SelectedIndex
$TAB2LISTBOX.Items.Insert( $SELECTITEM, $TAB2LISTBOX.Items.Item( $SELECTITEM +1 ) )
$TAB2LISTBOX.Items.RemoveAt( $SELECTITEM +2 )
$TAB2LISTBOX.SelectedIndex = ( $SELECTITEM +1 )
$TAB2LISTBOX.EndUpdate() } }
#Function for Move Up
Function MoveUp {
IF ( $TAB2LISTBOX.SelectedIndex -gt 0 )
{ $TAB2LISTBOX.BeginUpdate()
$SELECTITEM = $TAB2LISTBOX.selectedIndex
$TAB2LISTBOX.Items.Insert( $SELECTITEM -1, $TAB2LISTBOX.Items.Item( $SELECTITEM ) )
$TAB2LISTBOX.SelectedIndex = ( $SELECTITEM -1 )
$TAB2LISTBOX.Items.RemoveAt( $SELECTITEM +1 )
$TAB2LISTBOX.EndUpdate() } }

Resources