Updating multiple textboxes from a Global "write-log" function.

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.
This topic is 2 years and 10 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked
User avatar
brianclark
Posts: 23
Last visit: Wed Feb 22, 2023 5:19 pm

Updating multiple textboxes from a Global "write-log" function.

Post by brianclark »

So I am attempting to write a basic logging script which will output to 1 - 2 different textboxes depending on what tab the task is run from.
I am using a global function and calling it when the task is ran. I can set one of the textboxes to log as it is the overall applications logging textbox, however I have another textbox on the tab the users will be interfacing with, to show their progress, errors, etc..
The issue I am having is passing the textbox into the function as a parameter.
The following is my basic function, where I tried to recreate the $tbx_whatever name of the textbox, however I get an error with this.
"ERROR: Method invocation failed because [System.String] does not contain a method named 'appendtext'."

Code: Select all

function write-Applog
{
	[CmdletBinding()]
	param (
		[Parameter(Mandatory = $true)]
		[ValidateNotNullOrEmpty()]
		[string]$Message,
		[Parameter()]
		[ValidateNotNullOrEmpty()]
		[string]$appTab,
		[Parameter()]
		$localLog
	)
	$LogMessage = [pscustomobject]@{
		Time = (Get-Date -f g)
		Message = $Message
		AppTab = $appTab
	}
	$rtb_Logs_Output.appendtext("[$($LogMessage.Time)]::[$appTab]::$Message`n")
	if ($localLog)
	{
		$localtbx = "`${0}" -f $localLog
		$localtbx.appendText("[$appTab]:$Message`n")
	}
}
How would I reference this function and give pass it the textbox name? Do I somehow reference the form.tab.whatever?
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Updating multiple textboxes from a Global "write-log" function.

Post by jvierra »

The TAB has a collection called "Controls". The controls have names.

$appTab.Controls['rtb_Logs_Output'].AppendText( ... )
User avatar
brianclark
Posts: 23
Last visit: Wed Feb 22, 2023 5:19 pm

Re: Updating multiple textboxes from a Global "write-log" function.

Post by brianclark »

I feel so stupid, I guess I was just overthinking it.
All I had to do was pass the textbox object from the form into the function.

So calling the function looks like this;

Code: Select all

write-Applog -Message "Share Name will be: $stagname." -appTab $($tab_ADGS.Text) -localLog $rtb_ADGS_Output

Code: Select all

function write-bcApplog
{
	[CmdletBinding()]
	param (
		[Parameter(Mandatory = $true)]
		[ValidateNotNullOrEmpty()]
		[string]$Message,
		[Parameter()]
		[ValidateNotNullOrEmpty()]
		[string]$appTab,
		[Parameter()]
		$localLog
	)
	$LogMessage = [pscustomobject]@{
		Time = (Get-Date -f g)
		Message = $Message
		AppTab = $appTab
	}
	$rtb_Logs_Output.appendtext("[$($LogMessage.Time)]::[$appTab]::$Message`n")
	if ($localLog)
	{
		$localLog.appendText("[$appTab]:$Message`n")
	}
}
That will write the output to both locations. Thanks for letting me brain dump here, and for the kick in the right direction.
This topic is 2 years and 10 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked