locked
Cannot resize form or objects using powershell windows forms RRS feed

  • Question

  • Hi,

    I am trying to create an interface using PowerShell and Windows Forms.  I cannot seem to position, size or move objects on the

    Snippet1:

    Add-Type -AssemblyName System.Windows.Forms
    Add-Type -AssemblyName System.Drawing
    $form = New-Object System.Windows.Forms.Form
    $form.Text = 'User Requirements Form'
    $form.Size = New-Object System.Drawing.Size(1024,768)
    #$Form.Width = 1024
    #$Form.Height = 768
    $Form.KeyPreview = $True
    $Form.FormBorderStyle = "1"
    # $MainForm.MaximizeBox = $false
    $form.StartPosition = 'CenterScreen'
    $Form.AutoScroll = $True
    $Form.AutoSize = $True
    $Form.AutoSizeMode = "GrowAndShrink"

    None of the following will work and if I resize to 800x600 it still displays the same

    $form.Size = New-Object System.Drawing.Size(1024,768)
    #$Form.Width = 1024
    #$Form.Height = 768 form or the form itself.

    $listBox = New-Object System.Windows.Forms.Listbox
    $listBox.Location = New-Object System.Drawing.Point(321,211)
    $listBox.Location = New-Object System.Drawing.Point(250,200)
    # $listBox.Size = New-Object System.Drawing.Size(260,400)
    # $listBox.StartPosition = "manual"
    # $listBox.Location = New-Object System.Drawing.Size(-450, 200)

    $listBox.Height = 200
    $form.Controls.Add($listBox)
    $form.Topmost = $true

    $result = $form.ShowDialog()

    Is there something I should be doing instead?

    Nigel


    Friday, August 31, 2018 8:53 AM

Answers

  • Formborderstyle stops the form from being resized. Remove or change that :)
    • Marked as answer by trailblazer1 Monday, September 3, 2018 1:31 PM
    Friday, August 31, 2018 8:57 AM

All replies

  • Formborderstyle stops the form from being resized. Remove or change that :)
    • Marked as answer by trailblazer1 Monday, September 3, 2018 1:31 PM
    Friday, August 31, 2018 8:57 AM
  • Here is an example of how to build a form the easy way.

    Add-Type -AssemblyName System.Windows.Forms
    
    $form = New-Object System.Windows.Forms.Form
    $form.Text = 'User Requirements Form'
    $form.Size = '1024,768'
    $Form.KeyPreview = $True
    $form.StartPosition = 'CenterScreen'
    $form.Topmost = $true
    
    $listBox = New-Object System.Windows.Forms.Listbox
    $listBox.Location = '20,10'
    $listBox.Height = 200
    $form.Controls.Add($listBox)
    
    $result = $form.ShowDialog()
    
    

    Try approaching forms a few lines at a time until you understand what the lines actually do.  Forms are very complex and technical.  You must understand why they have the properties and how they are intended  to be used.  Look up each control and read how it works before using it;

    Here is the doc for the "form" control: https://docs.microsoft.com/en-us/dotnet/api/system.windows.forms.form?view=netframework-4.7.2


    \_(ツ)_/

    Friday, August 31, 2018 9:31 AM
  • Hi,

    Thanks for your question.

    I agree with I.T.Delinquent. You should change or remove the Form.BorderStyle Property.

    About Form.BorderStyle Property, you set the "1" value for it. Then form has a thin border and can include any of the border elements. The form isn't resizable (the Size command on the Control menu isn't available).

    If you want to learn more about it, refer the link below.

    https://docs.microsoft.com/en-us/office/vba/api/access.form.borderstyle

    Best Regards,

    Lee


    Just do it.

    Monday, September 3, 2018 6:25 AM