$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.
This topic is 5 years and 9 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked
User avatar
localpct
Posts: 397
Last visit: Thu Oct 27, 2022 5:57 am

$radiobutton1.Checked =$true not working

Post by localpct »

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
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: $radiobutton1.Checked =$true not working

Post by jvierra »

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: 5913
Last visit: Mon Jul 08, 2019 8:55 am
Been upvoted: 2 times

Re: $radiobutton1.Checked =$true not working

Post by davidc »

You have an if instead of an elseif on the "Power Saver" line:
  1. if ($PowerSetting -eq "Balanced")
  2. {
  3.     $radiobuttonHighPerformace.Checked = $true
  4. }
  5. elseif ($PowerSetting -eq "Power Saver")
  6. {  
  7.     $radiobuttonHighPerformace.Checked = $true 
  8. }
  9. else
  10. {
  11.     $radiobuttonBalanced.Checked = $true
  12. }
David
SAPIEN Technologies, Inc.
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: $radiobutton1.Checked =$true not working

Post by jvierra »

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: 397
Last visit: Thu Oct 27, 2022 5:57 am

Re: $radiobutton1.Checked =$true not working

Post by localpct »

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: 397
Last visit: Thu Oct 27, 2022 5:57 am

Re: $radiobutton1.Checked =$true not working

Post by localpct »

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.     $radiobuttonHighPerformace.Checked = $true
  4. }
  5. elseif ($PowerSetting -eq "Power Saver")
  6. {  
  7.     $radiobuttonHighPerformace.Checked = $true 
  8. }
  9. else
  10. {
  11.     $radiobuttonBalanced.Checked = $true
  12. }
Looks like this did it... Thank you!
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: $radiobutton1.Checked =$true not working

Post by jvierra »

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: 397
Last visit: Thu Oct 27, 2022 5:57 am

Re: $radiobutton1.Checked =$true not working

Post by localpct »

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

Thanks!!!!
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: $radiobutton1.Checked =$true not working

Post by jvierra »

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/
This topic is 5 years and 9 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked