Paul Williams Paul Williams - 4 months ago 7
Vb.net Question

When does the event 'MyBase.Load' fire?

I'm working with the following bit of code to invoke a dialog.

Using frmSomeForm As New SomeForm()
frmSomeForm.intSomeVariable = 6
frmSomeForm.ShowDialog()
End Using


Inside that
SomeForm()
is a
MyBase.Load
event. In this particular case:

Private Sub SomeForm_Load(sender As Object, e As EventArgs) Handles MyBase.Load
If intSomeVariable <> 0 Then SomeOtherSub()
End Sub


My question is when does the load event fire? Does it fire at the moment the
.ShowDialog()
is invoked or is it fired at the moment of the
Using
line.

Answer

The Load event is only raised once you call Show() or ShowDialog(), when the form is about to be shown. Currently your frmSomeForm.intSomeVariable = 6 line will always fire before the Load event.

Answering your comment "Would a Property also work in this case?":

Keep in mind that any code that you put before your Show() or ShowDialog() call will be executed first, even if it's a time consuming one. Code isn't executed asynchronously line-by-line.

For example, this:

TimeConsumingMethod()
frmSomeForm.ShowDialog()

would only call frmSomeForm.ShowDialog() after the TimeConsumingMethod() has completed successfully, even if that takes for ever.