473,395 Members | 1,343 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,395 software developers and data experts.

overload or delegate?

Hi ,

I am trying to avoid behavior where everytime Button1Click is fired a new
form is exposed.

How can I keep the f object alive in the event so that a new object does not
have to be created?

Could I overload the Button Click event to accept the Form1 object?

void Button1Click(object sender, EventArgs e)

{

Form1 f = new Form1();

f.Show();

f.TextboxGet(this.textBox1.Text);

}

Thanks

D
Oct 30 '07 #1
3 1760
Dave,

You should have a field in the class itself for the Form1 instance. If
it is null, then create a new instance, and assign. You also want to hook
up to the Disposed event on the form, in the event that the form is closed.
Then in the event handler for the Disposed event, you would set the field
back to null.

Then, call Show on the instance.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"dave" <sp*****@nospammer.netwrote in message
news:Lu****************@bignews7.bellsouth.net...
Hi ,

I am trying to avoid behavior where everytime Button1Click is fired a new
form is exposed.

How can I keep the f object alive in the event so that a new object does
not
have to be created?

Could I overload the Button Click event to accept the Form1 object?

void Button1Click(object sender, EventArgs e)

{

Form1 f = new Form1();

f.Show();

f.TextboxGet(this.textBox1.Text);

}

Thanks

D


Oct 30 '07 #2
dave <sp*****@nospammer.netwrote:
I am trying to avoid behavior where everytime Button1Click is fired a new
form is exposed.

How can I keep the f object alive in the event so that a new object does not
have to be created?
You need to have the "child" form as a variable within the "parent"
form, so that the "parent" knows what to refer to when the button is
clicked.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Oct 30 '07 #3
On Oct 30, 1:12 pm, "dave" wrote:
Hi ,

I am trying to avoid behavior where everytime Button1Click is fired a new
form is exposed.

How can I keep the f object alive in the event so that a new object does not
have to be created?

Could I overload the Button Click event to accept the Form1 object?

void Button1Click(object sender, EventArgs e)

{

Form1 f = new Form1();

f.Show();

f.TextboxGet(this.textBox1.Text);

}

Thanks

D
I would probably have made the form2 a global variable since there is
only going to be one of them, but I realize that some people just
don't like that.

private void button1_click(object sender, eventargs e)
{
FormCollection fc = System.Windows.Forms.Application.OpenForms;
bool bFrmExists = false;
foreach (Form f in fc)
if (f.GetType() == typeof(Form2))
{
bFrmExists = true;
break;
}
if (!bFrmExists)
{
Form2 frm = new Form2();
frm.Show();
}
}

Oct 30 '07 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Piotre Ugrumov | last post by:
I'm following your help. I have written the overload of the operator <<. This overload work! :-) But I have some problem with the overload of the operator >>. I have written the overload of this...
3
by: Piotre Ugrumov | last post by:
I have done the overload on the operator >> and << in the class Attore. These 2 overload work correctly. I have done the overload of the same overload in the class Film. The class film ha inside...
17
by: Chris | last post by:
To me, this seems rather redundant. The compiler requires that if you overload the == operator, you must also overload the != operator. All I do for the != operator is something like this: ...
3
by: Minh Khoa | last post by:
Please give me more information about delegate and its usage? Why do i use it and when?
7
by: Ant | last post by:
Hello, Very simple question but one I need clarified. Which part of the statement below is considered the 'delegate'? Is it the 'new System.EventHandler' or the btnAccept_Click? or is it...
1
by: Quimbly | last post by:
I'm having some problems comparing delegates. In all sample projects I create, I can't get the problem to occur, but there is definitely a problem with my production code. I can't give all the...
8
by: not_a_commie | last post by:
I'd like to be able to overload the lock command so that I could log an entry on lock and unlock. Any ideas on how to do this? I think it would be powerful if you could inherit from a few...
5
by: jknupp | last post by:
In the following program, if the call to bar does not specify the type as <int>, gcc gives the error "no matching function for call to ‘bar(A&, <unresolved overloaded function type>)’". Since bar...
26
by: raylopez99 | last post by:
Here is a good example that shows generic delegate types. Read this through and you'll have an excellent understanding of how to use these types. You might say that the combination of the generic...
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
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
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...
0
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...

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.