1

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?

Naigel
  • 9,086
  • 16
  • 65
  • 106

2 Answers2

4

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"
Andy Arismendi
  • 50,577
  • 16
  • 107
  • 124
  • +1 - Supplemental link for further explanation: http://stackoverflow.com/questions/7892090/how-to-set-winform-start-position-at-top-right – dugas Apr 02 '13 at 14:47
  • Thank you! I found another easier solution, just setting `StartPosition` properly `$form.StartPosition = "manual"` – Naigel Apr 02 '13 at 14:51
  • Add that as an answer, it's probably more appropriate solution. – Andy Arismendi Apr 02 '13 at 14:58
0

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()
Ste
  • 1,729
  • 1
  • 17
  • 27