Naigel
Naigel

Reputation: 9644

Powershell and .NET form object

I need to create a basic "helpdesk style" tool that uses some powershell scripts. I'm using .NET form object to create a window, but I can't set properly the Localtion attribute (and other attributes that need a Point object)

$form = New-Object system.Windows.Forms.Form;
$form.AutoSize = $true;
$form.minimumSize = New-Object System.Drawing.Size(400, 300);
$form.Location = New-Object System.Drawing.Point(10, 10);
$form.DataBindings.DefaultDataSourceUpdateMode = 0;

$form.ShowDialog();

The window form appears, dimensions are correct, but position is wrong. Am I missing something?

Upvotes: 1

Views: 2864

Answers (2)

Ste
Ste

Reputation: 2293

You don't need to specify a new object for the position or sizes. As added by Andy the StartPostion property is what you need to change to Manual. Then simply give the Location property a string value like 'x, y'.

Add-Type -AssemblyName System.Windows.Forms

$Form               = New-Object system.Windows.Forms.Form
$Form.AutoSize      = $true
$Form.minimumSize   = '800, 300'
$Form.StartPosition = 'Manual'
$Form.Location      = '500, 500'

$Form.ShowDialog()

Upvotes: 0

Andy Arismendi
Andy Arismendi

Reputation: 52609

You can change the location property in the Load event:

$handler_form_Load = {
    $form.Location = New-Object System.Drawing.Point(10, 10);
}

$form = New-Object system.Windows.Forms.Form;
$form.AutoSize = $true;
$form.minimumSize = New-Object System.Drawing.Size(400, 300);
$form.add_Load($handler_form_Load)
$form.DataBindings.DefaultDataSourceUpdateMode = 0;

$form.ShowDialog();

Also, as you found @Lorenzo, set the StartPosition to manual to honor the location property on load, so the event handler above isn't needed.

$form.StartPosition = "manual"

Upvotes: 4

Related Questions