Quantcast
Channel: The Official Scripting Guys Forum! forum
Viewing all articles
Browse latest Browse all 15028

Using The MessageBox Class in The System.Windows.Forms namespace

$
0
0

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.


Viewing all articles
Browse latest Browse all 15028

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>