title: How to set keyboard focus to a TextBox in Powershell
body: <p>I'm looking to find out how to automatically set the keyboard focus to a text box in powershell. </p>
  
  <p>I have a script that asks the user to select an option from a dropdown menu, then based on that selection, they have to input certain parameters. That all works fine.</p>
  
  <p>For ease of use, I'd like the focus of the keyboard to shift to the input box each time a new one is shown, so the user doesn't have to keep clicking on it to enter some text.</p>
  
  <p>My code so far : </p>
  
  <pre><code>function inputBox($parameter)
  {
  $objForm = New-Object System.Windows.Forms.Form 
  $objForm.Text = $parameter
  $objForm.Size = New-Object System.Drawing.Size(300,200) 
  $objForm.StartPosition = "CenterScreen"
  
  $objForm.KeyPreview = $True
  $objForm.Add_KeyDown({if ($_.KeyCode -eq "Enter") 
      {$paramValue=$objTextBox.Text;$objForm.Close()}})
  $objForm.Add_KeyDown({if ($_.KeyCode -eq "Escape") 
      {$objForm.Close()}})
  
  
  
  $OKButton = New-Object System.Windows.Forms.Button
  $OKButton.Location = New-Object System.Drawing.Size(75,90)
  $OKButton.Size = New-Object System.Drawing.Size(75,25)
  $OKButton.Text = "OK"
  $OKButton.Add_Click({$paramValue=$objTextBox.Text;$objForm.Close()})
  $objForm.Controls.Add($OKButton)
  
  $objLabel = New-Object System.Windows.Forms.Label
  $objLabel.Location = New-Object System.Drawing.Size(10,20) 
  $objLabel.Size = New-Object System.Drawing.Size(280,20) 
  $objLabel.Text = $parameter
  $objForm.Controls.Add($objLabel) 
  
  $objTextBox = New-Object System.Windows.Forms.TextBox 
  $objTextBox.Location = New-Object System.Drawing.Size(10,40) 
  $objTextBox.Size = New-Object System.Drawing.Size(260,20) 
  $objForm.KeyPreview = $True
  $objForm.Controls.Add($objTextBox) 
  
  $objForm.Topmost = $True
  
  $objForm.Add_Shown({$objForm.Activate()})
  [void] $objForm.ShowDialog()
  
  return $paramValue
  </code></pre>
  
  <p>Any ideas?</p>
  
  <p>Thanks!</p>