"Gene Wirchenko" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
>>Open the form in the IDE, select the combobox, right click the control
>>and select Properties from the context menu. In the property window
>>click the lightening bolt icon to view the control's events. Clear the
>>value of the SelectedIndexChanged property.
>
> But I want something done when that event fires!
Here's what I understand from this thread:
1) You are loading a combo box during form construction or at some similarly
early point in the form's lifetime.
2) You have an event handler hooked up to this combo box (to handle
SelectedIndexChanged) which is firing during this initial loading sequence
and you don't want that.
3) You DO want the event handler to fire after the initial loading is
complete and the user is manipulating the combo box.
If I understand this correctly, then what you've been told is fine; you're
just not wrapping your head around it. People are telling you NOT to wire up
the event handler in the IDE because then it will be "live" before your
loading sequence is called and therefore it will fire. Instead, they are
telling you to delete it via the IDE and then to MANUALLY wire it up in code
AFTER you have loaded your combo box. Pete, "Reg," do I have this right?
Gene, do you get what I'm saying?
Now, as for your "kludge," in my experience, there are times that, at
run-time, you wish to ignore events if they are not generated by the proper
entity. What that boils down to for me is that I often want to react to the
event if it was caused by the user but ignore it if it was caused by code.
To that end I have a special nested class that looks like this:
#region ChangedByCodeMonitor
private enum ChangedByCodeAction
{
Acquire,
Clear,
Release
}
private static class ChangedByCodeMonitor
{
private static int _count = 0;
public static bool ChangedByCode
{
get { return _count > 0; }
}
public static void ChangeStatus(ChangedByCodeAction action)
{
switch (action)
{
case ChangedByCodeAction.Acquire:
_count++;
break;
case ChangedByCodeAction.Clear:
_count = 0;
break;
case ChangedByCodeAction.Release:
_count--;
if (_count < 0)
_count = 0;
break;
}
}
}
#endregion
Whenever I enter a block of code and I want subsequent code to behave
differently (which usually means I want it to not run), I call
ChangeStatus(ChangedByCodeAction.Acquire). This bumps the reference count.
As long as the count is > 0, the ChangedByCode property will return true.
And as you may guess, I check the ChangedByCode property to see if I should
do my special processing (i.e., ignoring) or not. When I'm out of the block
of code, I call ChangeStatus(ChangedByCodeAction.Release). Generally you
need as many Release calls as you made Acquire calls, but if you know you're
about to step completely out of your special mode you can just make a Clear
call and reset the class immediately.
I don't consider this to be a kludge at all, by the way. It's a glorified
flag, and flags are standard programming constructs.
|