Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Events and Application.DoEvents()

0 views
Skip to first unread message

Jay Dee

unread,
Apr 5, 2009, 8:30:09 AM4/5/09
to
Can anyone explain to me how to trigger events in a control.

I have overridden the text class of a control and stored the text as
List<char>.
Because I am not calling the base.Text method from within the
overridden method the TextChainged event is not being raised.

If I add OnTextChanged(new EventArgs()) to the overridden method then
this will call the text changed event, but then I am having trouble
when the control is first initialised as the OnTextChanged() event is
not normally triggered when the control is being initialised.

I am wondering weather this has something to do with the SuspendLayout
(); and ResumeLayout(false); calls that the designer adds to a control
that is used at design time but I don’t think it is relevant.

Another thought is weather it has something to do with the Events
property of the control If I am not mistaken the OnTextChanged is not
normally called until a call to Application.DoEvents() in witch case
the overridden property could be still adding the OnTextChanged method
to a list that is being called at the end of the initialize.

To be honest I don’t really have a clue, could someone point me in the
right direction.

Thanks

Jay Dee

Peter Duniho

unread,
Apr 5, 2009, 5:33:13 PM4/5/09
to
On Sun, 05 Apr 2009 05:30:09 -0700, Jay Dee <firs...@gmail.com> wrote:

> Can anyone explain to me how to trigger events in a control.
>
> I have overridden the text class of a control and stored the text as
> List<char>.

Do you mean "...have overridden the Text property of the Control class"?

> Because I am not calling the base.Text method from within the
> overridden method the TextChainged event is not being raised.

Why aren't you calling the base Text property? That is the simplest way
to ensure that everything works.

> If I add OnTextChanged(new EventArgs()) to the overridden method then
> this will call the text changed event, but then I am having trouble
> when the control is first initialised as the OnTextChanged() event is
> not normally triggered when the control is being initialised.

OnTextChanged() is called by the property implementation. If you override
the implementation, you either need to call the base implementation, or
call that method yourself.

If you are calling that method in the Text property override, in the
property setter, then the TextChanged event should be raised as expected.
There's no such things as "the OnTextChanged() event", but there is a
"TextChanged event" and that event most certainly is raised when the
control is being initialized for the default implementation of the Text
property, and you should make sure that your override does so as well.

> I am wondering weather this has something to do with the SuspendLayout
> (); and ResumeLayout(false); calls that the designer adds to a control
> that is used at design time but I don’t think it is relevant.

No, it's not.

> Another thought is weather it has something to do with the Events
> property of the control If I am not mistaken the OnTextChanged is not
> normally called until a call to Application.DoEvents()

You are mistaken. OnTextChanged() is called by the default Text property
implementation as soon as the property changes. Application.DoEvents()
has nothing to do with it.

> in witch case
> the overridden property could be still adding the OnTextChanged method
> to a list that is being called at the end of the initialize.
>
> To be honest I don’t really have a clue, could someone point me in the
> right direction.

My personal opinion is that you should rethink whether you really need to
override the Text property in the first place. But, there really
shouldn't be much trouble doing so. If you make sure to preserve the
original implementation, either by calling it explicitly, or by
replicating it in your own override, it should work fine.

Without a concise-but-complete code sample that reliably demonstrates the
problem, there's no way for us to know why it's not working fine in your
case. Presumably you have failed to ensure one or more of the things I
describe above, but what that might be is impossible to say without seeing
the code.

Pete

Jay Dee

unread,
Apr 5, 2009, 8:22:10 PM4/5/09
to
Thanks Peter, you have been a grate help. I have fixed the problem
that turned out to be nothing to do with what I was on about anyway,
by unhooking the event causing the problem then hocking it back after
making the change.

The reason I have not called the base method was for performance
mainly as the text is very large and I wanted to be able to access the
underlying char array rather than having to pass the whole string
every time I was changing one char.

Thanks a lot

Jay Dee

0 new messages