I am creating an admin tool for our helpdesk and I want the option to allow the tool to stay on top of other windows via a checkbox. This is the inital setup of the checkbox:
$cbx_OnTop = New-Object system.Windows.Forms.CheckBox
$cbx_OnTop.text = "Keep On Top"
$cbx_OnTop.AutoSize = $false
$cbx_OnTop.width = 175
$cbx_OnTop.height = 20
$cbx_OnTop.location = New-Object System.Drawing.Point(24,290)
$cbx_OnTop.Font = 'Microsoft Sans Serif,8'
$cbx_OnTop.Checked = $false
I then have the following function:
function KeepOnTop {
if ($cbx_OnTop.Checked = $True) {
$AdminTool.TopMost = $True
} else {
$AdminTool.TopMost = $false
}
}
How do I add the function to this checkbox?
The Add_CheckStateChanged event is the command you are looking for.
$cbx_OnTop.Add_CheckStateChanged({
If ($cbx_OnTop.Checked) {
$AdminTool.TopMost = $True
} Else {
$AdminTool.TopMost = $false
}
)}
Related
I'm trying to implement a PowerShell form whereby there is a faded overlay over the background desktop.
The only way I could think implementing this was to use two forms - one to show a full screen overlay (background form), and the other being the actual (main) form. However, when i click off (or deactivate) the main form, I want everything to close. However it no longer closes and I can't figure it why? I get the feeling that when I click off the main form, the click event is trying to click the background form (and can't due to the main form having focus) and the 'deactivate' event no longer works?
Any help appreciated:
[void][System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
[void][System.Reflection.Assembly]::LoadWithPartialName("System.Drawing")
[System.Windows.Forms.Application]::EnableVisualStyles()
$windowsGreyTrans = [System.Drawing.Color]::FromArgb(20,43,43,43)
$windowsGreyOff = [System.Drawing.Color]::FromArgb(43,43,43)
$windowsGreyOn = [System.Drawing.Color]::FromArgb(60,60,60)
$bgform = New-Object System.Windows.Forms.Form
$bgform.WindowState = 'Maximized'
$bgform.ShowInTaskbar = $false
$bgform.MaximizeBox = $false
$bgform.Opacity = 0.5
$bgform.ControlBox = $false
$bgform.FormBorderStyle = [System.Windows.Forms.FormBorderStyle]::none
[void]$bgform.show()
$form = New-Object System.Windows.Forms.Form
$form.Size = New-Object System.Drawing.Size(200, 40)
$Form.StartPosition = "CenterScreen"
$form.ShowInTaskbar = $false
$form.Name ="Power"
$form.MaximizeBox = $false
$form.ControlBox = $false
$form.FormBorderStyle = [System.Windows.Forms.FormBorderStyle]::none
$shutdownBtn = New-Object System.Windows.Forms.Button
$shutdownBtn.Location = New-Object System.Drawing.Point(0, 0)
$shutdownBtn.Size = New-Object System.Drawing.Size(200, 40)
$shutdownBtn.Text = "Shut down"
$shutdownBtn.FlatStyle = "Flat"
$shutdownBtn.UseVisualStyleBackColor = $True
$shutdownBtn.BackColor = $windowsGreyOff
$shutdownBtn.FlatAppearance.BorderSize = 0
$shutdownBtn.ForeColor = "White"
$shutdownBtn.Font = "Segoe UI,10pt"
#$shutdownBtn.Cursor = [System.Windows.Forms.Cursors]::Hand
$shutdownBtn.Add_Click({
try {
}
catch{
}
})
$shutdownBtn.Add_MouseEnter({
$this.BackColor = $windowsGreyOn
})
$shutdownBtn.Add_MouseLeave({
$this.BackColor = $windowsGreyOff
})
$form.Controls.Add($shutdownBtn)
$form.Add_Deactivate({
$this.Close()
$bgform.close()
})
$form.Add_Shown({
$this.Activate()
})
[void]$form.showdialog()
I propose you a better solution, use a tablelayoutpanel for center your button and intercept click event.
Try this :
[void][System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
[void][System.Reflection.Assembly]::LoadWithPartialName("System.Drawing")
[System.Windows.Forms.Application]::EnableVisualStyles()
$windowsGreyTrans = [System.Drawing.Color]::FromArgb(20,43,43,43)
$windowsGreyOff = [System.Drawing.Color]::FromArgb(43,43,43)
$windowsGreyOn = [System.Drawing.Color]::FromArgb(60,60,60)
#create form
$form = New-Object System.Windows.Forms.Form
$form.SuspendLayout()
$form.WindowState = [System.Windows.Forms.FormWindowState]::Maximized
$form.ShowInTaskbar = $false
$form.MaximizeBox = $false
$form.Opacity = 0.5
$form.ControlBox = $false
$form.FormBorderStyle = [System.Windows.Forms.FormBorderStyle]::none
#create Tablelayout panel on this form
$TableLayoutPanel = New-Object System.Windows.Forms.TableLayoutPanel
$TableLayoutPanel.SuspendLayout()
$TableLayoutPanel.ColumnCount=1
$TableLayoutPanel.RowCount=1
$TableLayoutPanel.Dock=[System.Windows.Forms.DockStyle]::Fill
$form.Controls.Add($TableLayoutPanel)
#create button on this tablelayoutpanel
$shutdownBtn = New-Object System.Windows.Forms.Button
$shutdownBtn.Size = New-Object System.Drawing.Size(200, 40)
$shutdownBtn.Text = "Shut down"
$shutdownBtn.FlatStyle = "Flat"
$shutdownBtn.UseVisualStyleBackColor = $True
$shutdownBtn.BackColor = $windowsGreyOff
$shutdownBtn.FlatAppearance.BorderSize = 0
$shutdownBtn.ForeColor =[System.Drawing.Color]::White
$shutdownBtn.Font = "Segoe UI,10pt"
$shutdownBtn.Anchor=[System.Windows.Forms.AnchorStyles]::None
$TableLayoutPanel.Controls.Add($shutdownBtn)
#Layout
$TableLayoutPanel.ResumeLayout($false)
$form.ResumeLayout($false)
$shutdownBtn.Add_Click({
try {
}
catch{
}
})
#change color to tablelayot on first show
$form.Add_Shown({
$TableLayoutPanel.BackColor =$windowsGreyTrans
})
$TableLayoutPanel.Add_Click({
$form.close()
})
$shutdownBtn.Add_MouseEnter({
$this.BackColor = $windowsGreyOn
})
$shutdownBtn.Add_MouseLeave({
$this.BackColor = $windowsGreyOff
})
[void]$form.ShowDialog()
$form.Dispose()
Should be pretty straight forward but cant work out the logic. Below is the code I currently have which sort of works but the only problem is if more than one checkboxes are checked and you uncheck one the button disables, I need the button to remain enabled as long as there is one or more checkbox checked. I've also tried various convoluted If and Elseif statements but nothing I've tried changes this behaviour.
Add-Type -AssemblyName System.Windows.Forms
[System.Windows.Forms.Application]::EnableVisualStyles()
$Form = New-Object system.Windows.Forms.Form
$Form.ClientSize = '170,191'
$Form.text = "Scheduler"
$Form.TopMost = $false
$MonCheckBox = New-Object system.Windows.Forms.CheckBox
$MonCheckBox.text = "Monday"
$MonCheckBox.AutoSize = $false
$MonCheckBox.width = 95
$MonCheckBox.height = 20
$MonCheckBox.location = New-Object System.Drawing.Point(30,30)
$MonCheckBox.Font = 'Microsoft Sans Serif,10'
$TueCheckBox = New-Object system.Windows.Forms.CheckBox
$TueCheckBox.text = "Tuesday"
$TueCheckBox.AutoSize = $false
$TueCheckBox.width = 95
$TueCheckBox.height = 20
$TueCheckBox.location = New-Object System.Drawing.Point(30,50)
$TueCheckBox.Font = 'Microsoft Sans Serif,10'
$WedCheckBox = New-Object system.Windows.Forms.CheckBox
$WedCheckBox.text = "Wednesday"
$WedCheckBox.AutoSize = $false
$WedCheckBox.width = 95
$WedCheckBox.height = 20
$WedCheckBox.location = New-Object System.Drawing.Point(30,70)
$WedCheckBox.Font = 'Microsoft Sans Serif,10'
$ThuCheckBox = New-Object system.Windows.Forms.CheckBox
$ThuCheckBox.text = "Thursday"
$ThuCheckBox.AutoSize = $false
$ThuCheckBox.width = 95
$ThuCheckBox.height = 20
$ThuCheckBox.location = New-Object System.Drawing.Point(30,90)
$ThuCheckBox.Font = 'Microsoft Sans Serif,10'
$FriCheckBox = New-Object system.Windows.Forms.CheckBox
$FriCheckBox.text = "Friday"
$FriCheckBox.AutoSize = $false
$FriCheckBox.width = 95
$FriCheckBox.height = 20
$FriCheckBox.location = New-Object System.Drawing.Point(30,110)
$FriCheckBox.Font = 'Microsoft Sans Serif,10'
$SchedButton = New-Object system.Windows.Forms.Button
$SchedButton.text = "Schedule"
$SchedButton.width = 60
$SchedButton.height = 30
$SchedButton.location = New-Object System.Drawing.Point(30,134)
$SchedButton.Font = 'Microsoft Sans Serif,10'
$SchedButton.Autosize = $true
$SchedButton.Enabled = $false
$Form.controls.AddRange(#($MonCheckBox,$TueCheckBox,$WedCheckBox,$ThuCheckBox,$FriCheckBox,$SchedButton))
$MonCheckBox.add_CheckedChanged({$SchedButton.Enabled = $MonCheckBox.Checked})
$TueCheckBox.add_CheckedChanged({$SchedButton.Enabled = $TueCheckBox.Checked})
$WedCheckBox.add_CheckedChanged({$SchedButton.Enabled = $WedCheckBox.Checked})
$ThuCheckBox.add_CheckedChanged({$SchedButton.Enabled = $ThuCheckBox.Checked})
$FriCheckBox.add_CheckedChanged({$SchedButton.Enabled = $FriCheckBox.Checked})
[void]$Form.ShowDialog()
I would do something like this:
$Form.controls.AddRange(#($MonCheckBox, $TueCheckBox, $WedCheckBox, $ThuCheckBox, $FriCheckBox, $SchedButton))
Function Test-AnyButtonChecked {
if (
$MonCheckBox.Checked -or
$TueCheckBox.Checked -or
$WedCheckBox.Checked -or
$ThuCheckBox.Checked -or
$FriCheckBox.Checked
) {
$SchedButton.Enabled = $true
}
else {
$SchedButton.Enabled = $false
}
}
$MonCheckBox.add_CheckedChanged( { Test-AnyButtonChecked })
$TueCheckBox.add_CheckedChanged( { Test-AnyButtonChecked })
$WedCheckBox.add_CheckedChanged( { Test-AnyButtonChecked })
$ThuCheckBox.add_CheckedChanged( { Test-AnyButtonChecked })
$FriCheckBox.add_CheckedChanged( { Test-AnyButtonChecked })
[void]$Form.ShowDialog()
giving the following PowerShell Studio Code, how can I call and disable the 'David Checkbox' under the 'Checkit' button. I know I am messing up on the checkbox declaration somehow because powershell does not recognize my checkbox as an object:
The property 'Enabled' cannot be found on this object. Verify that the property exists and can be set.
$accounts = "David", "Dave"
$buttonLoadLabels_Click = {
$CheckBoxCounter = 1
$accounts = 'didier','david'
foreach ($account in $accounts)
{
$label = New-Object System.Windows.Forms.Label
$label.Text = $account
$label.TextAlign = 'MiddleCenter'
$label.Font = $label1.Font
$flowlayoutpanel1.Controls.Add($label)
$CB = New-Object System.Windows.Forms.CheckBox
$CB.Name = $account
Write-Host $CB.Name
$flowlayoutpanel1.Controls.Add($CB)
}
}
$buttonCheckIt_Click = {
$checkbox_David.Enabled = $false
}
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
...
}
I'm trying to make a form in PowerShell that gets locked to a fixed position on the desktop.
Because whenever I press the "Show Desktop" button on the bottom right corner on Win 8.1, the form disappears until I open a different window and close it.
I just want it there like it's a widget, here's a part of the code i'm using:
Add-Type -AssemblyName System.Windows.Forms
Add-Type -Assembly System.Drawing
$Image = [System.Drawing.Image]::Fromfile("Panel.png")
$Form = New-Object system.Windows.Forms.Form
$Form.BackgroundImage = $Image
$Form.BackgroundImageLayout = "None"
$Form.Text = "Reboot Server"
$Form.Width = 517
$Form.Height = 134
$Form.ControlBox = $False
$Form.StartPosition = 'Manual'
$Form.Location = "1390, 300"
$Form.FormBorderStyle = 'None'
$Form.BackColor = "#000000"
$Form.MaximizeBox = $False
$Form.MinimizeBox = $False
$Form.Icon = "icon.ico"
$Form.Image = [System.Drawing.Image]::Fromfile("Panel.png")
$Form.ShowInTaskbar = $False
$Font = New-Object System.Drawing.Font("Tahoma",10, [System.Drawing.FontStyle]::Bold)
$Form.Font = $Font
$Label = New-Object System.Windows.Forms.Label
$Label.Text = ""
$Label.AutoSize = $True
$Form.Controls.Add($Label)
' Button 1 - Reboot Server'
$Button1 = new-object System.Windows.Forms.Button
$Button1.Location = new-object System.Drawing.Size(234,51)
$Button1.Size = new-object System.Drawing.Size(77,55)
$Button1.AutoSize = $True
$Button1.Add_Click({start-process "Reboot.lnk"})
$Button1.FlatStyle = [System.Windows.Forms.FlatStyle]::Flat
$Button1.FlatAppearance.BorderSize=0
$Button1.BackColor = "Transparent"
$Button1.ForeColor = "Transparent"
$Button1.FlatAppearance.MouseDownBackColor = "Transparent"
$Button1.FlatAppearance.MouseOverBackColor = "Transparent"
$Button1.FlatAppearance.BorderColor = "#252525"
$Form.Controls.Add($Button1)
'----------------------------------------
$Form.ShowDialog() | Out-Null
Exit 0
For gadget style positions try to use a little snippet that I have made:
$Poistion = 'RightBottom'
$Coordinates = switch ($Poistion)
{
'LeftTop' { 0, 0 }
'LeftBottom' { 0, $([System.Windows.Forms.Screen]::PrimaryScreen.WorkingArea.Bottom - $Form.Height) }
'RightTop' { $([System.Windows.Forms.Screen]::PrimaryScreen.WorkingArea.Width - $Form.Width), 0 }
'RightBottom' { $([System.Windows.Forms.Screen]::PrimaryScreen.WorkingArea.Width - $Form.Width), $([System.Windows.Forms.Screen]::PrimaryScreen.WorkingArea.Bottom - $Form.Height) }
}
$Form.Location = New-Object System.Drawing.Point($Coordinates)
To keep your form up at the time when you activate "Show Desktop" set TopMost property to $True and MinimizeBox property to $False. Like so:
$Form.TopMost = $True
$Form.MinimizeBox = $False
When you click on Show Desktop or use its hotkey (Win+D) windows tries to send Minimize All command to running applications. After minimizing all the windows that can be minimized, it then takes the desktop and "raises" it to the top of the window stack so that no other windows cover it.
You could also try to use sizing events. Like so:
$Form_Resize={
$Form.WindowState = 'Normal'
}
$Form.add_Resize($Form_Resize)