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

Re: Load event firing twice for one form instance by Armin

Armin
Mon Jan 05 17:38:31 CST 2004

"C M Shaw" <cshaw@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


Re: Load event firing twice for one form instance by Chris

Chris
Mon Jan 05 18:20:42 CST 2004

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" <cshaw@collegeboard.com> wrote in message
news:4cac2094.0401051514.7b2f1873@posting.google.com...
> 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



Re: Load event firing twice for one form instance by cshaw

cshaw
Tue Jan 06 08:38:50 CST 2004

"Chris Taylor" <chris_taylor_za@hotmail.com> wrote in message news:<#zBatr#0DHA.2680@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

Re: Load event firing twice for one form instance by v-phuang

v-phuang
Wed Jan 07 02:45:07 CST 2004

Hi,

Based on my understanding, what you wants to do is according to the
sequence
1.display an form(show)
2.setup some info( does this setup will add some control onto the form?)
3.hide(why you need to hide the form?)
4.showdialog

Why now show the form with showdialog directly?
If you add an control onto the form, the control will show too without hide
and show again?

If I misunderstaning your meaning, please post here.


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.


Re: Load event firing twice for one form instance by cshaw

cshaw
Wed Jan 07 09:15:30 CST 2004

v-phuang@online.microsoft.com (Peter Huang) wrote in message news:<r6ImErP1DHA.2900@cpmsftngxa07.phx.gbl>...
> Hi,
>
> Based on my understanding, what you wants to do is according to the
> sequence
> 1.display an form(show)
> 2.setup some info( does this setup will add some control onto the form?)
> 3.hide(why you need to hide the form?)
> 4.showdialog
>
> Why now show the form with showdialog directly?
> If you add an control onto the form, the control will show too without hide
> and show again?

Showing the form initially triggers some setup code in Form_Load which
requires the form to be visible (it's a fairly complicated setup
involving the initialization of usercontrols, tabs, and menus and the
retrieval and display of data, and we've have some problems in the
past with trying to run it while a form wasn't visible); the
additional setup code in step 2 requires that this Form_Load setup has
already occurred. If I show the form initially with ShowDialog, I
don't have any way to call the step 2 setup from the external module.

Caitlin Shaw

Re: Load event firing twice for one form instance by v-phuang

v-phuang
Wed Jan 07 23:44:28 CST 2004

Hi,

Based on my understanding, the code in Form_Load event and DoSomeSetup all
need the form to be visible. If so I think you have to set a flag in the
form_load to prevent it runs the codes in the form_load twice.

If I misunderstanding your meaning, please post here.
If you have any concern on this issue, please post here.

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.


Re: Load event firing twice for one form instance by cshaw

cshaw
Wed Jan 14 11:42:45 CST 2004

v-phuang@online.microsoft.com (Peter Huang) wrote in message news:<Q9qhkpa1DHA.3532@cpmsftngxa07.phx.gbl>...

> Based on my understanding, the code in Form_Load event and DoSomeSetup all
> need the form to be visible. If so I think you have to set a flag in the
> form_load to prevent it runs the codes in the form_load twice.

Thanks! Actually, I decided to overload the constructor, set a flag
there, and call the form once with ShowDialog. Then the end of the
Form_Load reads the flag and calls the correct setup function. Not
pretty, but it has passed our QA department....

Caitlin Shaw

Re: Load event firing twice for one form instance by v-phuang

v-phuang
Wed Jan 14 19:00:25 CST 2004

Hi Caitlin,

I am glad that you are able to resolve the problem.

If you have any concern on this issue, please post here.

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.