trying to get a function loaded from a PSM1 to write to a PSF-defined text box

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.

Any code longer than three lines should be added as code using the 'Select Code' dropdown menu or attached as a file.
Post Reply
User avatar
fvenezia65
Posts: 2
Joined: Sun Mar 03, 2013 12:42 pm

trying to get a function loaded from a PSM1 to write to a PSF-defined text box

Post by fvenezia65 » Wed Nov 07, 2018 1:12 pm

Product, version and build: Powershell studio 2018 / Powershell version 5.0.10586.117
32 or 64 bit version of product: 64-bit
Operating system: Win-7 Pro
32 or 64 bit OS: 64-bit

Hi.
I am trying to enable some functions i've defined in a PSM1 module that is loaded with a Posh GUI Form to write back to a text box on the main form. But i am unable to do so.

I can write to this text box from the code in the .PSF file that i am creating a 32-bit EXE from; however i am unable to get a function that loaded via a PSM1, when the form loads with an import-module, to be able to write to the form's text-box (for function status updates).

FYI - this product is awesome.

Thanks for any help!
-frank

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

Re: trying to get a function loaded from a PSM1 to write to a PSF-defined text box

Post by davidc » Fri Nov 09, 2018 7:49 am

[TOPIC MOVED TO POWERSHELL GUIS FORUM]

If you can provide an example, we will be able to assist you further. Typically, you want to use the Out-String to set the Text of the textbox control.

Code: Select all

$textBox1.Text = Get-Process | Out-String
I recommend looking at the "TextBox - Get Process" control set (located in Tools Panel->Control Sets tab).

More info on TextBox control:

https://info.sapien.com/index.php/guis/gui-controls/spotlight-on-the-textbox-control
David
SAPIEN Technologies, Inc.

User avatar
fvenezia65
Posts: 2
Joined: Sun Mar 03, 2013 12:42 pm

Re: trying to get a function loaded from a PSM1 to write to a PSF-defined text box

Post by fvenezia65 » Tue Nov 20, 2018 10:34 am

Hi, David.
Thanks for the reply.
I believe my issue is one of variable-scope.
I *am* able to update the textbox from the main .PSF compiled EXE GUI.
my issue occurs in a function defined in a PSM1 module that is loaded in the PSF.
when i call any function in that PSM1, i am unable to write back into that textbox from the main .PSF routine.

a .psf defines the textbox and imports a .PSM1 with our functions.

Code: Select all

$richtextbox1.Text = "Loading the NwHS-NOC-Utils PoSh Module."
Import-Module -Force "path\PowerShell-scripts\NWHS-NOC-Utils.psm1"
when we call one of the imported-functions, it generates an error and doesn't update the text-field, when we try to write to that textbox defined in the .PSF.

Code: Select all

	if ($OnScreen)
		{
		"$LogTime `t $EventToLog"
		$global:richtextbox1.Text = "$LogTime `t $EventToLog"
		}
Thanks for any help/thoughts on this one.
-frank

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

Re: trying to get a function loaded from a PSM1 to write to a PSF-defined text box

Post by davidc » Tue Nov 20, 2018 10:48 am

If the variable is part of a module (and the psf is separate from the module) then the module will have to export the variable. You have to do this because modules have their own variable scope.

Typically, if you want to reference a variable across functions or form events, you will need to specify the script scope for that variable:

Code: Select all

$script:variable = 'value'
David
SAPIEN Technologies, Inc.

Post Reply