Situation:
A Windows Form with a single User Control containing 4 custom controls. This lets me drop the User Control on its own form to make it a dialog, or make it part of a larger configuration dialog with several such User Controls.
Each custom control has a property for COM port name (serial port).
Each custom control has a method for writing the settings.
The user control has a GetEnumeration structure so it can be easily referenced with a foreach (myCustomContro l Bob in myUserControl) type reference.
Errant behavior:
If I walk through using the foreach construct everything executes correctly THEN all the custom controls are inexplicably rendered invisible. They still exist because I can step through the save function and they all perform their methods. They just become hidden on the Windows form.
Yet - If I walk through the same steps with a for (int a = [...]) type construct doing exactly the same things everything executes and the custom controls are NOT hidden.
Its not like the loop is doing a thousand tasks and the process is hard to follow:
1) Set port name
2) Write settings
It really shouldn't matter *how* the set of controls is looped through. Both the 'for' and 'foreach' *should* being doing exactly the same thing: Loop through the four controls, set the port name property and execute the WritePrefs() method. That's it. Yet for some reason after the 'foreach' completes all the custom controls go invisible.
Something in the bowels of the foreach seems to be reacting to the custom controls .Show() and .Hide()
Does anyone have a clue or has anyone seen something like this before?
Obviously I have already worked around the weird behavior by using a 'for' instead of 'foreach'. But I'd like to understand the actual problem so I can use the more elegant 'foreach' in the future.
The foreach that hides the custom controls
The for that does the same thing without hiding the custom controls.
The class with the GetEnumeration
A Windows Form with a single User Control containing 4 custom controls. This lets me drop the User Control on its own form to make it a dialog, or make it part of a larger configuration dialog with several such User Controls.
Each custom control has a property for COM port name (serial port).
Each custom control has a method for writing the settings.
The user control has a GetEnumeration structure so it can be easily referenced with a foreach (myCustomContro l Bob in myUserControl) type reference.
Errant behavior:
If I walk through using the foreach construct everything executes correctly THEN all the custom controls are inexplicably rendered invisible. They still exist because I can step through the save function and they all perform their methods. They just become hidden on the Windows form.
Yet - If I walk through the same steps with a for (int a = [...]) type construct doing exactly the same things everything executes and the custom controls are NOT hidden.
Its not like the loop is doing a thousand tasks and the process is hard to follow:
1) Set port name
2) Write settings
It really shouldn't matter *how* the set of controls is looped through. Both the 'for' and 'foreach' *should* being doing exactly the same thing: Loop through the four controls, set the port name property and execute the WritePrefs() method. That's it. Yet for some reason after the 'foreach' completes all the custom controls go invisible.
Something in the bowels of the foreach seems to be reacting to the custom controls .Show() and .Hide()
Does anyone have a clue or has anyone seen something like this before?
Obviously I have already worked around the weird behavior by using a 'for' instead of 'foreach'. But I'd like to understand the actual problem so I can use the more elegant 'foreach' in the future.
The foreach that hides the custom controls
Code:
foreach (Saint.Controls.Devices.DeviceCtrl pebbles in Controller1)
{
pebbles.myObject.PortName = myCOMport.PortName;
pebbles.myObject.WritePrefs();
}
Code:
for (int a = 0; a < Controller1.Controls.Count; a++)
{
((Saint.Controls.Devices.DeviceCtrl) Controller1.Controls[a]).myObject.PortName = myCOMport.PortName;
((Saint.Controls.Devices.DeviceCtrl) Controller1.Controls[a]).myObject.WritePrefs();
}
Code:
public partial class Controller1: UserControl, IEnumerable, IEnumerator
{
ArrayList AllControls = new ArrayList();
#region GetEnumeration
int Position = -1;
public IEnumerator GetEnumerator()
{
return (IEnumerator)this;
}
public bool MoveNext()
{
if (Position < AllControls.Count - 1)
{
++Position;
return true;
}
Reset();
return false;
}
public void Reset()
{
Position = -1;
}
public object Current
{
get
{
return AllControls[Position];
}
}
#endregion GetEnumeration
#region Construction
public Controller1()
{
InitializeComponent();
AllControls.Add(tcntrlWiper);
AllControls.Add(tcntlWasher);
AllControls.Add(tcntrlBlower);
AllControls.Add(tcntrlDefroster);
}
#endregion Construction
}
Comment