change ForeColor on textbox dynamic

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 1 year and 3 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
lontru
Posts: 103
Last visit: Thu Mar 07, 2024 2:00 pm

change ForeColor on textbox dynamic

Post by lontru »

Hi

I want to change the .ForeColor on conditions

Have 6 checkbox from
checkbox_Log1..6
and
textbox_Log1..6

I got this working so far getting info - but i want to change a property based on test-connection
trying to Set-Variable -Name "textbox_Log$($item).ForeColor" -Value "Green"

when running
$text = Get-Variable -Name "textbox_Log$($item)" -ValueOnly
Write-Host "$text"
getting this only where is the ForeColor property hidden?
System.Windows.Forms.TextBox, Text: *the textbox content.text*

Code: Select all

function get-checkboxLog
{
	[System.Collections.ArrayList]$clients = @()
	$checkbox_log = 1 .. 6
	foreach ($item in $checkbox_log)
	{	
		$statuslog = Get-Variable -Name "checkbox_Log$($item)" -ValueOnly
		if ($($statuslog.CheckState) -eq 'Checked')
		{
			$clientname = Get-Variable -Name "textbox_Log$($item)" -ValueOnly
			$client = $($clientname.Text)
			if (-not ([string]::IsNullOrEmpty($client)))
			{
				if ($clients -notcontains $client)
				{
					if (Test-Connection -Cn $client -Count 1 -ea 0 -quiet)
					{
						$clients.Add("$client") | Out-Null
						$text = Get-Variable -Name "textbox_Log$($item)" -ValueOnly
						Write-Host "$client Green"
					}
					else
					{
						Set-Variable -Name "textbox_Log$($item).ForeColor" -Value 'Red'
						Write-Host "$client Red"
					}
				}
			}
		}
	}
	return $clients
}
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: change ForeColor on textbox dynamic

Post by jvierra »

To set the property of a control we would do this:

$textbox_Log,ForeColor = 'Red'
User avatar
lontru
Posts: 103
Last visit: Thu Mar 07, 2024 2:00 pm

Re: change ForeColor on textbox dynamic

Post by lontru »

$textbox_Log.ForeColor = 'Red'

yes but is there a way so i can do it this way

$collection = 1..6
foreach ($item in $collection)
{
$textbox_Log($item).ForeColor = 'Red' # this give me an error
}
User avatar
lontru
Posts: 103
Last visit: Thu Mar 07, 2024 2:00 pm

Re: change ForeColor on textbox dynamic

Post by lontru »

added a switch but to do my forecolor change. it works but if there isnt any other ways

Code: Select all

			if (Test-Connection -Cn $client -Count 1 -ea 0 -quiet)
					{
						# online
						$clients.Add("$client") | Out-Null
						switch ($item) {
							1 {
								$textbox_Log1.ForeColor = 'Green'
							}
							2 {
								$textbox_Log2.ForeColor = 'Green'
							}
							3 {
								$textbox_Log3.ForeColor = 'Green'
							}
							4 {
								$textbox_Log4.ForeColor = 'Green'
							}
							5 {
								$textbox_Log5.ForeColor = 'Green'
							}
							6 {
								$textbox_Log6.ForeColor = 'Green'
							}
							default {
								#<code>
							}
						}
					}
User avatar
lontru
Posts: 103
Last visit: Thu Mar 07, 2024 2:00 pm

Re: change ForeColor on textbox dynamic

Post by lontru »

complete solution

Code: Select all

function get-checkboxLog
{
	[System.Collections.ArrayList]$clients = @()
	$checkbox_log = 1 .. 6
	foreach ($item in $checkbox_log)
	{	
		$statuslog = Get-Variable -Name "checkbox_Log$($item)" -ValueOnly
		if ($($statuslog.CheckState) -eq 'Checked')
		{
			$clientname = Get-Variable -Name "textbox_Log$($item)" -ValueOnly
			$client = $($clientname.Text)
			if (-not ([string]::IsNullOrEmpty($client)))
			{
				if ($clients -notcontains $client)
				{
					if (Test-Connection -Cn $client -Count 1 -ea 0 -quiet)
					{
						# online
						$clients.Add("$client") | Out-Null
						switch ($item) {
							1 {
								$textbox_Log1.ForeColor = 'Green'
							}
							2 {
								$textbox_Log2.ForeColor = 'Green'
							}
							3 {
								$textbox_Log3.ForeColor = 'Green'
							}
							4 {
								$textbox_Log4.ForeColor = 'Green'
							}
							5 {
								$textbox_Log5.ForeColor = 'Green'
							}
							6 {
								$textbox_Log6.ForeColor = 'Green'
							}
							default {
								#<code>
							}
						}
					}
					else
					{
						switch ($item)
						{
							1 {
								$textbox_Log1.ForeColor = 'Red'
							}
							2 {
								$textbox_Log2.ForeColor = 'Red'
							}
							3 {
								$textbox_Log3.ForeColor = 'Red'
							}
							4 {
								$textbox_Log4.ForeColor = 'Red'
							}
							5 {
								$textbox_Log5.ForeColor = 'Red'
							}
							6 {
								$textbox_Log6.ForeColor = 'Red'
							}
							default
							{
								#<code>
							}
						}
					}
				}
			}
		}
	}
	return $clients
}
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: change ForeColor on textbox dynamic

Post by jvierra »

Ok. Now I see what you were asking. It was very hard to understand what your basic question was. Yes, that method will work.

There are other ways to do this with table controls or group boxes. When I want to sync two controls, I will often store one control object in the "Tag" property of the paired control. By grouping you can just enumerate the child controls in a pipeline. This simplifies the code and creates the "strong" relationship needed between the paired controls.

Your method works but takes a lot more code and is slower. For a small number of controls speed is not an issue. Code simplicity is always an issue for other coders and for debugging and code maintenance. For simple utilities your method is acceptable.

Note that my suggestions would reduce the code to less than 10 lines.
This topic is 1 year and 3 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