none
Using The MessageBox Class in The System.Windows.Forms namespace RRS feed

  • Question

  • Hello,I'm new to Powershell and .Net and I am trying to come up with a simple form for a user to input values to change their power settings on windows 7 and the last field I thought it would be appropriate for a Yes/No box asking to turn off hibernation if they want. Following the examples I found online and asking here I came up with this snippett:

    $Label1 = New-Object System.Windows.Forms.Label
    $Label1.Location = New-Object System.Drawing.Size(20,40)
    $Label1.Size = New-Object System.Drawing.Size(380,20)
    $Label1.Text = "Change Power Off Display Value (In Minutes) :"
    $Form.Controls.Add($Label1)

    $TextBox1 = New-Object System.Windows.Forms.TextBox
    $TextBox1.Name='Text1'
    $TextBox1.Location = New-Object System.Drawing.Size(20,60)
    $TextBox1.Size = New-Object System.Drawing.Size(260,50)
    $Form.Controls.Add($TextBox1)

    $Label2 = New-Object System.Windows.Forms.Label
    $Label2.Location = New-Object System.Drawing.Size(20,90)
    $Label2.Size = New-Object System.Drawing.Size(380,20)
    $Label2.Text = "Please enter Screensaver Timeout :"
    $Form.Controls.Add($Label2)

    $TextBox2 = New-Object System.Windows.Forms.TextBox
    $TextBox2.Name='Text2'
    $TextBox2.Location = New-Object System.Drawing.Size(20,110)
    $TextBox2.Size = New-Object System.Drawing.Size(260,50)
    $Form.Controls.Add($TextBox2)

    $Label3 = New-Object System.Windows.Forms.Label
    $Label3.Location = New-Object System.Drawing.Size(20,130)
    $Label3.Size = New-Object System.Drawing.Size(680,20)
    $Label3.Text = "Change sleep time in minutes (Should be longer then Screen Saver timeout):"
    $Form.Controls.Add($Label3)

    $TextBox3 = New-Object System.Windows.Forms.TextBox
    $TextBox3.Name='Text3'
    $TextBox3.Location = New-Object System.Drawing.Size(20,150)
    $TextBox3.Size = New-Object System.Drawing.Size(260,50)
    $Form.Controls.Add($TextBox3)

    $Label4 = New-Object System.Windows.Forms.Label
    $Label4.Location = New-Object System.Drawing.Size(20,170)
    $Label4.Size = New-Object System.Drawing.Size(380,20)
    $Label4.Text = "Lastly turnoff Hibernate:"
    $Form.Controls.Add($Label4)

    $MessageBox4 = New-Object Windows.Forms.MessageBox::Show("Do You Want Hibernate Off","Hibernate Off", [Windows.Forms.MessageBoxButtons]::YesNo, [Windows.Forms.MessageBoxIcon]::Question)
    $MessageBox4.Name='$MessageBox4'
    $MessageBox4.Location = New-Object System.Drawing.Size(20,190)
    $MessageBox4.Size = New-Object System.Drawing.Size(260,50)
    $Form.Controls.Add($MessageBox4)

    The script chokes when it gets to the underlined part with error message:

    New-Object : Cannot find type [Windows.Forms.MessageBox::Show]: make sure the assembly containing this type is loaded.
    At line:1 char:16
    + $MessageBox4 = New-Object Windows.Forms.MessageBox::Show("Do You Want Hibernate  ...
    + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        + CategoryInfo          : InvalidType: (:) [New-Object], PSArgumentException
        + FullyQualifiedErrorId : TypeNotFound,Microsoft.PowerShell.Commands.NewObjectCommand
     
    Property 'Name' cannot be found on this object; make sure it exists and is settable.
    At line:2 char:1
    + $MessageBox4.Name='$MessageBox4'
    + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        + CategoryInfo          : InvalidOperation: (:) [], RuntimeException
        + FullyQualifiedErrorId : PropertyNotFound
     
    Property 'Location' cannot be found on this object; make sure it exists and is settable.
    At line:3 char:1
    + $MessageBox4.Location = New-Object System.Drawing.Size(20,190)
    + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        + CategoryInfo          : InvalidOperation: (:) [], RuntimeException
        + FullyQualifiedErrorId : PropertyNotFound
     
    Property 'Size' cannot be found on this object; make sure it exists and is settable.
    At line:4 char:1
    + $MessageBox4.Size = New-Object System.Drawing.Size(260,50)
    + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        + CategoryInfo          : InvalidOperation: (:) [], RuntimeException
        + FullyQualifiedErrorId : PropertyNotFound
     I know I must be using MessageBox wrong but have been reading up and searching but cant find answer. If anybody has a suggestion most appreciated.  Also how to use the return value to trigger the command "powercfg -Hibernate off" if Yes is returned. Thanks again.

    Tuesday, March 4, 2014 6:35 PM

Answers

  • Thanks you're right under normal circumstances. But with four different settings and maybe more to come it's just easier to have one form then having hunt around for the hibernate off etc. Here's the rough draft of the code I came up with I just have to add some bounds checking because when no input you get an error message and I dont want the user to get confused.

    [void] [System.Reflection.Assembly]::LoadWithPartialName("System.Drawing")
    [void] [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")

    $Form = New-Object System.Windows.Forms.Form
    $Form.Text = "Screen Saver & Power Settings Changer"
    $Form.Size = New-Object System.Drawing.Size(650,375)
    $Form.StartPosition = "CenterScreen"
    $Form.Topmost = $True
    $Form.Add_Shown({$Form.Activate()})

    $OKButton = New-Object System.Windows.Forms.Button
    $OKButton.Location = New-Object System.Drawing.Size(75,250)
    $OKButton.Size = New-Object System.Drawing.Size(75,23)
    $OKButton.Text='OK'
    $OKButton.DialogResult='Ok'
    $Form.Controls.Add($OKButton)
    $Form.AcceptButton=$OKButton

    $CancelButton = New-Object System.Windows.Forms.Button
    $CancelButton.Location = New-Object System.Drawing.Size(150,250)
    $CancelButton.Size = New-Object System.Drawing.Size(75,23)
    $CancelButton.Text ='Cancel'
    $Form.Controls.Add($CancelButton)
    $Form.CancelButton=$CancelButton

    $Label1 = New-Object System.Windows.Forms.Label
    $Label1.Location = New-Object System.Drawing.Size(20,40)
    $Label1.Size = New-Object System.Drawing.Size(380,20)
    $Label1.Text = "Change Power Off Display Value (In Minutes) :"
    $Form.Controls.Add($Label1)

    $TextBox1 = New-Object System.Windows.Forms.TextBox
    $TextBox1.Name='Text1'
    $TextBox1.Location = New-Object System.Drawing.Size(20,60)
    $TextBox1.Size = New-Object System.Drawing.Size(260,50)
    $Form.Controls.Add($TextBox1)

    $Label2 = New-Object System.Windows.Forms.Label
    $Label2.Location = New-Object System.Drawing.Size(20,90)
    $Label2.Size = New-Object System.Drawing.Size(380,20)
    $Label2.Text = "Please enter Screensaver Timeout :"
    $Form.Controls.Add($Label2)

    $TextBox2 = New-Object System.Windows.Forms.TextBox
    $TextBox2.Name='Text2'
    $TextBox2.Location = New-Object System.Drawing.Size(20,110)
    $TextBox2.Size = New-Object System.Drawing.Size(260,50)
    $Form.Controls.Add($TextBox2)

    $Label3 = New-Object System.Windows.Forms.Label
    $Label3.Location = New-Object System.Drawing.Size(20,130)
    $Label3.Size = New-Object System.Drawing.Size(680,20)
    $Label3.Text = "Change sleep time in minutes (Should be longer then Screen Saver timeout):"
    $Form.Controls.Add($Label3)

    $TextBox3 = New-Object System.Windows.Forms.TextBox
    $TextBox3.Name='Text3'
    $TextBox3.Location = New-Object System.Drawing.Size(20,150)
    $TextBox3.Size = New-Object System.Drawing.Size(260,50)
    $Form.Controls.Add($TextBox3)

    $Label4 = New-Object System.Windows.Forms.Label
    $Label4.Location = New-Object System.Drawing.Size(20,180)
    $Label4.Size = New-Object System.Drawing.Size(360,20)
    $Label4.Text = "Lastly turnoff Hibernate:"
    $Form.Controls.Add($Label4)

    $CheckBox4 = New-Object System.Windows.Forms.CheckBox
    $CheckBox4.Name='$CheckBox4'
    $CheckBox4.Location = New-Object System.Drawing.Size(20,180)
    $CheckBox4.Size = New-Object System.Drawing.Size(260,50)
    $Form.Controls.Add($CheckBox4)

    if($Form.ShowDialog() -eq 'Ok' ){
        powercfg -Change -monitor-timeout-ac $TextBox1.Text


        $TextBox2.Text = [int]$TextBox2.Text * 60

        reg add "HKLM\Software\Policies\Microsoft\Windows\Control Panel\Desktop" /v ScreenSaveTimeOut /t REG_SZ /d $TextBox2.Text /f


        powercfg -Change -standby-timeout-ac $TextBox3.Text

        if ($CheckBox4) {powercfg -Hibernate Off}

            
    }else{
           Exit
         }

    • Marked as answer by AlanK87 Wednesday, March 5, 2014 2:55 PM
    Wednesday, March 5, 2014 2:55 PM

All replies

  • Try this:

    [Windows.Forms.MessageBox]::Show('Hello World','My Message Box')


    ¯\_(ツ)_/¯

    Tuesday, March 4, 2014 6:44 PM
  • To get just a simple answer:

    [Windows.Forms.MessageBox]::Show('Do you want to learn more?','The big question:',4)

    I recommend using a checkbox instead.


    ¯\_(ツ)_/¯

    Tuesday, March 4, 2014 6:47 PM
  • Thanks. You helped me out before with this. So I dont need the lines :

    $MessageBox4.Name='$MessageBox4'
    $MessageBox4.Location = New-Object System.Drawing.Size(20,190)
    $MessageBox4.Size = New-Object System.Drawing.Size(260,50)
    $Form.Controls.Add($MessageBox4)


    Because this Box is not integrating with the Form Box for the other text three fields which is what I want. I get this pop up as a separate box.
    • Edited by AlanK87 Tuesday, March 4, 2014 6:55 PM
    Tuesday, March 4, 2014 6:52 PM
  • A message box is NOT a Windows control.  It is a full utility window.  It is not apart of the form.  It is a child of the forms Window.


    ¯\_(ツ)_/¯

    Tuesday, March 4, 2014 6:59 PM
  • Thanks for the clarification. Is there any other way to integrate a sort of Y/N into the form? I'm curious why is it listed under system.windows.forms class if doesnt integrate with the form?
    • Edited by AlanK87 Tuesday, March 4, 2014 7:05 PM
    Tuesday, March 4, 2014 7:02 PM
  • Thanks for the clarification. Is there any other way to integrate a sort of Y/N into the form?

    That is why I posted that you should use a checkbox.  A checkbox is a yes/no, true/false, 0/1 sort of control.


    ¯\_(ツ)_/¯

    Tuesday, March 4, 2014 7:05 PM
  • Hi,

    You can use radio buttons:

    http://msdn.microsoft.com/en-us/library/system.windows.forms.radiobutton%28v=vs.100%29.aspx

    EDIT: Also, is there a reason you're building a GUI for something that already has a GUI built in?


    Don't retire TechNet! - (Don't give up yet - 12,700+ strong and growing)


    Tuesday, March 4, 2014 7:06 PM
  • Hi,

    You can use radio buttons:

    http://msdn.microsoft.com/en-us/library/system.windows.forms.radiobutton%28v=vs.100%29.aspx

    EDIT: Also, is there a reason you're building a GUI for something that already has a GUI built in?


    Don't retire TechNet! - (Don't give up yet - 12,700+ strong and growing)


    Mike - in forms a radio button is not a yes/no - it is one of a choice.  While you can make the choices yes/no it is not how we normally select to use or not use a feature.  The paradigm in forms in Windows, the web  and Apple/Mac has always been to use a checkbox to select an item.

    There are some good books on User interface design standards that are worth reading if you do this frequently.  The original was written by IBM and called CUA (Common User Access).  All other vendors have subscribed to this and continue to evolve the standard.  Of course you are free to be as creative as you want but only after you understand the rules.


    ¯\_(ツ)_/¯

    Tuesday, March 4, 2014 7:19 PM
  • Here - read it an weep: http://en.wikipedia.org/wiki/IBM_Common_User_Access

    It has a very funny ending and makes a good coffeetable book.


    ¯\_(ツ)_/¯

    Tuesday, March 4, 2014 7:20 PM
  • Tuesday, March 4, 2014 7:21 PM
  • You and your rule following ways....

    =]


    Don't retire TechNet! - (Don't give up yet - 12,700+ strong and growing)

    I said you can be creative.  I just think that the checkbox is what users are used to.  It is also much easier to implement in this scenario.


    ¯\_(ツ)_/¯

    Tuesday, March 4, 2014 7:24 PM
  • There is also another drawback to doing this with a radio button.  Only one group is allowed.  How do you do more than one group?


    ¯\_(ツ)_/¯

    Tuesday, March 4, 2014 7:25 PM
  • Here is a simple demo of a radio button form. Run it and you will see what I mean.

    This builds a dessert.  Try and have the selections you want on you ice cream.

    function Demo-Radio {
    
    	#----------------------------------------------
    	#region Import the Assemblies
    	#----------------------------------------------
    	[void][reflection.assembly]::Load("mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089")
    	[void][reflection.assembly]::Load("System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089")
    	[void][reflection.assembly]::Load("System.Drawing, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a")
    	[void][reflection.assembly]::Load("System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089")
    	[void][reflection.assembly]::Load("System.Data, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089")
    	[void][reflection.assembly]::Load("System.Xml, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089")
    	[void][reflection.assembly]::Load("System.DirectoryServices, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a")
    	[void][reflection.assembly]::Load("System.Core, Version=3.5.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089")
    	[void][reflection.assembly]::Load("System.ServiceProcess, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a")
    	#endregion Import Assemblies
    
    	#----------------------------------------------
    	#region Generated Form Objects
    	#----------------------------------------------
    	[System.Windows.Forms.Application]::EnableVisualStyles()
    	$form1 = New-Object 'System.Windows.Forms.Form'
    	$labelBuildYourIceCreamSun = New-Object 'System.Windows.Forms.Label'
    	$radiobuttonSprinkles = New-Object 'System.Windows.Forms.RadioButton'
    	$radiobuttonPickles = New-Object 'System.Windows.Forms.RadioButton'
    	$radiobuttonFudge = New-Object 'System.Windows.Forms.RadioButton'
    	$buttonOK = New-Object 'System.Windows.Forms.Button'
    	$InitialFormWindowState = New-Object 'System.Windows.Forms.FormWindowState'
    	#endregion Generated Form Objects
    
    	#----------------------------------------------
    	# User Generated Script
    	#----------------------------------------------
    	
    	
    	
    	
    	
    	
    	
    	$FormEvent_Load={
    		#TODO: Initialize Form Controls here
    		
    	}
    	
    	# --End User Generated Script--
    	#----------------------------------------------
    	#region Generated Events
    	#----------------------------------------------
    	
    	$Form_StateCorrection_Load=
    	{
    		#Correct the initial state of the form to prevent the .Net maximized form issue
    		$form1.WindowState = $InitialFormWindowState
    	}
    	
    	$Form_Cleanup_FormClosed=
    	{
    		#Remove all event handlers from the controls
    		try
    		{
    			$form1.remove_Load($FormEvent_Load)
    			$form1.remove_Load($Form_StateCorrection_Load)
    			$form1.remove_FormClosed($Form_Cleanup_FormClosed)
    		}
    		catch [Exception]
    		{ }
    	}
    	#endregion Generated Events
    
    	#----------------------------------------------
    	#region Generated Form Code
    	#----------------------------------------------
    	#
    	# form1
    	#
    	$form1.Controls.Add($labelBuildYourIceCreamSun)
    	$form1.Controls.Add($radiobuttonSprinkles)
    	$form1.Controls.Add($radiobuttonPickles)
    	$form1.Controls.Add($radiobuttonFudge)
    	$form1.Controls.Add($buttonOK)
    	$form1.AcceptButton = $buttonOK
    	$form1.ClientSize = '284, 262'
    	$form1.FormBorderStyle = 'FixedDialog'
    	$form1.MaximizeBox = $False
    	$form1.MinimizeBox = $False
    	$form1.Name = "form1"
    	$form1.StartPosition = 'CenterScreen'
    	$form1.Text = "Form"
    	$form1.add_Load($FormEvent_Load)
    	#
    	# labelBuildYourIceCreamSun
    	#
    	$labelBuildYourIceCreamSun.Font = "Microsoft Sans Serif, 9.75pt, style=Bold"
    	$labelBuildYourIceCreamSun.Location = '12, 9'
    	$labelBuildYourIceCreamSun.Name = "labelBuildYourIceCreamSun"
    	$labelBuildYourIceCreamSun.Size = '221, 23'
    	$labelBuildYourIceCreamSun.TabIndex = 4
    	$labelBuildYourIceCreamSun.Text = "Build your ice cream sundae"
    	#
    	# radiobuttonSprinkles
    	#
    	$radiobuttonSprinkles.Location = '112, 77'
    	$radiobuttonSprinkles.Name = "radiobuttonSprinkles"
    	$radiobuttonSprinkles.Size = '104, 24'
    	$radiobuttonSprinkles.TabIndex = 3
    	$radiobuttonSprinkles.TabStop = $True
    	$radiobuttonSprinkles.Text = "Sprinkles"
    	$radiobuttonSprinkles.UseVisualStyleBackColor = $True
    	#
    	# radiobuttonPickles
    	#
    	$radiobuttonPickles.Location = '112, 107'
    	$radiobuttonPickles.Name = "radiobuttonPickles"
    	$radiobuttonPickles.Size = '104, 24'
    	$radiobuttonPickles.TabIndex = 2
    	$radiobuttonPickles.TabStop = $True
    	$radiobuttonPickles.Text = "Pickles"
    	$radiobuttonPickles.UseVisualStyleBackColor = $True
    	#
    	# radiobuttonFudge
    	#
    	$radiobuttonFudge.Location = '112, 47'
    	$radiobuttonFudge.Name = "radiobuttonFudge"
    	$radiobuttonFudge.Size = '104, 24'
    	$radiobuttonFudge.TabIndex = 1
    	$radiobuttonFudge.TabStop = $True
    	$radiobuttonFudge.Text = "Fudge"
    	$radiobuttonFudge.UseVisualStyleBackColor = $True
    	#
    	# buttonOK
    	#
    	$buttonOK.Anchor = 'Bottom, Right'
    	$buttonOK.DialogResult = 'OK'
    	$buttonOK.Location = '197, 227'
    	$buttonOK.Name = "buttonOK"
    	$buttonOK.Size = '75, 23'
    	$buttonOK.TabIndex = 0
    	$buttonOK.Text = "OK"
    	$buttonOK.UseVisualStyleBackColor = $True
    	#endregion Generated Form Code
    
    	#----------------------------------------------
    
    	#Save the initial state of the form
    	$InitialFormWindowState = $form1.WindowState
    	#Init the OnLoad event to correct the initial state of the form
    	$form1.add_Load($Form_StateCorrection_Load)
    	#Clean up the control events
    	$form1.add_FormClosed($Form_Cleanup_FormClosed)
    	#Show the Form
    	return $form1.ShowDialog()
    
    }
    
    Demo-Radio
    
    


    ¯\_(ツ)_/¯

    Tuesday, March 4, 2014 7:33 PM
  • There is also another drawback to doing this with a radio button.  Only one group is allowed.  How do you do more than one group?


    ¯\_(ツ)_/¯


    I've used GroupBox for that.

    Don't retire TechNet! - (Don't give up yet - 12,700+ strong and growing)

    Tuesday, March 4, 2014 7:34 PM
  • here is the identical same form with checkboxes.  Now select all of your favorite toppings.

    function Demo-Radio{
    
    	#----------------------------------------------
    	#region Import the Assemblies
    	#----------------------------------------------
    	[void][reflection.assembly]::Load("mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089")
    	[void][reflection.assembly]::Load("System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089")
    	[void][reflection.assembly]::Load("System.Drawing, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a")
    	[void][reflection.assembly]::Load("System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089")
    	[void][reflection.assembly]::Load("System.Data, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089")
    	[void][reflection.assembly]::Load("System.Xml, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089")
    	[void][reflection.assembly]::Load("System.DirectoryServices, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a")
    	[void][reflection.assembly]::Load("System.Core, Version=3.5.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089")
    	[void][reflection.assembly]::Load("System.ServiceProcess, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a")
    	#endregion Import Assemblies
    
    	#----------------------------------------------
    	#region Generated Form Objects
    	#----------------------------------------------
    	[System.Windows.Forms.Application]::EnableVisualStyles()
    	$form1 = New-Object 'System.Windows.Forms.Form'
    	$checkboxNuts = New-Object 'System.Windows.Forms.CheckBox'
    	$checkboxFriedAnts = New-Object 'System.Windows.Forms.CheckBox'
    	$checkboxFudge = New-Object 'System.Windows.Forms.CheckBox'
    	$labelBuildYourIceCreamSun = New-Object 'System.Windows.Forms.Label'
    	$buttonOK = New-Object 'System.Windows.Forms.Button'
    	$InitialFormWindowState = New-Object 'System.Windows.Forms.FormWindowState'
    	#endregion Generated Form Objects
    
    	#----------------------------------------------
    	# User Generated Script
    	#----------------------------------------------
    	
    	
    	
    	
    	
    	
    	
    	$FormEvent_Load={
    		#TODO: Initialize Form Controls here
    		
    	}
    	
    	# --End User Generated Script--
    	#----------------------------------------------
    	#region Generated Events
    	#----------------------------------------------
    	
    	$Form_StateCorrection_Load=
    	{
    		#Correct the initial state of the form to prevent the .Net maximized form issue
    		$form1.WindowState = $InitialFormWindowState
    	}
    	
    	$Form_Cleanup_FormClosed=
    	{
    		#Remove all event handlers from the controls
    		try
    		{
    			$form1.remove_Load($FormEvent_Load)
    			$form1.remove_Load($Form_StateCorrection_Load)
    			$form1.remove_FormClosed($Form_Cleanup_FormClosed)
    		}
    		catch [Exception]
    		{ }
    	}
    	#endregion Generated Events
    
    	#----------------------------------------------
    	#region Generated Form Code
    	#----------------------------------------------
    	#
    	# form1
    	#
    	$form1.Controls.Add($checkboxNuts)
    	$form1.Controls.Add($checkboxFriedAnts)
    	$form1.Controls.Add($checkboxFudge)
    	$form1.Controls.Add($labelBuildYourIceCreamSun)
    	$form1.Controls.Add($buttonOK)
    	$form1.AcceptButton = $buttonOK
    	$form1.ClientSize = '284, 262'
    	$form1.FormBorderStyle = 'FixedDialog'
    	$form1.MaximizeBox = $False
    	$form1.MinimizeBox = $False
    	$form1.Name = "form1"
    	$form1.StartPosition = 'CenterScreen'
    	$form1.Text = "Form"
    	$form1.add_Load($FormEvent_Load)
    	#
    	# checkboxNuts
    	#
    	$checkboxNuts.Location = '112, 80'
    	$checkboxNuts.Name = "checkboxNuts"
    	$checkboxNuts.Size = '104, 24'
    	$checkboxNuts.TabIndex = 7
    	$checkboxNuts.Text = "Nuts"
    	$checkboxNuts.UseVisualStyleBackColor = $True
    	#
    	# checkboxFriedAnts
    	#
    	$checkboxFriedAnts.Location = '112, 101'
    	$checkboxFriedAnts.Name = "checkboxFriedAnts"
    	$checkboxFriedAnts.Size = '104, 24'
    	$checkboxFriedAnts.TabIndex = 6
    	$checkboxFriedAnts.Text = "Fried ants"
    	$checkboxFriedAnts.UseVisualStyleBackColor = $True
    	#
    	# checkboxFudge
    	#
    	$checkboxFudge.Location = '112, 61'
    	$checkboxFudge.Name = "checkboxFudge"
    	$checkboxFudge.Size = '104, 24'
    	$checkboxFudge.TabIndex = 5
    	$checkboxFudge.Text = "Fudge"
    	$checkboxFudge.UseVisualStyleBackColor = $True
    	#
    	# labelBuildYourIceCreamSun
    	#
    	$labelBuildYourIceCreamSun.Font = "Microsoft Sans Serif, 9.75pt, style=Bold"
    	$labelBuildYourIceCreamSun.Location = '12, 9'
    	$labelBuildYourIceCreamSun.Name = "labelBuildYourIceCreamSun"
    	$labelBuildYourIceCreamSun.Size = '221, 23'
    	$labelBuildYourIceCreamSun.TabIndex = 4
    	$labelBuildYourIceCreamSun.Text = "Build your ice cream sundae"
    	#
    	# buttonOK
    	#
    	$buttonOK.Anchor = 'Bottom, Right'
    	$buttonOK.DialogResult = 'OK'
    	$buttonOK.Location = '197, 227'
    	$buttonOK.Name = "buttonOK"
    	$buttonOK.Size = '75, 23'
    	$buttonOK.TabIndex = 0
    	$buttonOK.Text = "OK"
    	$buttonOK.UseVisualStyleBackColor = $True
    	#endregion Generated Form Code
    
    	#----------------------------------------------
    
    	#Save the initial state of the form
    	$InitialFormWindowState = $form1.WindowState
    	#Init the OnLoad event to correct the initial state of the form
    	$form1.add_Load($Form_StateCorrection_Load)
    	#Clean up the control events
    	$form1.add_FormClosed($Form_Cleanup_FormClosed)
    	#Show the Form
    	return $form1.ShowDialog()
    
    }
    Demo-Radio
    


    ¯\_(ツ)_/¯

    Tuesday, March 4, 2014 7:36 PM
  • So for every time you want to select something you will create a separate group box and add two radio buttons marked yes and no.

    The two items.  radio and check, are designed to behave differently to perform specifc kinds of tasks.  THe deign of th check and radio behavior was done by IBM many, many years ago,  It is how we think and work.

    On paper we do a radio group by providing a list and asking the form filler to place a number in the box.  In a computer form we only need to select a radio button.  THe nuber is entered automatically by the software.

    Working with databases this becomes a powerful tool.  We can use an FK table to generate the choices and the group will auto-post the select to the database with very little code.

    a checkbox will either generate bits to a field or records to a relational table dependingon what the schema is designed to do.

    None of this is by accidient and none is because it is cute, convenient or artistic.  It is because that is how a computerized system (automated system)  works best.


    ¯\_(ツ)_/¯

    Tuesday, March 4, 2014 7:44 PM
  • Yeah, it's not the best way to handle things, but I've found it to be useful in the past for a few things here and there.

    Somehow two radio buttons look better to my eye than a single checkbox. No good reason for that though.


    Don't retire TechNet! - (Don't give up yet - 12,700+ strong and growing)

    Tuesday, March 4, 2014 8:02 PM
  • Yeah, it's not the best way to handle things, but I've found it to be useful in the past for a few things here and there.

    Somehow two radio buttons look better to my eye than a single checkbox. No good reason for that though.


    Don't retire TechNet! - (Don't give up yet - 12,700+ strong and growing)

    If you like to write twice as much code then knock yourself out.

    The problem is one of concept.  YOu think of it as a yes no issue and one or tehother must be chosen.  In the reality of our usivivers the choice is always pre-defined. 

    In almost any language an initialized variable or field is a NO/Nyet/False/0  state,  We choose it and it is the opposite.  A check box choses to flag something that is un-flagged by default.  A radio chooses one of many and usually always more than two.  You have chosen to enable the one possibly ambiguous case.  If it works then shoot it.


    ¯\_(ツ)_/¯

    Tuesday, March 4, 2014 8:34 PM
  • Hi,

    You can use radio buttons:

    http://msdn.microsoft.com/en-us/library/system.windows.forms.radiobutton%28v=vs.100%29.aspx

    EDIT: Also, is there a reason you're building a GUI for something that already has a GUI built in?


    Don't retire TechNet! - (Don't give up yet - 12,700+ strong and growing)


    Because the "IT" dept thought it was a good idea to set the screensaver to logoff and make it 15 minutes.Then go into gpo and enable not being able to change the screensaver. Mind you I work in  a school were periods are 50 minutes. so teacher teaches and goes to use desktop and guess what, finds he/she is logged off and thanks to those same "IT" guys logon takes 2-3 minutes on edirectory. Me, helpdesk guy, gets calls of complaint naturally about the ridiculous setup. So I just want something where I can centrally change all power settings. Cause believe me having these users going 2-3 deep in menus is no picnic either.

    Tuesday, March 4, 2014 10:23 PM
  • If something is dictated by policy, you're wasting your time trying to change it manually, because the next time the policy refreshes, the setting will be reset anyway.

    The correct way to "fix" this is to adjust the policy, not to try to script a hack around it.

    Bill

    Tuesday, March 4, 2014 10:30 PM
    Moderator
  • Just want to say thanks for the input. I need a good beginners book. I'm confused because I'm not sure what .net does? When I look up something like the usage for windows.forms.textbox there are code samples on the technet page for c#,C## or VB, none of which I can run in the script. What exactly is .net? Should I get a book on that, or VB? All quite confusing coming from learning programming 30 years ago with high level language like Pascal or PL/1.But thanks and let me see if I can figure out how to integrate the checkbox into the form cause I dont see that in your comments. Cheers!

    Tuesday, March 4, 2014 10:32 PM
  • Fortunately  they really dont know what they're doing so the only policy they  invoke is the user unable to click the screensaver link under personalization. No other policies are enforced so I can change screensaver timeout,turn off hibernation etc and it will be persistent.
    Tuesday, March 4, 2014 10:40 PM
  • Hi,

    You can use radio buttons:

    http://msdn.microsoft.com/en-us/library/system.windows.forms.radiobutton%28v=vs.100%29.aspx

    EDIT: Also, is there a reason you're building a GUI for something that already has a GUI built in?


    Don't retire TechNet! - (Don't give up yet - 12,700+ strong and growing)


    Because the "IT" dept thought it was a good idea to set the screensaver to logoff and make it 15 minutes.Then go into gpo and enable not being able to change the screensaver. Mind you I work in  a school were periods are 50 minutes. so teacher teaches and goes to use desktop and guess what, finds he/she is logged off and thanks to those same "IT" guys logon takes 2-3 minutes on edirectory. Me, helpdesk guy, gets calls of complaint naturally about the ridiculous setup. So I just want something where I can centrally change all power settings. Cause believe me having these users going 2-3 deep in menus is no picnic either.

    Somebody is jerking your chain because the screensaver does NOT log a user of and closing the screen saver takes only a second.  It sounds more like you are trying to find a way to defeat the IT departments security settings which you cannot do.  This will not work.

    Settings cannot be changed once the GP has been set to control them. The screen saver DOES NOT log off a user.

    Whoever told you that was the case is jerking your chain or, more likely, you are trying to find a way to hack the system that the IT department has locked down.  It is always recommended that school systems be locked down and that teachers and other have screen locks time out in 5 minutes.  Student always try to get around this to try and steal test scores or other little pranks.  The IT department has done the right thing.  You cannot defeat it.


    ¯\_(ツ)_/¯

    Tuesday, March 4, 2014 10:59 PM
  • Fortunately  they really dont know what they're doing so the only policy they  invoke is the user unable to click the screensaver link under personalization. No other policies are enforced so I can change screensaver timeout,turn off hibernation etc and it will be persistent.

    If you are a trained desktop technician then you know what dotNet is.  Writing forms code is tricky.  It requires good computer skills. 

    dotNet cannot bypass Group Policy nor can any scripting language or tools.


    ¯\_(ツ)_/¯

    Tuesday, March 4, 2014 11:01 PM
  • Hi,

    You can use radio buttons:

    http://msdn.microsoft.com/en-us/library/system.windows.forms.radiobutton%28v=vs.100%29.aspx

    EDIT: Also, is there a reason you're building a GUI for something that already has a GUI built in?


    Don't retire TechNet! - (Don't give up yet - 12,700+ strong and growing)


    Because the "IT" dept thought it was a good idea to set the screensaver to logoff and make it 15 minutes.Then go into gpo and enable not being able to change the screensaver. Mind you I work in  a school were periods are 50 minutes. so teacher teaches and goes to use desktop and guess what, finds he/she is logged off and thanks to those same "IT" guys logon takes 2-3 minutes on edirectory. Me, helpdesk guy, gets calls of complaint naturally about the ridiculous setup. So I just want something where I can centrally change all power settings. Cause believe me having these users going 2-3 deep in menus is no picnic either.

    Somebody is jerking your chain because the screensaver does NOT log a user of and closing the screen saver takes only a second.  It sounds more like you are trying to find a way to defeat the IT departments security settings which you cannot do.  This will not work.

    Settings cannot be changed once the GP has been set to control them. The screen saver DOES NOT log off a user.

    Whoever told you that was the case is jerking your chain or, more likely, you are trying to find a way to hack the system that the IT department has locked down.  It is always recommended that school systems be locked down and that teachers and other have screen locks time out in 5 minutes.  Student always try to get around this to try and steal test scores or other little pranks.  The IT department has done the right thing.  You cannot defeat it.


    ¯\_(ツ)_/¯

    Not to belabor the point but please see the attached images. Perhaps I wasnt being precise when I said GPO I should have specified Local Group Policy? And I am not trying to circumvent IT but when you're boss says do it to quote Tennyson "yours is not to wonder why, yours is but to do or die."  Your skills are obviously way ahead my measly attempts. I am just starting out in a second career, so if you could please cut me a little slack. Thanks again for your input.
    Wednesday, March 5, 2014 2:16 PM
  • You cannot override local group policy either.  That is why it is called "Group Policy"  It is an enforced "policy".

    If local group policy is not set by domain policy is, as you have noted, then you cannot over ride it.

    Why not just let the user set the screensaver with the personalization tool?  It only takes one click to get there.

    On Windows 8 just type "settings' and it will appear.

    On Windows 7 and earlier you can just put a link on the desktop to the personalization form.


    ¯\_(ツ)_/¯

    Wednesday, March 5, 2014 2:26 PM
  • Running "desk.cpl" will open the screensaver dialog on all versions of Windows.  If SS is disable by domain policy then all items will be grayed out making the settings readonly.  If you can set the settings then here is the dialog you are trying to build.

    You can create a shortcut to this cpl applet on the "All Users" desktop and everyone will get it.


    ¯\_(ツ)_/¯

    Wednesday, March 5, 2014 2:30 PM
  • Thanks you're right under normal circumstances. But with four different settings and maybe more to come it's just easier to have one form then having hunt around for the hibernate off etc. Here's the rough draft of the code I came up with I just have to add some bounds checking because when no input you get an error message and I dont want the user to get confused.

    [void] [System.Reflection.Assembly]::LoadWithPartialName("System.Drawing")
    [void] [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")

    $Form = New-Object System.Windows.Forms.Form
    $Form.Text = "Screen Saver & Power Settings Changer"
    $Form.Size = New-Object System.Drawing.Size(650,375)
    $Form.StartPosition = "CenterScreen"
    $Form.Topmost = $True
    $Form.Add_Shown({$Form.Activate()})

    $OKButton = New-Object System.Windows.Forms.Button
    $OKButton.Location = New-Object System.Drawing.Size(75,250)
    $OKButton.Size = New-Object System.Drawing.Size(75,23)
    $OKButton.Text='OK'
    $OKButton.DialogResult='Ok'
    $Form.Controls.Add($OKButton)
    $Form.AcceptButton=$OKButton

    $CancelButton = New-Object System.Windows.Forms.Button
    $CancelButton.Location = New-Object System.Drawing.Size(150,250)
    $CancelButton.Size = New-Object System.Drawing.Size(75,23)
    $CancelButton.Text ='Cancel'
    $Form.Controls.Add($CancelButton)
    $Form.CancelButton=$CancelButton

    $Label1 = New-Object System.Windows.Forms.Label
    $Label1.Location = New-Object System.Drawing.Size(20,40)
    $Label1.Size = New-Object System.Drawing.Size(380,20)
    $Label1.Text = "Change Power Off Display Value (In Minutes) :"
    $Form.Controls.Add($Label1)

    $TextBox1 = New-Object System.Windows.Forms.TextBox
    $TextBox1.Name='Text1'
    $TextBox1.Location = New-Object System.Drawing.Size(20,60)
    $TextBox1.Size = New-Object System.Drawing.Size(260,50)
    $Form.Controls.Add($TextBox1)

    $Label2 = New-Object System.Windows.Forms.Label
    $Label2.Location = New-Object System.Drawing.Size(20,90)
    $Label2.Size = New-Object System.Drawing.Size(380,20)
    $Label2.Text = "Please enter Screensaver Timeout :"
    $Form.Controls.Add($Label2)

    $TextBox2 = New-Object System.Windows.Forms.TextBox
    $TextBox2.Name='Text2'
    $TextBox2.Location = New-Object System.Drawing.Size(20,110)
    $TextBox2.Size = New-Object System.Drawing.Size(260,50)
    $Form.Controls.Add($TextBox2)

    $Label3 = New-Object System.Windows.Forms.Label
    $Label3.Location = New-Object System.Drawing.Size(20,130)
    $Label3.Size = New-Object System.Drawing.Size(680,20)
    $Label3.Text = "Change sleep time in minutes (Should be longer then Screen Saver timeout):"
    $Form.Controls.Add($Label3)

    $TextBox3 = New-Object System.Windows.Forms.TextBox
    $TextBox3.Name='Text3'
    $TextBox3.Location = New-Object System.Drawing.Size(20,150)
    $TextBox3.Size = New-Object System.Drawing.Size(260,50)
    $Form.Controls.Add($TextBox3)

    $Label4 = New-Object System.Windows.Forms.Label
    $Label4.Location = New-Object System.Drawing.Size(20,180)
    $Label4.Size = New-Object System.Drawing.Size(360,20)
    $Label4.Text = "Lastly turnoff Hibernate:"
    $Form.Controls.Add($Label4)

    $CheckBox4 = New-Object System.Windows.Forms.CheckBox
    $CheckBox4.Name='$CheckBox4'
    $CheckBox4.Location = New-Object System.Drawing.Size(20,180)
    $CheckBox4.Size = New-Object System.Drawing.Size(260,50)
    $Form.Controls.Add($CheckBox4)

    if($Form.ShowDialog() -eq 'Ok' ){
        powercfg -Change -monitor-timeout-ac $TextBox1.Text


        $TextBox2.Text = [int]$TextBox2.Text * 60

        reg add "HKLM\Software\Policies\Microsoft\Windows\Control Panel\Desktop" /v ScreenSaveTimeOut /t REG_SZ /d $TextBox2.Text /f


        powercfg -Change -standby-timeout-ac $TextBox3.Text

        if ($CheckBox4) {powercfg -Hibernate Off}

            
    }else{
           Exit
         }

    • Marked as answer by AlanK87 Wednesday, March 5, 2014 2:55 PM
    Wednesday, March 5, 2014 2:55 PM
  • Sorry to bother again but one thing. The line " if ($CheckBox4) {powercfg -Hibernate Off}" seems to be executing even when box is left unchecked. Am I using the true Boolean correctly in that line, meaning if checkbox is ticked $Checkbox4 is true and if not it returns false?
    Wednesday, March 5, 2014 4:33 PM
  • $checkbox4.checked


    ¯\_(ツ)_/¯

    Wednesday, March 5, 2014 4:36 PM
  • Thanks. I just found an online example too. Totally my fault I should have checked the Checkbox properties page and I would have got my answer rightaway. I'm starting to get it. By the way do you ever sleep? It seems you're always on here:).
    Wednesday, March 5, 2014 5:08 PM
  • Thanks. I just found an online example too. Totally my fault I should have checked the Checkbox properties page and I would have got my answer rightaway. I'm starting to get it. By the way do you ever sleep? It seems you're always on here:).

    Global clients. I sleep in two shifts.

    ¯\_(ツ)_/¯

    Wednesday, March 5, 2014 5:15 PM
  • just wanted to post the results in case anyone needed to do something similar. I'm sure it can be done more elegantly but it seems to work ok and changes the settings for all users on the computer,

    [void] [System.Reflection.Assembly]::LoadWithPartialName("System.Drawing")
    [void] [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")

    $Form = New-Object System.Windows.Forms.Form
    $Form.Text = "Screen Saver & Power Settings Changer"
    $Form.Size = New-Object System.Drawing.Size(650,375)
    $Form.StartPosition = "CenterScreen"
    $Form.Topmost = $True
    $Form.Add_Shown({$Form.Activate()})

    $OKButton = New-Object System.Windows.Forms.Button
    $OKButton.Location = New-Object System.Drawing.Size(75,250)
    $OKButton.Size = New-Object System.Drawing.Size(75,23)
    $OKButton.Text='OK'
    $OKButton.DialogResult='Ok'
    $Form.Controls.Add($OKButton)
    $Form.AcceptButton=$OKButton

    $CancelButton = New-Object System.Windows.Forms.Button
    $CancelButton.Location = New-Object System.Drawing.Size(150,250)
    $CancelButton.Size = New-Object System.Drawing.Size(75,23)
    $CancelButton.Text ='Cancel'
    $Form.Controls.Add($CancelButton)
    $Form.CancelButton=$CancelButton

    $Label1 = New-Object System.Windows.Forms.Label
    $Label1.Location = New-Object System.Drawing.Size(20,40)
    $Label1.Size = New-Object System.Drawing.Size(380,20)
    $Label1.Text = "Change Power Off Display Value (In Minutes) :"
    $Form.Controls.Add($Label1)

    $TextBox1 = New-Object System.Windows.Forms.TextBox
    $TextBox1.Name='Text1'
    $TextBox1.Location = New-Object System.Drawing.Size(20,60)
    $TextBox1.Size = New-Object System.Drawing.Size(260,50)
    $Form.Controls.Add($TextBox1)

    $Label2 = New-Object System.Windows.Forms.Label
    $Label2.Location = New-Object System.Drawing.Size(20,90)
    $Label2.Size = New-Object System.Drawing.Size(380,20)
    $Label2.Text = "Please enter Screensaver Timeout :"
    $Form.Controls.Add($Label2)

    $TextBox2 = New-Object System.Windows.Forms.TextBox
    $TextBox2.Name='Text2'
    $TextBox2.Location = New-Object System.Drawing.Size(20,110)
    $TextBox2.Size = New-Object System.Drawing.Size(260,50)
    $Form.Controls.Add($TextBox2)

    $Label3 = New-Object System.Windows.Forms.Label
    $Label3.Location = New-Object System.Drawing.Size(20,130)
    $Label3.Size = New-Object System.Drawing.Size(680,20)
    $Label3.Text = "Change sleep time in minutes (Should be longer then Screen Saver timeout):"
    $Form.Controls.Add($Label3)

    $TextBox3 = New-Object System.Windows.Forms.TextBox
    $TextBox3.Name='Text3'
    $TextBox3.Location = New-Object System.Drawing.Size(20,150)
    $TextBox3.Size = New-Object System.Drawing.Size(260,50)
    $Form.Controls.Add($TextBox3)

    $Label4 = New-Object System.Windows.Forms.Label
    $Label4.Location = New-Object System.Drawing.Size(20,180)
    $Label4.Size = New-Object System.Drawing.Size(360,20)
    $Label4.Text = "Lastly turnoff Hibernate:"
    $Form.Controls.Add($Label4)

    $CheckBox4 = New-Object System.Windows.Forms.CheckBox
    $CheckBox4.Name='$CheckBox4'
    $CheckBox4.Location = New-Object System.Drawing.Size(20,180)
    $CheckBox4.Size = New-Object System.Drawing.Size(260,50)
    $Form.Controls.Add($CheckBox4)

    if($Form.ShowDialog() -eq 'Ok' ){
        
        If ([string]::IsNullOrWhiteSpace($TextBox1.Text)) {}
        Else {powercfg -Change -monitor-timeout-ac $TextBox1.Text}


        If ([string]::IsNullOrWhiteSpace($TextBox2.Text)) {}
        Else {$TextBox2.Text = [int]$TextBox2.Text * 60
              reg add "HKLM\Software\Policies\Microsoft\Windows\Control Panel\Desktop" /v ScreenSaveTimeOut /t      REG_SZ /d $TextBox2.Text /f | Out-Null
              }

        If ([string]::IsNullOrWhiteSpace($TextBox3.Text)) {}
        Else {powercfg -Change -standby-timeout-ac $TextBox3.Text}

        If ($CheckBox4.Checked -eq $true) {powercfg -Hibernate Off}

            
    }else{
           Exit
         }
    Kudos to JRV for lighting the way....

    Wednesday, March 5, 2014 6:14 PM
  • There is an easier way to pop a question.....

    Just an idea...

    $title = "Messagebox Title"
    
    $message = "This is where you pop the big question. `nThere is enough space to write a full story so don't hold back. `n`n Do you understand?"
    
    $yes = New-Object System.Management.Automation.Host.ChoiceDescription "Yes, I do ", "Yes, I fully understand the concept."
    
    $no = New-Object System.Management.Automation.Host.ChoiceDescription "No, I don't ", "No, I don't get it."
    
    $idontknow = New-Object System.Management.Automation.Host.ChoiceDescription "I don't know", "I have no idea what I just read!!"
    
    $options = [System.Management.Automation.Host.ChoiceDescription[]]($yes, $no, $idontknow)
    
    $result = $host.ui.PromptForChoice($title, $message, $options, 0) 
    
    switch ($result)
        {
            0 {"You selected Yes."}
            1 {"You selected No."}
            2 {"You selected I don't know."}
        }

     
    Wednesday, March 5, 2014 6:32 PM
  • There is always one kid on the block that comes along in the middle of a conversation and tries to supply all of the answers that were just rejected for cause.

    gaff clearly doesn'tlike to read questions and did not even read the very first two answers. 

    This topic has nothing to do with PowerShell at a command prompt guy.  This is a discussion about very complex admin issues and not about how to answer homeqwork questions.

    Go back and read it.  I guarantee you will learn a lot.


    ¯\_(ツ)_/¯

    Wednesday, March 5, 2014 6:37 PM