By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,230 Members | 2,427 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,230 IT Pros & Developers. It's quick & easy.

Load event firing twice for one form instance

P: n/a
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
Nov 20 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
"C M Shaw" <cs***@collegeboard.com> schrieb
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?
--
Armin

http://www.plig.net/nnq/nquote.html
http://www.netmeister.org/news/learn2quote.html

Nov 20 '05 #2

P: n/a
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

--
Chris Taylor
http://www.xanga.com/home.aspx?user=taylorza

"C M Shaw" <cs***@collegeboard.com> wrote in message
news:4c**************************@posting.google.c om...
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

Nov 20 '05 #3

P: n/a
"Chris Taylor" <ch*************@hotmail.com> wrote in message news:<#z**************@tk2msftngp13.phx.gbl>...
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
Nov 20 '05 #4

P: n/a
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.

Nov 20 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.