0

I am dot sourcing a script which builds a GUI form.

The user puts a value in a text box, some input validation happens and then if it passes the user can click a button: The Apply button. (e.g to save the value)

So I want that value returned to my script1 as below:

Any recommendations for a neat way to do this?

#Script1.ps1
. ($PSScriptRoot + "\GUIForm.ps1"
InitialiseForm

#When the Ok button is pressed in the GuiForm.ps1 script. 
#Store value to here to parse elsewhere
#GUIForm.ps1
$TheTextIWantFromTextBox = New-Object system.Windows.Forms.TextBox
$OkButton.Add_Click({SubmitTextBox})

Function SubmitTextBox()
{
    $Form.close()
    return $TheTextIWantFromTextBox.Text
}

Function InitialiseForm()
{
    [void]$Form.ShowDialog()
}

Note: I have omitted all the code that creates the form etc...If this is relevant I will update!

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.