Load event firing twice for one form instance

C

C M Shaw

I have a form which I want to show modally; it's a fairly old form
that's been ported up several versions of VB, and I'd like to keep its
rewriting to a minimum. Basically, it is used in this sequence:

1. The form is shown. The Form_Load event does some initialization.
2. Further parameters are passed to this form.
3. We actually need this form to be modal, so we hide it and show it
again modally.
4. Stuff happens on the form based on the parameters passed in, etc.

(The VB.NET code for this is below.) However, I've discovered that
calling ShowDialog() in step #3 fires off the Form_Load event a second
time, even though it's the same object instance. Does any instance of
showing a form fire this event?

(My problem here is that the Form_Load initialization should only
happen once. I could use an already_initialized flag, but that's
horribly messy; if I have to, I'd rather pass Foo in the constructor
and hold onto it through the Form_Load process.)

--Caitlin Shaw

**Code**

Private WithEvents frmThing As ThingForm

Public Sub DoThatThingWithFoo(Foo as Object)

'Create the thing and set it up
frmThing = New ThingForm()
frmThing.Show()
frmThing.DoSomeSetup(Foo)

'Really, though, we want this thing to be modal
frmThing.Hide()
frmThing.ShowDialog()

'Good, that's done
frmThing.Dispose()

End Sub
 
A

Armin Zingler

C M Shaw said:
I have a form which I want to show modally; it's a fairly old
form that's been ported up several versions of VB, and I'd like to
keep its rewriting to a minimum. Basically, it is used in this
sequence:

1. The form is shown. The Form_Load event does some
initialization.
2. Further parameters are passed to this form.
3. We actually need this form to be modal, so we hide it and show
it again modally.
4. Stuff happens on the form based on the parameters passed in,
etc.

Why not pass the parameters to a constructor, then show the Form modally?
 
C

Chris Taylor

Hi,

The Form_Load event is fired when the form is initially made visible by
through the Show() method and then again when the ShowDialog() method is
invoked.

Does the form need to be visible for the call to DoSomeSetup()?
If not then your code can be replaced with:

frmThing = New ThingForm()
frmThing.DoSomeSetup(Foo)
frmThing.ShowDialog()

If it a mater of requiring the child controls to be created, then you could
have a member of the form initialized to Foo be for the call to ShowDialog()
and then in the Load event you can call DoSomeSetup().

Public Class ThingForm
Inherits System.Windows.Forms.Form
Public FooObject as Object

Private Sub ThingForm_Load(ByVal sender As Object, ByVal e As
System.EventArgs) Handles MyBase.Load
' Include some error checking here...
DoSomeSetup( FooObject )
End Sub
End Class

frmThing = New ThingForm()
frmThing.FooObject = Foo
frmThing.ShowDialog()

OR

You can provide your form with a constructor that accepts a parameter

Public Sub New(ByVal fooObject As Object)
MyBase.New()

'This call is required by the Windows Form Designer.
InitializeComponent()

'Controls can be accessed at this point
DoSomeSetup(fooObject)

End Sub

frmThing = New ThingForm( Foo )
frmThing.ShowDialog()

Hope this helps
 
C

C M Shaw

Chris Taylor said:
The Form_Load event is fired when the form is initially made visible by
through the Show() method and then again when the ShowDialog() method is
invoked.

That still seems counter-intuitive to me, but thanks for confirming
it, Chris!
Does the form need to be visible for the call to DoSomeSetup()?

It actually needs to have run through the rest of the Form_Load setup,
which does need visibility.
If it a mater of requiring the child controls to be created, then you could
have a member of the form initialized to Foo be for the call to ShowDialog()
and then in the Load event you can call DoSomeSetup().

OR

You can provide your form with a constructor that accepts a parameter

Yes -- what I realize I've failed to mention, though, is that I also
use ThingForm elsewhere with a different setup function --
DoSomeSetup(Foo) preloads certain information from Foo onto the form
for creating a new record, and DoDifferentSetup(Foo, BarEnumeration)
displays some of Foo for editing. So I'd need to pass not only Foo
but also a flag indicating what to do with Foo once the form is
loaded....

Thanks again for your reply! I suppose I need to resign myself to
flags.

--Caitlin Shaw
 
P

Peter Huang

Hi,

I have replied to your post in the group below
microsoft.public.dotnet.framework.windowsforms
you may go and take a look.
If you have any concern on this issue, please post there.

Regards,
Peter Huang
Microsoft Online Partner Support
Get Secure! www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top