multiple occurences of HandleCreated for a single control

  • Thread starter Thread starter Asher F.
  • Start date Start date
A

Asher F.

I’m using control that needs to be register to asynchronous events.
The events will be raised in the UI thread using the ISynchronizeInvoke
interface implemented by WinForms controls


I can’t register to the event at the constructor because it will allow
calling the event handler before the control is fully created. during which
calls to ISynchronizeInvoke are not allowed.

Solution to that problem is to use the perform the asynchronous event
registration from an event handler to the HandleCreated instead of
registering from the constructor.

however, this poses another issue, in some scenations the HandleCreated
event is raised multiple times as result of change at the control state.
For example, each changing of the “RightToLeft†property causes a WMCreate
message that cause raising the “HandleCreated†event.


How can I prevent the multiply times of event rising?
Is there is another way to know when the control is created and display for
the first time?

I can keep a boolean flag in the HandleCreated, however it feels like a hack
and I am wondering if there is a better way to handle this issue.
 
Hi Asher,

Based on my understanding, you have a UserControl and would like to
register to asynchronous events from within the UseControl after the
UserControl is fully created. If I'm off base, please feel free to let me
know.

I understand your scenario. A better solution is to implement the
ISupportInitialize interface on the UserControl and then register the
events in the ISupportInitialize.EndInit method.

If a control implements the ISupportInitialize interface, the call to the
ISupportInitialize.BeginInit and ISupportInitialize.EndInit methods will be
serialized in the InitializeComponent method within the parent Form's code
file when the control is added to the Form at desgin time. Note that the
ISupportInitialize.EndInit method will be called after the control has
finished the construction and initialization. So when the
ISupportInitialize.EndInit method is called, the control has already been
fully created.

The following is a sample.

public partial class UserControl1 : UserControl,ISupportInitialize
{
public UserControl1()
{
InitializeComponent();
}

#region ISupportInitialize Members

void ISupportInitialize.BeginInit()
{
}

void ISupportInitialize.EndInit()
{
// register the events you want here
}

#endregion
}

Hope this helps.
If you have any question, please feel free to let me know.

Sincerely,
Linda Liu
Microsoft Online Community Support

Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
(e-mail address removed).

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
 
Back
Top