views:

103

answers:

2

Hello.

I'm developing a Windows Mobile 5.0 or above with .Net Compact Framework 2.0 SP2 and C#.

I have a WinForm that only has a Custom Control. I want to call a method of that custom control when the form has finished loading and display the control.

Now I using Activated Event, but these event is thrown everytime when I close a messagebox.

Is there an event thrown in that moment?

Thank you.

+1  A: 

I don't think you have a Shown event in the Compact Framework, but you should be able to use the Load event. Set your form's Visible property to true and you should be able to access your custom control after that.

MyForm_Load(object sender, EventArgs e)
{
    ' this procedure runs only once, when the form loads
    ' make the form visible to the user now
    this.Visible = true
    ' the form is now visible

    ' ... more code
}
Beaner
As you can see here: http://msdn.microsoft.com/en-us/library/system.windows.forms.form.load.aspxLoad event occurs before a form is displayed for the first time. I want to call this method when the form and its control are displayed.
VansFannel
Yes, I am aware that Load happens first. However setting this.Visible = true; in the Load event should be a viable work-around.
Beaner
I don't understand you. How can I access my control after setting Visible = true. I understand the control is created with this property set to false. And then I set up to true on Load event, isn`t it?
VansFannel
Your form has a Load event. Inside the load event set your forms visible property = true. Your form will immediately be visible to the user, kind of the same thing as having a Shown property. Now you know the form is loaded and visible for the first time - you can do what ever it is you are trying to do when the form is shown and this will only happen once.
Beaner
It sounds like you’re explicitly setting the form to be visible.
Chris
+4  A: 

Simply use the activate event and have a boolean in your form that is set to true when you have called the method on your custom control. When the form's activated event is triggered again, you just make a check on this boolean.

I thought it was a bad design use a global boolean variable to run my code once on activated event.
VansFannel
If you use this hack, wouldn't you have to manage the boolean during the entire lifetime of the form?
Chris
It's not a global variable, it's local to the form only. Is that bool a lot to manage? don't think so. It's normal to have a bool in a class to tell wether it has initialized or it is disposed and other things. Standard .NET forms have a lot of properties telling us about its states. Is 'IsDisposed' a hack? No, I don't think so. –