$radiobutton1.Checked =$true not working

Ask questions about creating Graphical User Interfaces (GUI) in PowerShell and using WinForms controls.
Forum rules
Do not post any licensing information in this forum.

Any code longer than three lines should be added as code using the 'Select Code' dropdown menu or attached as a file.
Post Reply
User avatar
localpct
Posts: 252
Joined: Wed Sep 09, 2015 12:53 pm

$radiobutton1.Checked =$true not working

Post by localpct » Fri Jun 08, 2018 11:23 am

I'm trying to detect the powersettings for a PC

Code: Select all

$PowerSetting = Invoke-Command -ComputerName $computer { (powercfg /l | ? { $_.Contains('*') -and $_.Contains('GUID') }).Split()[3] }
		$statusbar1.Text = "Finding Power Setting"
		$PowerSetting = switch ($PowerSetting.trim())
		{
 "381b4222-f694-41f0-9685-ff5bb260df2e" { "Balanced" }
 "8c5e7fda-e8bf-4a96-9a85-a6e23a8c635c" { "High Performace" }
 "a1841308-3541-4fab-bc81-f71556f20b4a" { "Power Saver" }
		}
		$statusbar1.Text = "$($textbox1.text) power setting is $PowerSetting"
		if ($PowerSetting -eq "Balanced")
		{
			$radiobuttonHighPerformace.Checked =$true
		}
		if ($PowerSetting -eq "Power Saver")
		{
			$radiobuttonHighPerformace.Checked =$true
		}
		else
		{
			$radiobuttonBalanced.Checked =$true
		}
The current computer I'm working on, it's currently set to Balanced, but the radiobutton doesn't change to High Performace

User avatar
jvierra
Posts: 12274
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: $radiobutton1.Checked =$true not working

Post by jvierra » Fri Jun 08, 2018 11:53 am

I suggest that stepping through with the debugger would give you the answer.

This article will show you how to use WMI to easily get the power settings:

https://blogs.technet.microsoft.com/hey ... owershell/

User avatar
davidc
Posts: 5236
Joined: Thu Aug 18, 2011 4:56 am

Re: $radiobutton1.Checked =$true not working

Post by davidc » Fri Jun 08, 2018 11:53 am

You have an if instead of an elseif on the "Power Saver" line:
  1. if ($PowerSetting -eq "Balanced")
  2.  
  3. {
  4.  
  5.     $radiobuttonHighPerformace.Checked = $true
  6.  
  7. }
  8.  
  9. elseif ($PowerSetting -eq "Power Saver")
  10.  
  11. {  
  12.  
  13.     $radiobuttonHighPerformace.Checked = $true 
  14.  
  15. }
  16.  
  17. else
  18.  
  19. {
  20.  
  21.     $radiobuttonBalanced.Checked = $true
  22.  
  23. }
David
SAPIEN Technologies, Inc.

User avatar
jvierra
Posts: 12274
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: $radiobutton1.Checked =$true not working

Post by jvierra » Fri Jun 08, 2018 12:02 pm

A quick look says you have numerous spelling and naming issues. Review the code line-by-line and word-by-word to find all.

Be sure your code names match the GUI designer names.

User avatar
localpct
Posts: 252
Joined: Wed Sep 09, 2015 12:53 pm

Re: $radiobutton1.Checked =$true not working

Post by localpct » Fri Jun 08, 2018 12:14 pm

jvierra wrote:
Fri Jun 08, 2018 11:53 am
I suggest that stepping through with the debugger would give you the answer.

This article will show you how to use WMI to easily get the power settings:

https://blogs.technet.microsoft.com/hey ... owershell/
GWMI doesn't work on all our PCs or I would have went with that.

User avatar
localpct
Posts: 252
Joined: Wed Sep 09, 2015 12:53 pm

Re: $radiobutton1.Checked =$true not working

Post by localpct » Fri Jun 08, 2018 12:17 pm

davidc wrote:
Fri Jun 08, 2018 11:53 am
You have an if instead of an elseif on the "Power Saver" line:
  1. if ($PowerSetting -eq "Balanced")
  2.  
  3. {
  4.  
  5.     $radiobuttonHighPerformace.Checked = $true
  6.  
  7. }
  8.  
  9. elseif ($PowerSetting -eq "Power Saver")
  10.  
  11. {  
  12.  
  13.     $radiobuttonHighPerformace.Checked = $true 
  14.  
  15. }
  16.  
  17. else
  18.  
  19. {
  20.  
  21.     $radiobuttonBalanced.Checked = $true
  22.  
  23. }
Looks like this did it... Thank you!

User avatar
jvierra
Posts: 12274
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: $radiobutton1.Checked =$true not working

Post by jvierra » Fri Jun 08, 2018 12:19 pm

This would be the better way to use the code:

Code: Select all

    
$sb = { (powercfg /l | ? { $_.Contains('*') -and $_.Contains('GUID') }).Split()[3] }
$powerSetting = Invoke-Command -ScriptBlock $sb -ComputerName  $computer 
$statusbar1.Text = 'Finding Power Setting'
switch ($powerSetting){
    '381b4222-f694-41f0-9685-ff5bb260df2e' {
            $statusbar1.Text = $setting = 'Balanced' 
            $radiobuttonBalanced.Checked =$true
        }
    '8c5e7fda-e8bf-4a96-9a85-a6e23a8c635c' {
            $statusbar1.Text = $setting = 'High Performace' 
            $radiobuttonHighPerformance.Checked =$true
        }
    'a1841308-3541-4fab-bc81-f71556f20b4a' {
            $statusbar1.Text = $setting = 'Power Saver' 
            $radiobuttonPowerSaver.Checked =$true
        }
    default { $statusbar1.Text = 'Settings not found' }
}
$statusbar1.Text = "$($textbox1.text) power setting is $setting"

This avoids most issues of using an "if" construct and is more explicit and easier to manage.

User avatar
localpct
Posts: 252
Joined: Wed Sep 09, 2015 12:53 pm

Re: $radiobutton1.Checked =$true not working

Post by localpct » Fri Jun 08, 2018 12:58 pm

That's very cool.. I did not know you can use a switch statement like that.

Thanks!!!!

User avatar
jvierra
Posts: 12274
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: $radiobutton1.Checked =$true not working

Post by jvierra » Fri Jun 08, 2018 1:03 pm

One of the first thing we learn in any PowerShell book or training is how to use the switch statement. It is one of the most useful control structures in all programming languages and is on steroids in PowerShell


https://kevinmarquette.github.io/2018-0 ... statement/

Post Reply