Combo Box SelectedIndex Changes Automatically

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 8 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
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Combo Box SelectedIndex Changes Automatically

Post by jvierra »

I do not understand why you have this code: Validate-ComboBox

What does it do and why are you not using the validating event? If you want a common validation method then just assign both boxes to the same event. This makes the code easier to maintain and understand. If you do feel that a function is better then add the errorprovider to the function and call it like this:

$_.Cancel = Validate-ComboBox $this

I simplified your code so it is easier to see what you are trying to do:

Code: Select all

$cmb_Role_Validating = [System.ComponentModel.CancelEventHandler]{
	if ($_.Cancel = Validate-ComboBox -cmbx Role){
		$errorprovider1.SetError($this, 'Please select a Role from the list.')
	} else {
		$errorprovider1.SetError($this, '')
	}
}

$cmb_Dept_Validating = [System.ComponentModel.CancelEventHandler]{
	if ($_.Cancel = Validate-ComboBox -cmbx Dept){
		$errorprovider1.SetError($this, 'Please select a valid Department from the list.')
	} else {
		$errorprovider1.SetError($this,'')
	}
}
You didn't post the Validate code so the event is still a mystery.
User avatar
barnold08
Posts: 22
Last visit: Fri Sep 02, 2022 11:36 am

Re: Combo Box SelectedIndex Changes Automatically

Post by barnold08 »

Thank you for the code streamlining. I posted the validation code (Validate-ComboBox function) as the first code snippet of my previous post... unless that's not what you're talking about. This is what is happening. In Active Directory, I have two OU's with the same name, but under different parent OUs.
  • I change the value in cmb_role combo box, the values in cmb_dept combo box change, as intended.
  • If I select a value under cmb_dept combo box (say, "orange" under the "Fruits" OU), the index of cmb_dept combo box changes to "orange" under the "Fruits" OU, as intended
  • However, when I leave cmb_dept combo box or cmb_dept combo box loses focus, the index of cmb_dept combo box is, for some reason, changing to "orange" under the "Colors" OU). This only happens on this OU that has the same name as another OU. Other OUs do not have this problem.
  • When I set a breakpoint at the first line of code in the cmb_dept_SelectedIndexChanged event handler and step through the code, the script performs the code inside the cmb_dept_SelectedIndexChanged event handler, performs the code in the cmb_dept_leave event handler, then performs (again) under cmd_dept_SelectedIndexChanged.
Somehow, the selected index of cmb_dept combo box is changing to the value with the same name under the other parent OU after the cmb_dept_leave event handler. The only code I have inside the cmd_dept_leave event handler is a write-host to output leaving the cmb_dept control. Is there another event that occurs after the leave event of a control?
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Combo Box SelectedIndex Changes Automatically

Post by jvierra »

Sorry. For some reason I missed that.

This is a better way to handle validation:

Code: Select all

$cmb_Role_Validating = [System.ComponentModel.CancelEventHandler]{
	$_.Cancel = if ($this.Text -in $($MDTRoles.Role)) {
		[void]$errorprovider1.SetError($this, '')
		$false
	} else {
		[void]$errorprovider1.SetError($this, 'Please select a Role from the list.')
		$true
	}
}

$cmb_Dept_Validating = [System.ComponentModel.CancelEventHandler]{
	$_.Cancel = if (!($this.Text -in $($this.Items.Name)) -or ($this.Text -like "---*")) {
		[void]$errorprovider1.SetError($this, 'Please select a valid Department from the list.')
		$true
	} else {
		[void]$errorprovider1.SetError($this, '')
		$false
	}
}
This eliminates much of the ambiguity.

I also recommend getting rid of the unused labels in the list. If you want grouped items use a ListView and the items will not get in your way. My suspicion is that, by skipping the labels as invalid, you are possible causing something else to happen.

You also need to look at all places where you may be resetting or refreshing the form or controls.
User avatar
barnold08
Posts: 22
Last visit: Fri Sep 02, 2022 11:36 am

Re: Combo Box SelectedIndex Changes Automatically

Post by barnold08 »

I have removed the "---" labels from the cmb_dept combo box, but the problem persisted. I cannot see anywhere where I'm programmatically refreshing or resetting the form or any controls, but I guess I'll keep digging.
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Combo Box SelectedIndex Changes Automatically

Post by jvierra »

Well it's in your code. You just have to keep debugging until you find the code that causes this. It is not a usual behavior of a control. Somewhere you have a mistake or an error. Debugging code takes time to learn. We cannot do it for you since we do not have access to your system.

One method for tracking down difficult errors is to take all code out of the form and add it back one event at a time until you find the error. This has to be done in a controlled, logical fashion.
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Combo Box SelectedIndex Changes Automatically

Post by jvierra »

Start by placing a breakpoint at the beginning of every event and every function. Run the form to the place where the problem happens and watch every line of code until you see where the error is happening.

It may also be caused by interleaved closures. This is the hardest coding error to find. By carefully stepping through your code you might see the error. By rebuilding the code on event at a time you may also see the error.
User avatar
barnold08
Posts: 22
Last visit: Fri Sep 02, 2022 11:36 am

Re: Combo Box SelectedIndex Changes Automatically

Post by barnold08 »

I figured it out! The culprit turned out to be the autocomplete setting on the cmb_dept control. I had it set to "Suggest". Once I turned off autocomplete, it worked as expected. Thanks again for all your help and sorry for wasting your time!
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Combo Box SelectedIndex Changes Automatically

Post by jvierra »

I could have made 20 guesses before guessing t that. That also tells me that your code is changing something on the combo else the autocomplete would not do that. I suspect you will find other issues down the road.

Go slow and look hard. Most things are discoverable in the debugger or with some brain drain.
This topic is 6 years and 8 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