Click to Rate and Give Feedback
MSDN
MSDN Library
.NET Development
.NET Framework 4
Form Class
Form Events
 Load Event
Collapse All/Expand All Collapse All
.NET Framework Class Library
Form..::.Load Event

Occurs before a form is displayed for the first time.

Namespace:  System.Windows.Forms
Assembly:  System.Windows.Forms (in System.Windows.Forms.dll)
Visual Basic
Public Event Load As EventHandler
C#
public event EventHandler Load
Visual C++
public:
 event EventHandler^ Load {
    void add (EventHandler^ value);
    void remove (EventHandler^ value);
}
F#
member Load : IEvent<EventHandler,
    EventArgs>

You can use this event to perform tasks such as allocating resources used by the form.

For more information about handling events, see Consuming Events.

The following code example demonstrates how to use the SetDesktopLocation, Load, Activated, and Activate members. To run the example, paste the following code in a form called Form1 containing a Button called Button1 and two Label controls called Label1 and Label2.

Visual Basic
Shared x As Integer = 200
Shared y As Integer = 200

Private Sub Button1_Click(ByVal sender As System.Object, _
    ByVal e As System.EventArgs) Handles Button1.Click

    ' Create a new Form1 and set its Visible property to true.
    Dim form2 As New Form1
    form2.Visible = True

    ' Set the new form's desktop location so it appears below and 
    ' to the right of the current form.
    form2.SetDesktopLocation(x, y)
    x += 30
    y += 30

    ' Keep the current form active by calling the Activate method.
    Me.Activate()
    Me.Button1.Enabled = False
End Sub



' Updates the label text to reflect the current values of x and y, 
' which was were incremented in the Button1 control's click event.
Private Sub Form1_Activated(ByVal sender As Object, _
    ByVal e As System.EventArgs) Handles MyBase.Activated
    Label1.Text = "x: " & x & " y: " & y
    Label2.Text = "Number of forms currently open: " & count
End Sub

Shared count As Integer = 0

Private Sub Form1_Closed(ByVal sender As Object, _
    ByVal e As System.EventArgs) Handles MyBase.Closed
    count -= 1
End Sub

Private Sub Form1_Load(ByVal sender As Object, _
    ByVal e As System.EventArgs) Handles MyBase.Load
    count += 1
End Sub
C#
    static int x = 200;
    static int y = 200;

    private void Button1_Click(System.Object sender, 
        System.EventArgs e)
    {
        // Create a new Form1 and set its Visible property to true.
        Form1 form2 = new Form1();
        form2.Visible = true;

        // Set the new form's desktop location so it  
        // appears below and to the right of the current form.
        form2.SetDesktopLocation(x, y);
        x += 30;
        y += 30;

        // Keep the current form active by calling the Activate
        // method.
        this.Activate();
        this.Button1.Enabled = false;
    }
    


    // Updates the label text to reflect the current values of x 
    // and y, which was were incremented in the Button1 control's 
    // click event.
    private void Form1_Activated(object sender, System.EventArgs e)
    {
        Label1.Text = "x: "+x+" y: "+y;
        Label2.Text = "Number of forms currently open: "+count;
    }

    static int count = 0;

    private void Form1_Closed(object sender, System.EventArgs e)
    {
        count -= 1;
    }

    private void Form1_Load(object sender, System.EventArgs e)
    {
        count += 1;
    }
Visual C++
static int x = 200;
static int y = 200;
void Button1_Click( System::Object^ sender, System::EventArgs^ e )
{

   // Create a new Form1 and set its Visible property to true.
   Form1^ form2 = gcnew Form1;
   form2->Visible = true;

   // Set the new form's desktop location so it  
   // appears below and to the right of the current form.
   form2->SetDesktopLocation( x, y );
   x += 30;
   y += 30;

   // Keep the current form active by calling the Activate
   // method.
   this->Activate();
   this->Button1->Enabled = false;
}


// Updates the label text to reflect the current values of x 
// and y, which was were incremented in the Button1 control's 
// click event.
void Form1_Activated( Object^ sender, System::EventArgs^ e )
{
   Label1->Text = String::Format( "x: {0} y: {1}", x, y );
   Label2->Text = String::Format( "Number of forms currently open: {0}", count );
}

static int count = 0;
void Form1_Closed( Object^ sender, System::EventArgs^ e )
{
   count -= 1;
}

void Form1_Load( Object^ sender, System::EventArgs^ e )
{
   count += 1;
}

.NET Framework

Supported in: 4, 3.5, 3.0, 2.0, 1.1, 1.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.
Tags What's this?: Add a tag
Community Content   What is Community Content?
Add new content RSS  Annotations
Dealing with Exception Errors During Form Load      DavidRossG   |   Edit   |   Show History

The VB.NET event firing sequence, when a form is loading, has a different order than VB6. It was ILRAP (Initialize, Reload, Resize, Activate, Paint), but is now TRLAP (TextChanged, Resized, Load, Activate, Paint). TextChanged and Resized events fire first because in the InitializeComponents method, the invocation of PerformLayout will force the TextChanged and Resized events to fire, if they are defined. However, if they fire before a form is loaded, this can often result in an exception error. To fix this, we have to set up form-level flag that is set to True once the form is loaded, and in TextChanged and Resized events, ignore them if the flag is not set. Why can’t TextChanged and Resized events simply be forced to be ignored until the form is loaded, automatically?


In the meantime, I have found that I can eradicate the frustration resulting from the early firing of TextChange and Resized events by declaring a Boolean flag at the procedure-level of the form, named mFormLoaded (Protected mFormLoaded As Boolean = False) that will be set to True (mFormLoaded = True) at the very bottom of the form’s Load event. Then, as the first statement in every existing TextChanged and Resized event I have on my form, I insert the following simple statement:

If Not mFormLoaded Then Return 'do not process this event until the form is loaded

Flag as ContentBug
Processing
© 2012 Microsoft. All rights reserved. Terms of Use | Trademarks | Privacy Statement
Page view tracker