How to force Form2_Load code to execute?  
Author Message
rmgjohnes





PostPosted: Visual Basic General, How to force Form2_Load code to execute? Top

VS 2005

How can code in the Form2_Load Event be forced to execute from code in Form1

The following is an outline of my dilemma:

WindowsApplication1

This application has two forms: Form1 and Form2

Startup form is Form1

Graphics are performed on Form2

Public Class Form1

'Public variables are defined here

Private Sub Form1_Load(ByVal sender as System.Object, ByVal eventArgs As Systerm.EventArgs) Handles MyBase.Load

'Public variables updated

Form2.Show()

End Sub

End Class

Public Class Form2

Private Sub Form2_Load(ByVal sender as System.Object, ByVal eventArgs As Systerm.EventArgs) Handles MyBase.Load

'set graphic parameters here with use of Public variables set in Form1

Call Form2_Paint(Nothing,Nothing) 'update graphics

End Sub

Private Sub Form2_Paint(ByVal sender As Object, ByVal e As Syster.Windows.Forms.PaintEventArgs) Handles MyBase.Paint

'perform graphics

End Sub

End Class

At times when the statement Form2.Show() is executed from Form1, the Form2_Paint Event subroutine is entered directly but the code in Form2_Load Event subroutine is not executed. The graphics is performed without recognizing the updated Public variables set in Form1. So the problem is how can the code in the Form2_Load Event be forced to execute, thus allowing the Public variables to be used in the Form2_Paint routine

Thanks very much for any suggestions...

jes



Visual Basic1  
 
 
jwooley





PostPosted: Visual Basic General, How to force Form2_Load code to execute? Top

If you are trying to get form2 to read properties of form1 consider refactoring it to have methods on form2 that you pass the needed values from form1, possibly on an overloaded constructor. Thus, you add the following to form2:

Private _param1 as string
Private _param2 as string

Public Sub New(param1 as string, param2 as string)

me.New()
SetParameters(param1, param2)

End Sub

Public Sub SetParameters(param1 as string, param2 as string)

_param1=param1
_param2=param2
me.Invalidate()

end sub

Then in your Form2_paint method, refer to these parameters. If you need to update it after you create the form, you can consider adding a call from form1 to SetParameters with the new values. Form2's Paint method should get called due to the Invalidate command.

Jim Wooley
http://devauthority.com/blogs/jwooley