Click here to Skip to main content
15,886,578 members
Please Sign up or sign in to vote.
1.00/5 (3 votes)
See more:
How would I create a form_load event if the form hasn't been created yet?

If I use:
Form x = new Form();
x.Visible = true;


How would I create an event from this? So that when it loads a MessageBox would show up.

Thanks for any help.
Posted

In real life, you never call the code you have shown in the Question, because you never create an instance of System.Windows.Forms.Form. Instead, you always create a derived class using Form as a base class. You create a constructor for your form class. This is where you can setup all event handles, and in most cases should. It will happen exactly as you wanted: before the instance of the form is created.

C#
public class MyForm : Form {
    public MyForm {
         this.Load += (sender, eventArgs) =&ft; {
              MessageBox.Show(
                  "If you can read this, the form is not yet created. Hope you will see it later",
                  string.Format(" {0}: before showing the form", Application.ProductName));
         };
         //...
    }
}


You can handle any other form event this way and setup events for all of the form's controls in this way. The event Form.Load is kind of "fictional" event, simply invoked before all other form event directly. Even though this event is often handled, it is never absolutely required. Instead, you can simplu call its handler from the Form constructor. More typically, you should setup the event Form.Shown, Form.FormClosing, FormFormClosed. In these cases handling events is essentially important.

—SA
 
Share this answer
 
There are several ways to do what you want.

The first, and easiest, to design the Form for 'x' using the designer and just create a FormLoad handler in the normal way.

If you must control this from the Form that creates x then something like:
C#
Form x = new Form();
x.Load += this.OnXLoad;
x.Show()  // Note Show() NOT visible. You could also use ShowDialog() which would make x Modal
..............................
..............................
..............................
..............................
..............................
private void OnXLoad(object sender, System.EventArgs e)
{
  MessageBox.Show("Boo");
}
 
Share this answer
 
if i understand it correctly, you can use
this.VisibleChanged += new System.EventHandler(Form1_VisibuleChanged); 


have a message box show 'hello', whenever you change the Visibility
 
Share this answer
 
Comments
Henry Minute 11-Apr-11 22:08pm    
The OP wants to show a MessageBox when Form x Loads not when its visibility changes.
Also the code you have posted would fire when the current form changes its visibility, not when Form x did.
Ramu Sangabathula Original 11-Apr-11 23:05pm    
haa...,gotcha...,I was thinking he can apply the same principle to Form x. Thanks for setting right

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900