473,698 Members | 2,869 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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_initial ized 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 DoThatThingWith Foo(Foo as Object)

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

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

'Good, that's done
frmThing.Dispos e()

End Sub
Nov 20 '05 #1
4 9087
"C M Shaw" <cs***@collegeb oard.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.DoSome Setup(Foo)
frmThing.ShowDi alog()

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.EventArg s) Handles MyBase.Load
' Include some error checking here...
DoSomeSetup( FooObject )
End Sub
End Class

frmThing = New ThingForm()
frmThing.FooObj ect = Foo
frmThing.ShowDi alog()

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.
InitializeCompo nent()

'Controls can be accessed at this point
DoSomeSetup(foo Object)

End Sub

frmThing = New ThingForm( Foo )
frmThing.ShowDi alog()

Hope this helps

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

"C M Shaw" <cs***@collegeb oard.com> wrote in message
news:4c******** *************** ***@posting.goo gle.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_initial ized 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 DoThatThingWith Foo(Foo as Object)

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

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

'Good, that's done
frmThing.Dispos e()

End Sub

Nov 20 '05 #3
"Chris Taylor" <ch************ *@hotmail.com> wrote in message news:<#z******* *******@tk2msft ngp13.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 DoDifferentSetu p(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.publi c.dotnet.framew ork.windowsform s
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
2452
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 on computers running WinXP. This does not appear to happen in Access 2000 on a computer with Win2000. This only seems to happen if there is a hidden form also loaded and, so far, I'm only able to reproduce this if the forms are loaded through...
7
7769
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 twice (event fired twice). Without event is fired once. if(txtMSG.Length > 0) {
7
2343
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: AutoEventWireup="true" instead of AutoEventWireup="false" Spyware software And having your events all wired wrong I had done everything to my machine, stripped it down to just have ..NET on it and I was still having this problem until...
1
6080
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 set to false. I have included the code used to create the BasePage and one of the child pages below. // Class BasePage public class BasePage : Page {
5
7530
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 uneeded database calls, since many of them do not need to be fired on a PostBack. I have already set the AutoEventWireup=false in the ASPX @Page directive. I am setting the visible property of a datagrid in some of my methods, but I commented...
4
6090
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 around, but I'm curious how one is supposed to programmatically navigate between web pages without the page_unload event firing twice. Thanks, Larry
4
3981
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 was endeavoring to create a custom user control to make things a bit simpler, but she noticed that her Page_Load eventhandler was firing twice. So after long hours of research and experimentation, I stumbled upon, imho, is quite the discovery. ...
2
1450
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 resolution except to try "AutoEventWireup="false" , which it already is and it doesn't fix it. Can anyone help me? c# & net 1.1 thanks
2
1130
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 UBound(oCountries.getCountries)
0
8674
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9026
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8893
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
6518
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4366
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4619
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3045
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2328
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2001
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.