473,378 Members | 1,427 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

Load event firing twice for one form instance

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
4 9002
"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
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
"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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: dlieu | last post by:
I've found an odd situation in where the Load event of the active form fires (after the Unload event) when Access is closed. I am able to reproduce this situation in Access 2002 SP3 and Access 2003...
7
by: | last post by:
Hello to all I'm handling the Validating event for one text box. If something is wrong in user input I show a warning message. The problem is that if I add to method the message is displayed...
7
by: Shane Bishop | last post by:
I've been fighting with the Page_Load event firing twice. I looked through this user group and saw several other people having similar problems. There were various reasons for it:...
1
by: Magdelin | last post by:
Hi, I have a BasePage class from which all other ASPX pages of the project is inherited. The BasePage class implements Page_Load event handler. All child pages have its AutoEventWireUp property...
5
by: Asa Monsey | last post by:
I am having a problem that the page load event fires twice in reponse to an autopostback. The first time, the IsPostBack property is true, and the second time it it false. This is causing many...
4
by: Larry Morris | last post by:
The following code, pasted into a web form with a link button on it, will cause the page_unload event to fire twice. If I remove the response.redirect, the problem goes away :). I've got a work...
4
by: Seraph | last post by:
Again, I'm rather new here, so if I fail to follow any etiquette, please forgive me and let me know what I've done wrong, but I think this might interest quite a few people. One of my colleaques...
2
by: ann | last post by:
I have a datagrid control I am using - the edit & update events are firing twice. I searched on google and see others have had same issue not just with datagrid but with other controls, but no...
2
by: Slim | last post by:
I have loop that builds table rows, it also attaches events, this works fine on all but the last row, where the event fires twice I have autowireup set to false any ideas? For i = 0 To...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.