Drag and drop outlook selected attachtment with powershell - powershell

I've managed to get all attachment from a mail and save it to a location.
Now i am trying to to drag and drop it to a listbox.
Now it gets all the attachements, but i want only the selecte on in the outlook message.
$outlook = New-Object -comobject outlook.application
$sel=$outlook.ActiveExplorer().selection
foreach ($s in $sel) {
foreach ($at in $s.Attachments)
{if ($at.FileName -match 'SCAN_*')
{$newname=[io.path]::ChangeExtension($Filesgo_listbox.SelectedItem,"PDF")
if ($newname -NOTmatch "20*") {$newname= $DATUM + " " + $newname}
$name= join-path $curfil $newname }
else {$name=join-path $curfil $at.FileName}

Continuing from my comment.
'PowerShell Drag & Drop files' winform
# Example 1
Function DragDropSample()
{
[System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
$form = New-Object Windows.Forms.Form
$form.text = "Drag&Drop sample"
$listBox = New-Object Windows.Forms.ListBox
$listBox.Dock = [System.Windows.Forms.DockStyle]::Fill
$handler = {
if ($_.Data.GetDataPresent([Windows.Forms.DataFormats]::FileDrop)) {
foreach ($filename in $_.Data.GetData([Windows.Forms.DataFormats]::FileDrop)) {
$listBox.Items.Add($filename)
}
}
}
$form.AllowDrop = $true
$form.Add_DragEnter($handler)
$form.Controls.Add($listBox)
$form.ShowDialog()
}
DragDropSample | Out-Null
Similar Q&A right here on SP:
PowerShell - DataGridView Windows Form Drag and Drop Issue

Related

Powershell: Output a search from a csv to a small gui

maybe one of you experts can help a complete newbie (I don't know if what I want is even feasible).
Let's assume I have a CSV file with various data. (see csv_screenshot)csv_screenshot
I import this data via Powershell into a small GUI . How can I make it so that when I search for "Paris", I really only get the output for Paris in the GUI as a list view like this (see powershell_screenshot)
powershell_screenshot
Currently the output in the GUI looks like this (see current_result.png). How do I get it nicely formatted as a list in there. I really want to insert it like this (via Out Grid View it is no problem)
current_result.png
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
[void] [System.Windows.Forms.Application]::EnableVisualStyles();
function search_csv {
$Input = $textbox_Search.text
$Input = "*$Input*"
$Input_Result = import-csv -path C:\Users\check.csv -Header "Location", "Client", "Mobile Device"
$output_TextBox.text = $Input_Result -like $Input
}
$search_csvtool = New-Object System.Windows.Forms.Form
$search_csvtool.Text = "CSV Search"
$search_csvtool.Size = New-Object System.Drawing.Size(674,500)
$search_csvtool.FormBorderStyle ="FixedDialog"
$search_csvtool.TopMost = $true
$search_csvtool.MaximizeBox = $false
$search_csvtool.MinimizeBox = $true
$search_csvtool.ControlBox = $true
$search_csvtool.StartPosition = "CenterScreen"
$search_csvtool.Font = "Courier New"
$label_Search = New-Object System.Windows.Forms.Label
$label_Search.Location = New-Object System.Drawing.Size(195,18)
$label_Search.Size = New-Object System.Drawing.Size(265,32)
$label_Search.TextAlign ="MiddleCenter"
$label_Search.Text = "Please enter "
$search_csvtool.Controls.Add($label_Search)
$textbox_Search = New-Object System.Windows.Forms.TextBox
$textbox_Search.Location = New-Object System.Drawing.Size(195,50)
$textbox_Search.Size = New-Object System.Drawing.Size(266,37)
$search_csvtool.Controls.Add($textbox_Search)
$button_Search = New-Object System.Windows.Forms.Button
$button_Search.Location = New-Object System.Drawing.Size(195,80)
$button_Search.Size = New-Object System.Drawing.Size(266,24)
$button_Search.TextAlign = "MiddleCenter"
$button_Search.Text = "Search"
$button_Search.Add_Click({search_csv})
$search_csvtool.Controls.Add($button_Search)
$output_TextBox = New-Object System.Windows.Forms.TextBox
$output_TextBox.Multiline = $true;
$output_TextBox.Location = New-Object System.Drawing.Size(16,130)
$output_TextBox.Size = New-Object System.Drawing.Size(627,314)
$output_TextBox.ScrollBars = "Vertical"
$output_TextBox.ReadOnly = $true;
$search_csvtool.Controls.Add($output_TextBox)
$search_csvtool.Add_Shown({$search_csvtool.Activate()})
[void] $search_csvtool.ShowDialog()
Ok, so here's what I meant in my comment:
Start the code with
Add-Type -AssemblyName System.Windows.Forms
[System.Windows.Forms.Application]::EnableVisualStyles()
$csvData = Import-Csv -path 'C:\Users\check.csv' -Header "Location", "Client", "Mobile Device"
function search_csv {
$searchThis = $textbox_Search.Text.Trim()
# use $script: scoping here to reference the $csvData variable
$data = $script:csvData | Where-Object {$_.Location -like "*$searchThis*"}
if ($data) {
$output_TextBox.Text = ($data | Format-List | Out-String).Trim()
}
else {
$output_TextBox.Text = "Not found.."
}
}
Then create the rest of the form as you did.
Important: Destroy the form when done with a last new code line:
$search_csvtool.Dispose()
You should then have this result:
As per your comment to empty the textbox when nothing (or just whitespace) has been entered, you could change the function to:
function search_csv {
$searchThis = $textbox_Search.Text.Trim()
# use $script: scoping here to reference the $csvData variable
$data = $script:csvData | Where-Object {$_.Location -like "*$searchThis*"}
if ([string]::IsNullOrWhiteSpace($searchThis)) {
$output_TextBox.Clear()
}
elseif ($data) {
$output_TextBox.Text = ($data | Format-List | Out-String).Trim()
}
else {
$output_TextBox.Text = "Not found.."
}
}
However, as postanote already commented, it would be much better to simply disable the search button and only enable it when something other that whitespace has been entered.
To do that, you need to add an eventhandler to the textbox:
$textbox_Search = New-Object System.Windows.Forms.TextBox
$textbox_Search.Location = New-Object System.Drawing.Size(195,50)
$textbox_Search.Size = New-Object System.Drawing.Size(266,37)
$textbox_Search.Add_TextChanged({
# enable the button when there is at least one non-whitespace character present
$button_Search.Enabled = $this.Text -match '\S'
# or use
# $button_Search.Enabled = (-not [string]::IsNullOrWhiteSpace($this.Text))
})
$search_csvtool.Controls.Add($textbox_Search)
And initialize the button to be disabled at startup:
$button_Search = New-Object System.Windows.Forms.Button
$button_Search.Location = New-Object System.Drawing.Size(195,80)
$button_Search.Size = New-Object System.Drawing.Size(266,24)
$button_Search.TextAlign = "MiddleCenter"
$button_Search.Text = "Search"
# initialize to Disabled; will be enabled as soon as there is text entered in the $textbox_Search
$button_Search.Enabled = $false
$button_Search.Add_Click({search_csv})
$search_csvtool.Controls.Add($button_Search)
Inside an event handler, you can refer to the object itself using automatic variable $this
You should be able to go thru your string output line by line and change this.
That stated, you probably won't like how it looks. Normalized colons will probably look better.
This code should do the trick. I'll let you be the judge of if it looks "better"
$dataLines = ($data | Format-List | Out-String).Trim() -split '(?>\r\n|\n)'
$dataText = #(
foreach ($dataLine in $dataLines) {
$dataLine -replace '\s{1,}\:', ':'
}
) -join [Environment]::Newline
$output_TextBox.Text = $dataText

PowerShell Add_Click in foreach loop

What I am trying to accomplish is to create buttons that launch exe files in a certain directory when clicked, but when I try using a foreach loop to create a few buttons, all of the buttons just launch the file the last button is supposed to launch.
Add-Type -AssemblyName System.Windows.Forms
Add-Type -AssemblyName System.Drawing
$form = New-Object System.Windows.Forms.Form
$form.Text = 'Main Window'
$form.Size = New-Object System.Drawing.Size(600,400)
$flp = New-Object System.Windows.Forms.FlowLayoutPanel
$flp.Location = New-Object System.Drawing.Point(0,0)
$flp.Height = $form.Height
$flp.Width = $form.Width
$form.Controls.Add($flp)
$files = Get-ChildItem "$home\Downloads" -Include *.exe -Name
foreach ($file in $files){
$button = New-Object System.Windows.Forms.Button
$flp.Controls.Add($button)
$button.Width = 100
$button.Height = 50
$button.Text = $file
$button.Add_Click{
Start-Process -FilePath "$home\Downloads\$file"
}
}
$form.Topmost = $true
$form.ShowDialog()
Whatever I'm doing is probably pretty stupid, so I was just looking for any alternatives or solutions to this other than to just hard code everything.
It is likely that you need to use .GetNewClosure() ScriptBlock method so that each script block (button click event) holds the current value of the $file variable at the moment of enumeration.
Example of what this means:
$blocks = foreach($i in 0..5) {
{ "hello $i" }
}
& $blocks[0] # => hello 5
& $blocks[1] # => hello 5
$blocks = foreach($i in 0..5) {
{ "hello $i" }.GetNewClosure()
}
& $blocks[0] # => hello 0
& $blocks[1] # => hello 1
In that sense, and assuming this is the issue, the following should work:
foreach ($file in $files) {
$button = New-Object System.Windows.Forms.Button
$button.Width = 100
$button.Height = 50
$button.Text = $file
$thisEvent = {
Start-Process -FilePath "$home\Downloads\$file"
}.GetNewClosure()
$button.Add_Click($thisEvent)
$flp.Controls.Add($button)
}
A nice alternative to having a need to use .GetNewClosure() can be seen on this answer. The .Tag property of the Button can be used to store the information of the file's path which then can be used on the button's .Click event:
foreach ($file in $files) {
$button = New-Object System.Windows.Forms.Button
$button.Width = 100
$button.Height = 50
$button.Text = $file
# Store the file's path in the Tag's property of this Button
$button.Tag = "$home\Downloads\$file"
$button.Add_Click({
Start-Process -FilePath $this.Tag
})
$flp.Controls.Add($button)
}

Don’t display Cancel in PowerShell script result

I have the following PowerShell script which displays file dialog to select a txt file. If user cancels dialog then provide a multiline text box
function GetDetails() {
Add-Type -AssemblyName System.Windows.Forms;
$browser = New-Object System.Windows.Forms.OpenFileDialog;
$browser.Filter = "txt (*.txt)|*.txt";
$browser.InitialDirectory = "E:\";
$browser.Title = "select txt file";
$browserResult = $browser.ShowDialog();
if($browserResult -eq [System.Windows.Forms.DialogResult]::OK) {
$nfoFile = $browser.FileName;
if([string]::IsNullOrWhiteSpace($txtFile)) {
return GetFromForm;
}
$txtFile = [System.IO.Path]::ChangeExtension($nfoFile, ".dac");
$txtFile = $temp + [System.IO.Path]::GetFileName($txtFile);
$exeArgs = "-f -S `"$txtFile`" -O `"$txtFile`"";
Start-Process $anExe -ArgumentList $exeArgs -Wait;
$result = Get-Content $txtFile | Out-String;
$browser.Dispose();
return $result;
} else {
return GetFromForm;
}
}
function GetFromForm(){
Add-Type -AssemblyName System.Windows.Forms;
$form = New-Object System.Windows.Forms.Form;
$form.Width = 800;
$form.Height = 600;
$txtBox = New-Object System.Windows.Forms.TextBox;
$txtBox.Multiline = $true;
$txtBox.AcceptsReturn = $true;
$txtBox.AcceptsTab = $true;
$txtBox.Visible = $true;
$txtBox.Name = "txtName";
$txtBox.Width = 760;
$txtBox.Height = 660;
$form.Controls.Add($txtBox);
$form.ShowDialog();
 
$form.Dispose();
return $txtBox.Text;
}
$desc = GetDetails;
cls;
Write-Host $desc;
Here I have two issues:
In Write-Host $desc, prints also Cancel hereiswhateverstrimg string if user chose to cancel dialog. How to avoid that?
If I run script in ISE, the generated form (in second function) will be always behind ISE even I call ShowDialog(), I expected to behave as modal dialog. It’s normal or there is a fix for this ?
Ok, there are a few changes that I made for efficiency and a few for functionality. Read the comments in the script for the explanations.
# Just add types once. There is no need to add the types in each function.
Add-Type -AssemblyName System.Windows.Forms;
Add-Type -AssemblyName System.Drawing
function GetDetails() {
$browser = New-Object System.Windows.Forms.OpenFileDialog;
$browser.Filter = "txt (*.txt)|*.txt";
$browser.InitialDirectory = "E:\";
$browser.Title = "select txt file";
$browserResult = $browser.ShowDialog();
# Combined the if statements
if($browserResult -eq [System.Windows.Forms.DialogResult]::OK -and [string]::IsNullOrWhiteSpace($txtFile) -ne $true) {
$nfoFile = $browser.FileName;
$txtFile = [System.IO.Path]::ChangeExtension($nfoFile, ".dac");
$txtFile = $temp + [System.IO.Path]::GetFileName($txtFile);
$exeArgs = "-f -S `"$txtFile`" -O `"$txtFile`"";
Start-Process $anExe -ArgumentList $exeArgs -Wait;
# The Raw flag should return a string
$result = Get-Content $txtFile -Raw;
$browser.Dispose();
return $result;
}
# No need for else since the if statement returns
return GetFromForm;
}
function GetFromForm(){
$form = New-Object System.Windows.Forms.Form;
$form.Text = 'Adding Arguments'
$form.Size = New-Object System.Drawing.Size(816,600)
$form.StartPosition = 'CenterScreen'
# Added a button
$OKButton = New-Object System.Windows.Forms.Button
$OKButton.Location = New-Object System.Drawing.Point(585,523)
$OKButton.Size = New-Object System.Drawing.Size(75,23)
$OKButton.Text = 'OK'
$OKButton.DialogResult = [System.Windows.Forms.DialogResult]::OK
$form.AcceptButton = $OKButton
$form.Controls.Add($OKButton)
$txtBox = New-Object System.Windows.Forms.TextBox;
$txtBox.Multiline = $true;
$txtBox.AcceptsReturn = $true;
$txtBox.AcceptsTab = $true;
$txtBox.Visible = $true;
$txtBox.Name = "txtName";
$txtBox.Size = New-Object System.Drawing.Size(660,500)
$form.Controls.Add($txtBox);
# Needed to force it to show on top
$form.TopMost = $true
# Select the textbox and activate the form to make it show with focus
$form.Add_Shown({$txtBox.Select(), $form.Activate()})
# Finally show the form and assign the ShowDialog method to a variable (this keeps it from printing out Cancel)
$result = $form.ShowDialog();
# If the user hit the OK button return the text in the textbox
if ($result -eq [System.Windows.Forms.DialogResult]::OK)
{
return $txtBox.Text
}
}
$desc = GetDetails;
cls;
Write-Host $desc;
You can see reference material here: https://learn.microsoft.com/en-us/powershell/scripting/samples/creating-a-custom-input-box?view=powershell-6
You need to suppress output of $form.ShowDialog() in GetFromForm:
$form.ShowDialog()|out-null
Powershell will add to a return value everything that was outputted to host within a function/commandlet.
Regarding your second issue - see this answer
And please do not use semi-colon at an end of line. This is not C# and will confuse you into thinking that the line is ended here but it's not quite true.

powershell gui select list highlight

I hope there i can get an answer for this quick. Im populating a list box based on radio button being selected. I want the list box to highlight the row automatically so that when i click the button the relevant action can happen based on the selection. In this case it will be a sql server instance. See extract of code for adding it to ListBox1.
ForEach ($Server in $Servers)
{
#$NL = "`r`n"
[void] $ListBox1.Items.Add($Server)
#$ListBox1.Items.selectedItem
}
Example:
$form = New-Object System.Windows.Forms.Form
$listbox = New-Object System.Windows.Forms.ListBox
$listbox.SelectionMode = "MultiSimple"
$listbox.Items.Add("item1") | Out-Null
$listbox.Items.Add("item2") | Out-Null
$listbox.Items.Add("item3") | Out-Null
$listbox.Items.Add("item4") | Out-Null
for($i = 0; $i -lt $listbox.Items.Count; $i++) {
$listbox.SetSelected($i, $true)
}
$form.Controls.Add($listbox)
$form.ShowDialog()

Powershell Checkedlistbox handling - checked item count inconsistency and missed click events

I'm having trouble with multiple issues with a checkedlistbox. Its content is the Windows feature name (commandline parameter to install a Windows feature via Powershell) and a description which really is its more readable name. Because I develop on Windows 7 and this command is only available on a Server platform I read the data from a XML file source. The XML file was created by the output of function BuildFeaturesFile, below. I've pasted a sample at the bottom if that is a problem.
1) I have to click an item twice to check it.
2) I have to double click an item slowly and surely to avoid the program missing the second click. Would I need to change the Windows control panel config to affect this or if I wanted (not too bothered but more curious) could I reduce the polling time to slicken the interface response?
3) This is my main issue. If I use the mouse to select an item it normally updates the count correctly however when I use the keyboard despite calling the same function I get different results; the first check is seemingly missed (doubled by the look of it reading further online to undo its action - however the event "only" fires once which causes misalignment of the displayed count to the true count. I think it maybe linked to the remark in the ItemCheck event documentation "The check state is not updated until after the ItemCheck event occurs."
http://msdn.microsoft.com/en-us/library/system.windows.forms.checkedlistbox.itemcheck(v=vs.110).aspx
The main code also updates a description text box but I removed that for brevity.
4) In function ftn_CheckAllItemsInCheckList the top remmed out line works for the first item but as I'm calling it by index seemingly the action of checking the item causes the index to change which unseats the action causing it to break.
Please can you assist - at least with questions #3 and #4 if possible?
Thanks in advance.
Shaun
function BuildForm
{
# Declare objects
$frm_BuildConfigurator = New-Object System.Windows.Forms.Form
$btn_Cancel = New-Object System.Windows.Forms.Button
$gb_CC_Features = New-Object Windows.Forms.GroupBox
$clb_CC_Features = New-Object System.Windows.Forms.CheckedListBox
$btn_CC_Features_UncheckList = New-Object System.Windows.Forms.Button
$btn_CC_Features_SelectAll = New-Object System.Windows.Forms.Button
$gb_CCF_Description = New-Object Windows.Forms.GroupBox
$tb_CCF_Description = New-Object System.Windows.Forms.TextBox
#Build the form
$frm_BuildConfigurator.Text = "Build Configurator"
$frm_BuildConfigurator.StartPosition = "CenterScreen"
$frm_BuildConfigurator.Width = 380
$frm_BuildConfigurator.Height = 200
$frm_BuildConfigurator.FormBorderStyle = "FixedSingle"
$frm_BuildConfigurator.ControlBox = $false
$frm_BuildConfigurator.Controls.Add($btn_Cancel)
#Set default button behaviour
$frm_BuildConfigurator.KeyPreview = $True
$frm_BuildConfigurator.Add_KeyDown({if ($_.KeyCode -eq "Enter") {$frm_BuildConfigurator.Close()}})
$frm_BuildConfigurator.Add_KeyDown({if ($_.KeyCode -eq "Escape") {$frm_BuildConfigurator.Close()}})
# Create the Cancel button
$btn_Cancel.Location = New-Object System.Drawing.Size(50,145)
$btn_Cancel.Size = New-Object System.Drawing.Size(55,23)
$btn_Cancel.Text = "Cancel"
$btn_Cancel.Add_Click({$frm_BuildConfigurator.Close()})
# Create the Features form elements
$frm_BuildConfigurator.Controls.Add($gb_CC_Features)
$frm_BuildConfigurator.Controls.Add($gb_CCF_Description)
# Create the Features group box
$gb_CC_Features.DataBindings.DefaultDataSourceUpdateMode = [System.Windows.Forms.DataSourceUpdateMode]::OnValidation
$gb_CC_Features.Location = New-Object System.Drawing.Point(10,6)
$gb_CC_Features.Name = "gb_CC_Features"
$gb_CC_Features.Size = New-Object System.Drawing.Size(350,132)
$gb_CC_Features.Text = "Features (0 selected)"
$gb_CC_Features.Controls.Add($clb_CC_Features)
$gb_CC_Features.Controls.Add($btn_CC_Features_SelectAll)
$gb_CC_Features.Controls.Add($btn_CC_Features_UncheckList)
$clb_CC_Features.Location = New-Object Drawing.Point 11,16
$clb_CC_Features.Size = New-Object System.Drawing.Size(220,110)
$clb_CC_Features.Add_ItemCheck({ftnUpdateFeatureSelectionCount})
$clb_CC_Features.Add_SelectedIndexChanged({ftnUpdateFeatureSelectionCount})
$clb_CC_Features.Add_Click({ftnUpdateFeatureSelectionCount})
# Populate the Features checked list box
ForEach ($FeatureItem in $script:xmlFeatures.FeatureList.Feature | Select-Object -Property Name) {
$clb_CC_Features.Items.Add($FeatureItem.Name) | Out-Null
}
# Create the Check All button
$btn_CC_Features_SelectAll.Location = New-Object System.Drawing.Point(250,20)
$btn_CC_Features_SelectAll.Size = New-Object System.Drawing.Size(80,23)
$btn_CC_Features_SelectAll.Text = "Check All"
$btn_CC_Features_SelectAll.Add_Click({ftn_CheckAllItemsInCheckList $clb_CC_Features})
$btn_CC_Features_SelectAll.TabIndex = 2
# Create the Uncheck All button
$btn_CC_Features_UncheckList.Location = New-Object System.Drawing.Point(250,50)
$btn_CC_Features_UncheckList.Size = New-Object System.Drawing.Size(80,23)
$btn_CC_Features_UncheckList.Text = "Uncheck All"
$btn_CC_Features_UncheckList.Add_Click({ftn_UncheckList $clb_CC_Features})
$btn_CC_Features_UncheckList.TabIndex = 2
ftnUpdateFeatureSelectionCount
#Show the Form
$frm_BuildConfigurator.ShowDialog() | Out-Null
}
function ftnUpdateFeatureDescription()
{
$tb_CCF_Description.Text = $script:FeatureDisplayNames[$clb_CC_Features.SelectedIndex]
}
function ftnUpdateFeatureSelectionCount ()
{
$gb_CC_Features.Text = "Features (" + $clb_CC_Features.CheckedItems.Count + " selected)"
}
function BuildFeaturesFile ()
{
# Only runs on Windows Server
$Features = Get-WindowsFeature
$xml = "<xml>"
$NewFeaturesFilePath = $ScriptDir + "\FeaturesNew.xml"
ForEach($Feature in $Features)
{
$xml += "<Feature Name='" + $Feature.Name + "' DisplayName='" + $Feature.DisplayName + "'>"
$xml += "</Feature>"
}
$xml += "</xml>"
$xml | Out-File -FilePath $NewFeaturesFilePath
}
function ReadFeaturesFile ()
{
[array] $script:FeatureNames = $null
[array] $script:FeatureDisplayNames = $null
$FileExists = Test-Path $FeaturesFilePath
if ($FileExists -eq $true) {
$script:xmlFeatures.Load($FeaturesFilePath)
$xml_Features = $script:xmlFeatures.SelectNodes("/FeatureList/Feature")
ForEach ($Feature in $xml_Features) {
[array] $script:FeatureNames += $Feature.Name
[array] $script:FeatureDisplayNames += $Feature.DisplayName
}
}
}
function ftn_UncheckList( $checkedListBoxObject )
{
ForEach($i in $checkedListBoxObject.CheckedIndices) { $checkedListBoxObject.SetItemCheckState($i, 'Unchecked') | Out-Null }
}
function ftn_CheckAllItemsInCheckList( $checkedListBoxObject )
{
#ForEach($i in $checkedListBoxObject.Items) { $checkedListBoxObject.SetItemChecked($checkedListBoxObject.Items.IndexOf($i), $true) }
For($index =0; $index -lt $checkedListBoxItem.Items.Count; $index++){ if($checkedListBoxItem.GetItemChecked($index)) { $checkedListBoxItem.SetItemChecked($i, $true); }}
}
Set-ExecutionPolicy -ExecutionPolicy Unrestricted -Force
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Drawing")
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
$script:xmlFeatures = $null; $script:xmlFeatures = New-Object -TypeName XML
$ScriptPath = $MyInvocation.MyCommand.Path
$ScriptDir = Split-Path -parent $ScriptPath
$FeaturesFilePath = $ScriptDir + "\Features.xml"
$BuildScriptStr = $MyInvocation.MyCommand.Definition #Full path - for script name only use $MyInvocation.MyCommand.Name
$noSelectedFeatures = 0
ReadFeaturesFile
BuildForm
# End of script
File: Features.xml
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<FeatureList>
<Feature Name="AD-Certificate" DisplayName="Active Directory Certificate Services"/>
<Feature Name="ADCS-Cert-Authority" DisplayName="Certification Authority"/>
<Feature Name="ADCS-Web-Enrollment" DisplayName="Certification Authority Web Enrollment"/>
</FeatureList>
There are a lot of questions :
1)-2) If you want to click once to check a box you have to set the CheckOnClick property on the CheckedListBox. In you case a line can be checked only once it's selected.
$clb_CC_Features.Size = New-Object System.Drawing.Size(220,110)
$clb_CC_Features.CheckOnClick = $true
$clb_CC_Features.Add_ItemCheck({})
3) The message ItemCheck append before the line is really checked so in the function you call on this even you have look if the line is going to be cheched or unchecked.
I change
$clb_CC_Features.Add_ItemCheck({ftnChecked})
#$clb_CC_Features.Add_SelectedIndexChanged({ftnUpdateFeatureSelectionCount})
#$clb_CC_Features.Add_Click({})
...
$_ represent the value of the event.
function ftnChecked ()
{
if ($_.NewValue -eq 'checked')
{
$gb_CC_Features.Text = "Features (" + $($clb_CC_Features.CheckedItems.Count + 1) + " selected)"
}
else
{
$gb_CC_Features.Text = "Features (" + $($clb_CC_Features.CheckedItems.Count -1) + " selected)"
}
}
4) You can try the following :
function ftn_CheckAllItemsInCheckList #( $checkedListBoxObject )
{
For($index =0; $index -lt $clb_CC_Features.Items.Count; $index++){ $clb_CC_Features.SetItemChecked($index, $true)}
}