Neon Flash
Neon Flash

Reputation: 3233

Form_Load doesn't execute in application

I am new to Visual Basic. I have installed Microsoft Visual Studio 2010. Created a new Windows Form Application. As an example, I made a simple program which will ask the end user to input 2 numbers and allow them to either add them or subtract the second number from the first one and display the output in a Textbox.

Now, I added another Subroutine which would be executed automatically when the Windows Form loads. This would calculate the width of the output Textbox and the Form Width and display at the bottom.

This is how the code looks like right now:

Public Class Form1

    ' Run this Subroutine initially to display the Form and Text box width
    Private Sub Form_Load()
        Label5.Text = TextBox3.Width
        Label7.Text = Me.Width
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim a As Integer
        Dim b As Integer
        a = TextBox1.Text
        b = TextBox2.Text
        TextBox3.Text = a + b
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        Dim a As Integer
        Dim b As Integer
        a = TextBox1.Text
        b = TextBox2.Text
        TextBox3.Text = a - b
    End Sub
End Class

While everything works correctly for the addition and subtraction, it does not display the Form and output Textbox width in the Windows Form.

I think, Form_Load() is not executing properly.

I also tried, Form_Activate() but that did not work either.

Once I am able to do this, I would like to extend this concept to resize the output Textbox along with the Form resize. However, for the purpose of understanding I wanted to see if I can execute Form_Load() successfully.

Thanks.

Upvotes: 2

Views: 3458

Answers (2)

SysDragon
SysDragon

Reputation: 9888

You need to add the Handle so the app executes it automatically:

Private Sub Form_Load(ByVal sender As Object, ByVal e As EventArgs) Handles MyBase.Load
    '...
End Sub

Upvotes: 2

Konrad Rudolph
Konrad Rudolph

Reputation: 545598

Form_Load doesn’t execute. For now, it’s just any other method. In order to tell VB to make this method handle the Load event, you need to tell it so:

Private Sub Form_Load(sender As Object, e As EventArgs) Handles MyBase.Loasd
    Label5.Text = TextBox3.Width
    Label7.Text = Me.Width
End Sub

(And add the required parameters for the event.)

A few other remarks:

  • Ensure that Option Strict On is enabled in your project options at all times. This will make the compiler much stricter with your code and flag more errors. This is a good thing since these errors are potential bugs. In particular, your code is very lax with conversions between different data types, these should be made explicit.

  • Initialise variables when you declare them, don’t assign a value in a separate statement. That is, write this:

    Dim a As Integer = Integer.Parse(TextBox1.Text)
    

    (Explicit conversion added as well.)

  • If you want to make a control fill the form, you can just set its Dock property appropriately in the forms editor, instead of having to program this manually.

Upvotes: 2

Related Questions