Windows Develop Bookmark and Share   
 index > Windows Forms General > Please help
 

Please help

I want to add Onload event for my windows user control. But I am not getting the idea where to call this event in my control.Can somebody help me please its very urgent?
Chatanya  Friday, April 21, 2006 1:59 PM

Hello Chatanya.

The closest I have come to is

Form OldParent;

protected override void OnParentChanged(EventArgs e)

{

if (OldParent!=null)

{ OldParent.FormClosed -= new FormClosedEventHandler(OnFormClosed); }

base.OnParentChanged(e);

if (this.ParentForm!=null)

{

this.ParentForm.FormClosed += new FormClosedEventHandler(OnFormClosed);

this.OldParent = this.ParentForm;

}

}

void OnFormClosed(object sender, FormClosedEventArgs e)

{

// Cleanup code

System.Diagnostics.Debug.WriteLine("Unloading");

}

What this code does is, whenever the control's parent is changed it subscribes to the form's ClosedEvent. Whenever the form is Closed, theEvent is triggered and the control should be in the process of unloading. Thbe OldParent code is there to ensure that events do not stay attached or get subscribed to multiple times.

I'm not sure what you are trying to use this code for so i can't say whether this will accomplish what you really need.

MarcD  Monday, April 24, 2006 1:49 PM

the OnLoad Event already exists for a UserControl. If you want code inside the UserControl to execute upon the load simply do this

protected override void OnLoad(EventArgs e)

{

// code to execute

base.OnLoad(e);

}

If you have code that is outside, say in the Form that is going to host it then simply bind to it through the designer by clicking on the controlon your form in the designer going to properties and then clicking on events and finding the Load event. You can do it manualy by putting

userControl11.Load += new EventHandler(userControl11_Load); this after the InitializeComponent()

(assuming C#. If not please provide more information like what version of .NET and what language)

Hope this helps.

MarcD  Friday, April 21, 2006 2:45 PM

Hi,

U r correct but sorry for typo mistake.

It is for UnLoad event.

thanks for looking into my problem

Chatanya  Friday, April 21, 2006 2:47 PM

Hello Chatanya.

The closest I have come to is

Form OldParent;

protected override void OnParentChanged(EventArgs e)

{

if (OldParent!=null)

{ OldParent.FormClosed -= new FormClosedEventHandler(OnFormClosed); }

base.OnParentChanged(e);

if (this.ParentForm!=null)

{

this.ParentForm.FormClosed += new FormClosedEventHandler(OnFormClosed);

this.OldParent = this.ParentForm;

}

}

void OnFormClosed(object sender, FormClosedEventArgs e)

{

// Cleanup code

System.Diagnostics.Debug.WriteLine("Unloading");

}

What this code does is, whenever the control's parent is changed it subscribes to the form's ClosedEvent. Whenever the form is Closed, theEvent is triggered and the control should be in the process of unloading. Thbe OldParent code is there to ensure that events do not stay attached or get subscribed to multiple times.

I'm not sure what you are trying to use this code for so i can't say whether this will accomplish what you really need.

MarcD  Monday, April 24, 2006 1:49 PM

Hi amrc,

thanks for your reply.

i will try this.

However I have added unload event and I am Invoking this event for handledestroyed.

I am not very sure that this is correct way or not.

But I will try your code also.

thanks for ur response.

Chatanya  Monday, April 24, 2006 1:54 PM

You can use google to search for other answers

Custom Search

More Threads

• TreeView - Drag & Drop - Need some help formatting it
• Will MS stop developing Windows Form?
• Start Windows Service After Install
• Changing size of Control
• Disable arrow keys for radiobuttons
• Change backcolor and forecolor when hovering menu items of MenuStrip
• keeping string values secure in a winforms app
• underline text
• how to convert
• how to initialise with array[counter];what is missing in my code