473,699 Members | 3,239 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Static control, multiple references

Hello,

If I have a static control and 2 forms, each of which references the
static. When I return from the 2nd and go back to the first, the
reference to static control appears to have been removed altogether. It
disappears from the first form.

If you run the following example, click "DerivedFor m1" then close it
(ok). Then click "DerivedFor m2" and close that.

When you click DerivedForm1 again, the "Something" button has vanished.

I tested this on .NETCF. Is it a problem? If not, would someone explain
why this happens?

Cheers,
..pd.

using System;
using System.Drawing;
using System.Collecti ons;
using System.Windows. Forms;
using System.Data;

namespace ToggleSubscribe r
{
public class Form1 : System.Windows. Forms.Form
{
DerivedForm1 df1;
DerivedForm2 df2;
public static Button the_button;
public Form1()
{
the_button = new Button();
MinimizeBox = false;
Button b1 = new Button();
b1.Text = "DerivedFor m1";
b1.Size = new Size(100, 20);
b1.Location = new Point(60, 40);
Button b2 = new Button();
b2.Size = new Size(100, 20);
b2.Text = "DerivedFor m2";
b2.Location = new Point(60, 80);

b1.Click += new EventHandler(b1 _Click);
b2.Click += new EventHandler(b2 _Click);

Controls.Add(b1 );
Controls.Add(b2 );
}
protected override void Dispose( bool disposing )
{
base.Dispose( disposing );
}
static void Main()
{
Application.Run (new Form1());
}

private void b1_Click(object sender, EventArgs e)
{
if (df1 == null)
df1 = new DerivedForm1(th e_button);
df1.Show();
}

private void b2_Click(object sender, EventArgs e)
{
if (df2 == null)
df2 = new DerivedForm2(th e_button);
df2.Show();
}
}

public class BaseForm : Form
{
public delegate void OnSomethingHapp ens(object sender,
EventArgs e);
public event OnSomethingHapp ens SomethingHappen ed;
private Button b;

public BaseForm(Button btn)
{
MinimizeBox = false;
b = btn;
b.Location = new Point(60, 60);
b.Text = "Something" ;
b.Click += new EventHandler(b_ Click);
Controls.Add(b) ;
}

private void b_Click(object sender, EventArgs e)
{
if (SomethingHappe ned != null)
SomethingHappen ed(sender, e);
}
}

public class DerivedForm1 : BaseForm
{
BaseForm.OnSome thingHappens del;
public DerivedForm1(Bu tton btn) : base(btn)
{
del = new OnSomethingHapp ens(DerivedForm 1
_SomethingHappe ned);
SomethingHappen ed += del;

Closing += new System.Componen tModel.CancelEv entHandler
(DerivedForm1_C losing);
}

private void DerivedForm1_So methingHappened (object sender,
EventArgs e)
{
MessageBox.Show ("Something happened in DerivedForm1");
}

private void DerivedForm1_Cl osing(object sender,
System.Componen tModel.CancelEv entArgs e)
{
SomethingHappen ed -= del;
this.Visible = false;
e.Cancel = true;
}
}
public class DerivedForm2 : BaseForm
{
BaseForm.OnSome thingHappens del;
public DerivedForm2(Bu tton btn) : base(btn)
{
del = new OnSomethingHapp ens(DerivedForm 1
_SomethingHappe ned);
SomethingHappen ed += del;

Closing += new System.Componen tModel.CancelEv entHandler
(DerivedForm1_C losing);
}

private void DerivedForm1_So methingHappened (object sender,
EventArgs e)
{
MessageBox.Show ("Something happened in DerivedForm2");
}

private void DerivedForm1_Cl osing(object sender,
System.Componen tModel.CancelEv entArgs e)
{
SomethingHappen ed -= del;
e.Cancel = true;
this.Visible = false;
}
}
}

Nov 22 '05 #1
0 1276

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

Similar topics

5
2270
by: joe martin | last post by:
Hey all, For singleton code that looks like this: class JoeClass { friend JoeClass &joeFunction(); private: JoeClass() { someFunction();} }
0
284
by: .pd. | last post by:
Hello, If I have a static control and 2 forms, each of which references the static. When I return from the 2nd and go back to the first, the reference to static control appears to have been removed altogether. It disappears from the first form. If you run the following example, click "DerivedForm1" then close it (ok). Then click "DerivedForm2" and close that.
9
4157
by: Clint | last post by:
Hey all - Excuse the cross-post ... I'm not sure what the appropriate newsgroup would be for this question. I have a question that I'm not quite sure how to ask. For all I know, I have the verbaige completely wrong, but here goes nothing ... I'm currently using the MS Data Access Block for a desktop application I'm writing. Recently, I had to add a call to a web service, which in
4
14900
by: Gery D. Dorazio | last post by:
Gurus, If a static variable is defined in a class what is the scope of the variable resolved to for it to remain 'static'? For instance, lets say I create a class library assembly that is strongly name which contains the class where the static variable is defined. This library can be referenced by multiple projects. I am fairly sure the static variable does not survive across the application boundry but does it within the application...
0
1721
by: Tony Wong | last post by:
I am trying to implement the Singleton Pattern for a assembly (class) which control a common resource on my computer. I need the Singleton behavior within a single process which contain multiple AppDomains. I use a static member to count class instance like this, public __gc class foo: public IDisposable { public:
4
2045
by: Joe Fallon | last post by:
In another post Kevin Spencer stated: "one should be careful of using static fields, properties, and methods, by understanding what the implications of such are (e.g. locking static variables when changing because they are not thread-safe)." Can someone please elaborate on the pros and cons of using static methods? (In VB they are called Shared methods.) The MS DAAB uses Shared methods. This means that all Data Access calls go through...
5
6856
by: Tom Pearson | last post by:
What is the scope of static variable when programming in ASP.NET? For example I have a control class that uses static callbacks so that another window can pass a list of items to it. The control doesn't have access to this other object so needs to subscribe to the static callback. As such the static callback then needs to get hold of the real control instance which it can do via a member vaiarble which is set to the instance of the control on...
5
6273
by: | last post by:
Hi, How long do webservice objects live for? In particular, if i have static variables filled with data from a static constructor in a webservice, how long will that data persist? thxs
14
2569
by: Jeroen | last post by:
Hi all, I've got a question about writing a library. Let me characterize that library by the following: * there is a class A which is available to the user * there is a class B that is used in severel 'underwater operations' * there is a list which stores objects of class B There are several issues I'm not sure about:
4
4527
by: Dave | last post by:
I have a global.asax file with Application_Start defined and create some static data there and in another module used in the asp.net application and I realize that static data is shared amongst child apps of an IIS application and can be used by multiple users during the application life cycle and for multiple page loads for the same or different page under a root application. What I don't understand and need to know is whether that...
0
8706
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
8633
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9199
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9055
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...
0
8902
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7787
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5891
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4392
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...
3
2016
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.