Just capture what is in the text box on the button click event.
You don't have to show your whole form, just part of it that is relevant.
You could have just created a simple dialog for the same use case.
What you are asking for is a very common task. There are literally tons of examples and videos on YouTube that show how to do this. As well as on the very site.
What you are asking for is a very common task. See the example here:
Powershell Custom GUI input box for passing values to Variables
I shortened the code for this reply.
function button ($mailbx)
{
###################Load Assembly for creating form & button######
[void][System.Reflection.Assembly]::LoadWithPartialName( “System.Windows.Forms”)
[void][System.Reflection.Assembly]::LoadWithPartialName( “Microsoft.VisualBasic”)
#####Define the form size & placement
$form = New-Object “System.Windows.Forms.Form”;
$form.Width = 500;
$form.Height = 150;
$form.Text = $title;
$form.StartPosition = [System.Windows.Forms.FormStartPosition]::CenterScreen;
##############Define text label1
$textLabel1 = New-Object “System.Windows.Forms.Label”;
$textLabel1.Left = 25;
$textLabel1.Top = 15;
$textLabel1.Text = $mailbx;
############Define text box1 for input
$textBox1 = New-Object “System.Windows.Forms.TextBox”;
$textBox1.Left = 150;
$textBox1.Top = 10;
$textBox1.width = 200;
#############Define default values for the input boxes
$defaultValue = “”
$textBox1.Text = $defaultValue;
#############define OK button
$button = New-Object “System.Windows.Forms.Button”;
$button.Left = 360;
$button.Top = 85;
$button.Width = 100;
$button.Text = “Ok”;
############# This is when you have to close the form after getting values
$eventHandler = [System.EventHandler] {
$textBox1.Text;
$textBox2.Text;
$textBox3.Text;
$form.Close();
};
$button.Add_Click($eventHandler) ;
#############Add controls to all the above objects defined
$form.Controls.Add($button);
$form.Controls.Add($textLabel1);
$form.Controls.Add($textBox1);
$ret = $form.ShowDialog();
#################return values
return $textBox1.Text
}
$return = button 'Enter Folders' 'Enter mailbox'
# Below variables will get the values that had been entered by the user
$return
This is just one approach, there are other ways to capture and return text, for example, not showing any form code, just the return, from another simple dialog WinForm…
$result = $form.ShowDialog()
if ($result -eq [System.Windows.Forms.DialogResult]::OK)
{
($FName = $FName.Text )
($LName = $Lname.Text)
}
The same thing can be done for WPF (Windows Presentation Foundation) style forms as well.