title: How to handle label size and location in the GUI form using PowerShell?
body: <p>I create a simple GUI. I want to execute the GUI in any resolution screen. In my work screen, it show picture 1. Once I execute it in other screen, the size label, position is different, showed in the picture 2.
  Anyone can help me please how to handle this? Thank you so much.</p>
  <p>Picture 1
  <a href="https://i.stack.imgur.com/1rCUR.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/1rCUR.png" alt="enter image description here" /></a></p>
  <p>Picture 2
  <a href="https://i.stack.imgur.com/umPri.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/umPri.png" alt="enter image description here" /></a></p>
  <pre><code>Add-Type -AssemblyName System.Windows.Forms
  [System.Windows.Forms.Application]::EnableVisualStyles()
  
  $Form                            = New-Object system.Windows.Forms.Form
  $Form.ClientSize                 = '400,400'
  $Form.text                       = &quot;Form&quot;
  $Form.TopMost                    = $false
  $form.AutoScale                  = $true
  $form.AutoScaleMode              = &quot;Font&quot;
  $Form.FormBorderStyle            = &quot;FixedDialog&quot;
  
  $Groupbox1                       = New-Object system.Windows.Forms.Groupbox
  $Groupbox1.height                = 362
  $Groupbox1.width                 = 367
  $Groupbox1.text                  = &quot;Group Box&quot;
  $Groupbox1.location              = New-Object System.Drawing.Point(15,21)
  
  $Label1                          = New-Object system.Windows.Forms.Label
  $Label1.text                     = &quot;Do you need handling the job?&quot;
  $Label1.AutoSize                 = $true
  $Label1.width                    = 25
  $Label1.height                   = 10
  $Label1.location                 = New-Object System.Drawing.Point(92,82)
  $Label1.Font                     = 'Microsoft Sans Serif,10'
  
  $Button1                         = New-Object system.Windows.Forms.Button
  $Button1.text                    = &quot;Yes&quot;
  $Button1.width                   = 60
  $Button1.height                  = 30
  $Button1.location                = New-Object System.Drawing.Point(34,263)
  $Button1.Font                    = 'Microsoft Sans Serif,10'
  
  $Button2                         = New-Object system.Windows.Forms.Button
  $Button2.text                    = &quot;No&quot;
  $Button2.width                   = 60
  $Button2.height                  = 30
  $Button2.location                = New-Object System.Drawing.Point(268,263)
  $Button2.Font                    = 'Microsoft Sans Serif,10'
  
  $Form.controls.AddRange(@($Groupbox1))
  $Groupbox1.controls.AddRange(@($Label1,$Button1,$Button2))
  
  
  [void]$Form.ShowDialog()
  </code></pre>
  <h3>updated</h3>
  <p>I try this</p>
  <pre><code>Add-Type -AssemblyName System.Windows.Forms
  [System.Windows.Forms.Application]::EnableVisualStyles()
  $Form = New-Object system.Windows.Forms.Form
  $Form.FormBorderStyle = &quot;FixedDialog&quot;
  $Width = [System.Windows.Forms.Screen]::AllScreens.bounds.width
  $Heigt = [System.Windows.Forms.Screen]::AllScreens.bounds.height
  
  $Widht_Form = $Width[0] / 3.5
  Write-Host &quot;$Widht_Form&quot;
  $Heigt_Form = $Heigt[0] / 1.8
  Write-Host &quot;$Heigt_Form&quot;
  $Form.Width = $Widht_Form
  $Form.Height = $Heigt_Form
  
  $label1 = New-Object 'System.Windows.Forms.Label'
  $Yes = New-Object 'System.Windows.Forms.Button'
  $No = New-Object 'System.Windows.Forms.Button'
  $Title = New-Object 'System.Windows.Forms.Label'
  $timer1 = New-Object 'System.Windows.Forms.Timer'
  $InitialFormWindowState = New-Object 'System.Windows.Forms.FormWindowState'
  
  $Form_Load = {  
      $TotalTime = 10 #in seconds
          $script:StartTime = (Get-Date).AddSeconds($TotalTime)
          #Start the timer
          $timer1.Start()
  }   
  $Cancel_Click={ 
  }
  $timer1_Tick={
          #Use Get-Date for Time Accuracy
          [TimeSpan]$span = $script:StartTime - (Get-Date)
          #Update the display
          $Form.Text = $label1.Text = &quot;{0:N0}&quot; -f $span.TotalSeconds
          if ($span.TotalSeconds -le 0) {
              $timer1.Stop()
              $Form.Close()
          }
      }
  
  $Form_StateCorrection_Load=
  {
      $Form.WindowState = $InitialFormWindowState
  }
  
  $Form_Cleanup_FormClosed=
  {
      try
      {
          # $Cancel.remove_Click($Cancel_Click)
          $Form.remove_Load($Form_Load)
          $timer1.remove_Tick($timer1_Tick)
          $Form.remove_Load($Form_StateCorrection_Load)
          $Form.remove_FormClosed($Form_Cleanup_FormClosed)
      }
      catch [Exception]
      { }
  }
  
  $Form.SuspendLayout()
  $Form.Controls.Add($label1)
  $Form.Controls.Add($Yes)
  $Form.Controls.Add($No)
  $Form.Controls.Add($Title)
  
  $Form.StartPosition = &quot;CenterScreen&quot;
  $Form.BackColor = &quot;#f6f6f6&quot;
  $Form.add_Load($Form_Load)
  
  $label1.Font = 'Microsoft Sans Serif,20,style=Bold'
  $Label1.ForeColor = &quot;#176faa&quot;
  $label1.AutoSize = $true
  $label1.width = 25
  $label1.height = 10
  $label1_height = $Heigt_Form / 2.5
  $label1_width = $Widht_Form / 2.2
  $label1.location = New-Object System.Drawing.Point($label1_width,$label1_height)
  
  $Title.Text = &quot;Do you need handling the job?&quot;
  $Title.ForeColor = &quot;#176faa&quot;
  $Title.Font = 'Microsoft Sans Serif,16,style=Bold'
  $Title.AutoSize = $true
  $Title.width = 25
  $Title.height = 10
  $Title_height = $Heigt_Form / 5
  $Title_width = $Widht_Form / 5
  $Title.location = New-Object System.Drawing.Point($Title_width,$Title_height)
  
  $Yes.AutoSize = $true
  $Yes_height = $Heigt_Form * 0.7
  $Yes_width = $Widht_Form / 8
  $Yes.Location = New-Object System.Drawing.Size($Yes_width,$Yes_height)
  $Yes.Size = New-Object System.Drawing.Size(90,35)
  $Yes.Text = &quot;Yes&quot;
  $Yes.Add_Click(
      {
      Write-Host &quot;Call GUI Control&quot;
      Start-Sleep -s 1
      $Form.Close()
      }
  )
  
  $No.AutoSize = $true
  $No_height = $Heigt_Form * 0.7
  $No_width = ($Yes_width * 6) - 35
  $No.Location = New-Object System.Drawing.Size($No_width,$No_height)
  $No.Size = New-Object System.Drawing.Size(90,35)
  # $No.BackColor = &quot;#9fd5f3&quot;
  $No.Text = &quot;No&quot;
  $No.Add_Click(
      {
      Write-Host &quot;Continue the process&quot;
      $Form.Close()
      }
  )
  
  $timer1.add_Tick($timer1_Tick)
  $Form.ResumeLayout()
  
  #Save the initial state of the form
  $InitialFormWindowState = $Form.WindowState
  #Init the OnLoad event to correct the initial state of the form
  $Form.add_Load($Form_StateCorrection_Load)
  #Clean up the control events
  $Form.add_FormClosed($Form_Cleanup_FormClosed)
  #Show the Form
  return $Form.ShowDialog()
  </code></pre>
  <p>This way alson mesh up the label and button once execute in the different screen</p>