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; } }}
Here is the code.
// Loop through panel.Controls to locate specific controls (Windows Panel)// 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 doesn't 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 now, see two lines above.
// An additional issue I have found, is that reading the CheckState ALWAYS returns unchecked even when it is checked.
} else { c.Text = ctrlText; } }}