How to use the textbox to gather input for a variable?

Ask questions about creating Graphical User Interfaces (GUI) in PowerShell and using WinForms controls.
Forum rules
Do not post any licensing information in this forum.
User avatar
SystemsEngWookie
Posts: 5
Joined: Tue Jun 06, 2017 6:13 am

How to use the textbox to gather input for a variable?

Post by SystemsEngWookie » Fri Jul 14, 2017 2:26 pm

Why is it that I can get this to give me at least the text input from textbox1.... but not from Textbox2?

Code: Select all

$OKButton.Add_Click({ $script:xServerName = $objServer_TextBox1.Text; $xService = $objTextBox2.Text; $objForm.Close() })
I cannot even get the input to store in the variables using:

Code: Select all

$OKButton.Add_Click({ $xServerName = $objServer_TextBox1.Text; $xService = $objTextBox2.Text; $objForm.Close() })

Here is my snippet of code:
I'm just trying to test this... trying to get it to where a user can enter text from a GUI textbox... then that will plug into the code we need... for instance to restart a service on a server or remote pc.

Code: Select all

<#
.NOTES
===========================================================================
Created with: SAPIEN Technologies, Inc., PowerShell Studio 2017 v5.4.140
Created on: 7/14/2017 9:00 AM
Created by: cvm211
Organization:
Filename:
===========================================================================
.DESCRIPTION
A description of the file.
#>
[void][System.Reflection.Assembly]::LoadWithPartialName("System.Drawing")
[void][System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")

New-Variable -Name "xServerName" -Description "This is to input server names from textbox input"
New-Variable -Name "xService" -Description "This is to input service names from textbox input"

$Credential = $Host.ui.PromptForCredential("Need credentials", "Please enter your user name and password.", "", "NetBiosUserName")


$objForm = New-Object System.Windows.Forms.Form

$objForm.Text = "Service To Restart?"
$objForm.Size = New-Object System.Drawing.Size(300, 350)
$objForm.StartPosition = "CenterScreen"

$objForm.KeyPreview = $True

$OKButton = New-Object System.Windows.Forms.Button
$OKButton.Location = New-Object System.Drawing.Size(75, 250)
$OKButton.Size = New-Object System.Drawing.Size(75, 23)
$OKButton.Text = "OK"
#$objServer_TextBox.TabIndex = 2
$OKButton.Add_Click({ $xServerName = $objServer_TextBox1.Text; $xService = $objTextBox2.Text; $objForm.Close() })
$objForm.Controls.Add($OKButton)
$objForm.Add_KeyDown({
if ($_.KeyCode -eq "Enter")
{
$script:xServerName = $objServer_TextBox1.Text; $xService = $objTextBox2.Text; $objForm.Close()
}
})

$CancelButton = New-Object System.Windows.Forms.Button
$CancelButton.Location = New-Object System.Drawing.Size(150, 250)
$CancelButton.Size = New-Object System.Drawing.Size(75, 23)
$CancelButton.Text = "Cancel"
#$objServer_TextBox.TabIndex = 3
$CancelButton.Add_Click({ $objForm.Close() })
$objForm.Controls.Add($CancelButton)
$objForm.Add_KeyDown({
if ($_.KeyCode -eq "Escape")
{ $objForm.Close() }
})

#This creates a label for the Server_TextBox
$objServer_Label = New-Object System.Windows.Forms.Label
$objServer_Label.Location = New-Object System.Drawing.Size(10, 20)
$objServer_Label.Size = New-Object System.Drawing.Size(280, 20)
$objServer_Label.Text = "Enter the ServerName or IP:"
$objForm.Controls.Add($objServer_Label)

#This creates the TextBox1
$objServer_TextBox1 = New-Object System.Windows.Forms.TextBox
$objServer_TextBox1.Location = New-Object System.Drawing.Size(10, 40)
$objServer_TextBox1.Size = New-Object System.Drawing.Size(260, 20)
#$objServer_TextBox.TabIndex = 0
$objForm.Controls.Add($objServer_TextBox1)

#This creates a label for the TextBox2
$objService_Label2 = New-Object System.Windows.Forms.Label
$objService_Label2.Location = New-Object System.Drawing.Size(10, 70)
$objService_Label2.Size = New-Object System.Drawing.Size(280, 20)
$objService_Label2.Text = "Service Name needing restarted:"
$objForm.Controls.Add($objService_Label2)

#This creates the TextBox2
$objService_TextBox2 = New-Object System.Windows.Forms.TextBox
$objService_TextBox2.Location = New-Object System.Drawing.Size(10, 90)
$objService_TextBox2.Size = New-Object System.Drawing.Size(260, 20)
#$objService_TextBox2.TabIndex = 1
$objForm.Controls.Add($objService_TextBox2)

$objForm.Topmost = $True

$objForm.Add_Shown({ $objForm.Activate() })
[void]$objForm.ShowDialog()

Write-Host $xfortest

Write-Host $xServerName

Write-Host $xService

#$service = Get-WmiObject -ComputerName $xServerName -Class Win32_Service ` -Filter "Name=$xService" -Credential $Credential

#$service

#for testing use
# Qbooks02
# CryptSvc
To help you better we need some information from you.

*** Please fill in the fields below. If you leave fields empty or specify 'latest' rather than the actual version your answer will be delayed as we will be forced to ask you for this information. ***

Product, version and build:
32 or 64 bit version of product:
Operating system:
32 or 64 bit OS:

*** Please add details and screenshots as needed below. ***

DO NOT POST SUBSCRIPTIONS, KEYS OR ANY OTHER LICENSING INFORMATION IN THIS FORUM

User avatar
SystemsEngWookie
Posts: 5
Joined: Tue Jun 06, 2017 6:13 am

Re: How to use the textbox to gather input for a variable?

Post by SystemsEngWookie » Fri Jul 14, 2017 2:36 pm

Just to be clear, I have tried reading the post and article on Sapien and even Spiceworks. Nothing tells me how a textbox value is stored into a variable and then pasted into an argument or function.

The variables that I use keep having empty/null values.

User avatar
davidc
Posts: 4883
Joined: Thu Aug 18, 2011 4:56 am

Re: How to use the textbox to gather input for a variable?

Post by davidc » Fri Jul 14, 2017 3:06 pm

[TOPIC MOVED TO POWERSHELL GUIS FORUM BY MODERATOR]
David
SAPIEN Technologies, Inc.

User avatar
davidc
Posts: 4883
Joined: Thu Aug 18, 2011 4:56 am

Re: How to use the textbox to gather input for a variable?

Post by davidc » Fri Jul 14, 2017 3:10 pm

It looks like you are running into a scoping issue. Refer to the following articles:

https://www.sapien.com/blog/2013/04/10/ ... revisited/
https://info.sapien.com/index.php/guis/ ... ll-gui-app

As for passing and return values:

https://info.sapien.com/index.php/guis/ ... sing-forms
David
SAPIEN Technologies, Inc.

User avatar
jvierra
Posts: 11265
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: How to use the textbox to gather input for a variable?

Post by jvierra » Fri Jul 14, 2017 3:18 pm

This is where well formatted code can help you spot errors:

Here is what you are using but formatted so it is readable:

Code: Select all

$OKButton.Add_Click({
$script:xServerName = $objServer_TextBox1.Text
$xService = $objTextBox2.Text
$objForm.Close()
})
I think you can now see what is wrong. You just forgot to declare the scope.

User avatar
jvierra
Posts: 11265
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: How to use the textbox to gather input for a variable?

Post by jvierra » Fri Jul 14, 2017 3:29 pm

SystemsEngWookie wrote:
Fri Jul 14, 2017 2:36 pm
Just to be clear, I have tried reading the post and article on Sapien and even Spiceworks. Nothing tells me how a textbox value is stored into a variable and then pasted into an argument or function.

The variables that I use keep having empty/null values.
In forms the controls are the variables. We use them directly when needed. To persist variables after the form closes be sure to have a copy of the form variable at the scope the form was created from then you can extract any control values by control name.

Example:

If the form is created as $form1 then it will have this:

$form1.Controls['textbox1'].Text

Thus assumes you have assigned the "Name" property as "testbox1".

PowerShell Studio does not persist the form object but we can in the forms "load" event.

$global:form1 = $form1

We now have a reference to the form that will exist after the form has closed.

User avatar
SystemsEngWookie
Posts: 5
Joined: Tue Jun 06, 2017 6:13 am

Re: How to use the textbox to gather input for a variable?

Post by SystemsEngWookie » Mon Jul 17, 2017 9:21 am

Thanks, I will read those suggested articles.

What I think I'm seeing is that once the form has closed it is passing the variable. Therefore, I will have to pass the variable from form1.textbox to another variable that has a scope outside of that form? Sorry, I am completely new to GUI/Forms in Powershell.

User avatar
jvierra
Posts: 11265
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: How to use the textbox to gather input for a variable?

Post by jvierra » Mon Jul 17, 2017 9:37 am

If you are going to use forms then it is critical that you become proficient in PowerShell and also in Windows Forms. Search for blogs that discuss using FOrms and rad the MS documentation for forms and controls.

You can find many books on PowerShell that will teach you how to correctly use the scripting environment and commands. It is highly unlikely that you will learn ths at a technical level by guessing or copying code. That is usually only possible if you are already an experienced technical programmer.

Here is one good resource for learning technical PowerShell: https://www.sapien.com/books_training/W ... werShell-4

User avatar
SystemsEngWookie
Posts: 5
Joined: Tue Jun 06, 2017 6:13 am

Re: How to use the textbox to gather input for a variable?

Post by SystemsEngWookie » Mon Jul 17, 2017 9:40 am

jvierra wrote:
Fri Jul 14, 2017 3:18 pm
This is where well formatted code can help you spot errors:

Here is what you are using but formatted so it is readable:

Code: Select all

$OKButton.Add_Click({
$script:xServerName = $objServer_TextBox1.Text
$xService = $objTextBox2.Text
$objForm.Close()
})
I think you can now see what is wrong. You just forgot to declare the scope.
Not sure that I do see it. I'm very new to this...

User avatar
SystemsEngWookie
Posts: 5
Joined: Tue Jun 06, 2017 6:13 am

Re: How to use the textbox to gather input for a variable?

Post by SystemsEngWookie » Mon Jul 17, 2017 9:42 am

jvierra wrote:
Mon Jul 17, 2017 9:37 am
If you are going to use forms then it is critical that you become proficient in PowerShell and also in Windows Forms. Search for blogs that discuss using FOrms and rad the MS documentation for forms and controls.

You can find many books on PowerShell that will teach you how to correctly use the scripting environment and commands. It is highly unlikely that you will learn ths at a technical level by guessing or copying code. That is usually only possible if you are already an experienced technical programmer.

Here is one good resource for learning technical PowerShell: https://www.sapien.com/books_training/W ... werShell-4
Not new to powershell... just new to GUI programming area.

Locked