Query a form controls BackColor?

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 6 years and 5 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
iamtj69
Posts: 11
Last visit: Thu Jun 23, 2022 2:47 am

Query a form controls BackColor?

Post by iamtj69 »

Hello,

I am writing a GUI using PowerShell Studio 2017 and i have a form with several controls on it.

I am setting the BackColor attribute of each mandatory control to PaleGreen if certain conditions are not met (validation). This all works Fine.

I would like to then run a function that will then simply check a list of controls and report if any are currently set to $control.Backcolor = "PaleGreen". I am having trouble writing this funciton.

Here is what i have so far:

Code: Select all

Function validateMandatoryControlsCompleted()
{
	$ControlList = @("`$textbox1",`
					"`$textbox2",`
					"`$textbox3",`
					"`$textbox4",`
					"`$combobox1",`
					"`$maskedtextbox1",`
					"`$listbox1",`
					"`$maskedtextbox2")
	
	foreach ($Control in $ControlList)
	{
		$ControlAttribute = $Control + ".BackColor"
		
		If ($ControlAttribute = "PaleGreen")
		{
			Write-Host "$Control is a mandatory field"
		}
		Else
		{
			Write-Host "$Control is not required"
		}
	}
	
	
}
Am i going about this the right way? Any help would be much appreciated.

Thanks
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Query a form controls BackColor?

Post by jvierra »

Well - not a really good guess. Here is how it works.

Code: Select all

Function validateMandatoryControlsCompleted() {
	$controlList = @(
		$textbox1,`
		$textbox2,
		$textbox3,
		$textbox4,
		$combobox1,
		$maskedtextbox1,
		$listbox1,
		$maskedtextbox2
	)
	
	foreach ($control in $controlList) {
		$control.BackColor
		#Write-Host $control.BackColor	
		If ($control.BackColor -eq 'PaleGreen') {
			Write-Host $control.Name ' is a mandatory field'
		} Else {
			Write-Host $control.Name ' is not required'
		}
    }
}
You can also just do this:

Code: Select all

$form1.Controls | 
	Where-Object{ $_.BackColor -eq 'PaleGreen' } |
	ForEach-Object{ Write-Host $_.Name 'is a mandatory field' }
Normally we would use the "Tag" property to store almost anything like status or error message... an object with lots of information can be used.

Variables, by convention, always start with a lowercase letter and are Pascal case. Functions and properties are full Pascal case.

Comparisons are -eq,-ne, -gt,-lt .... and NOT the "=" sign.
User avatar
iamtj69
Posts: 11
Last visit: Thu Jun 23, 2022 2:47 am

Re: Query a form controls BackColor?

Post by iamtj69 »

Perfect! Thank you.
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Query a form controls BackColor?

Post by jvierra »

Normally we would handle errors like this: https://info.sapien.com/index.php/guis/ ... er-control
This topic is 6 years and 5 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