I am trying to set the CheckedIndex for the UltraOptionSet control.
Stepping through the debugger, when I first reach the line of code, the CheckedIndex value is -1.
The code then sets the value to 1.
The value immediately goes back to zero.
If I go to the locals window and change the CheckedIndex value to 1, it again goes right back to 0.
There are 3 items in the list but I cannot get the value to change.
What am I missing here. All the samples show setting the CheckedIndex.
Please help
Thanks
Ben
Hi Ben,
There are two possibilities that I can think of here:
First, it's possible that the CheckedIndex is being set too soon. This might be before initialization or painting has occurred. In order to check whether this is the case, try setting CheckedIndex on a button press and see if the behavior is different. If so, this is the issue and you will need to set the value at a later point.
Second, this might be happening if two items have the same text. Please let me know if this is the case and it cannot be changed and I will look into a solution for you.
If neither of these situations applies, please put together a small reproduction sample and upload it here. I will look into the sample to determine why setting CheckedIndex is not having the expected effect.
List<Control> controls = new List<Control>();
// Loop through panel.Controls to locate specific controls// Add those controls to the controls list defined above.// All controls are fully instantiated and visible on the screen.// So its not an issue with trying to set the CheckedIndex to soon.// In addition, the options do not have the same text. (Yes, No or In Person, Remote, etc.)// It also dosnt matter how many options there are.foreach (Control c in panel.Controls){ // Is it one of the controls we are looking for if (AcceptableControls.Contains(c.GetType().Name)) { // Add the control to our list controls.Add(c); }} // Go find control and set valuesforeach (Control c in controls){ if (String.Compare(c.Name, ctrlName, true) == 0) { if (String.Compare(controlType, "UltraCheckEditor", true) == 0) { ((UltraCheckEditor)c).Checked = ctrlChecked.ToBool(); } if (String.Compare(controlType, "UltraOptionSet", true) == 0) { int index = ctrlCheckedIndex.ConvertTo<int>(); ((UltraOptionSet)c).Items[index].CheckState = CheckState.Checked; // When I save the information, I save the CheckedIndex value. // When loading the data back in, setting the CheckedIndex has no effect // I have worked around it for, see two lines above. } else { c.Text = ctrlText; } }}
I put together a sample based on your instructions and it works as I expect. Try running this sample and let me know if it reproduces the issue. If not, please modify the sample so that it does reproduce and send it back to me so I can continue to investigate.
OptionSetTest.zip
List<Control> controls = new List<Control>();private const string AcceptableControls = "UltraCheckEditor,UltraCombo,UltraDateTimeEditor,UltraMaskedEdit,UltraOptionSet,UltraTextEditor,UltraComboEditor,UltraCurrencyEditor,UltraDropDown,UltraDropDownButton,UltraRadioButton";
// Loop through panel.Controls to locate specific controls// Add those controls to the controls list defined above.// All controls are fully instantiated and visible on the screen.// So its not an issue with trying to set the CheckedIndex to soon.// In addition, the options do not have the same text. (Yes, No or In Person, Remote, etc.)// It also dosnt matter how many options there are.foreach (Control c in panel.Controls){ // Is it one of the controls we are looking for if (AcceptableControls.Contains(c.GetType().Name)) { // Add the control to our list controls.Add(c); }}
// Go find control and set valuesforeach (Control c in controls){ // ctrlName is a string read from an XML file. Its just the name of ANY control. ctrl.Name = txtText, or lblLabel, etc. // All this is doing is reading an XML, finding the matchine control in the control array and setting the property if (String.Compare(c.Name, ctrlName, true) == 0) { if (String.Compare(controlType, "UltraCheckEditor", true) == 0) { ((UltraCheckEditor)c).Checked = ctrlChecked.ToBool(); } if (String.Compare(controlType, "UltraOptionSet", true) == 0) { int index = ctrlCheckedIndex.ConvertTo<int>(); ((UltraOptionSet)c).Items[index].CheckState = CheckState.Checked; // When I save the information, I save the CheckedIndex value. // When loading the data back in, setting the CheckedIndex has no effect // I have worked around it for, see two lines above. } else { c.Text = ctrlText; } }}
You can also just open the VS Designer and drop some UltraOptionSet controls on it.In code, loop through all the controls in the form.Controls arrayFor each UltraOptionSet control in the array, try to set the CheckedIndex value.That is all that this code is doing. There just happens to be other control types on the form, which have controls which have controls, and so on.
Thank you for sending your code. I attempted to include it in a small sample, but was not able to get anything working. I am not sure what is intended to be in the AcceptableControls collection, nor what the expected value of any of the strings used in the comparisons should be.
I understand that you have found a way to work around this. Do you find the workaround acceptable, or would you prefer to send a sample application that I can run to continue debugging this issue?