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

static controls

Hi
I have a control on my form, and in the code I decided to define it static.
every change i make to the design of the form, this control is back to being
non-static, and i have to change the code over and over to make it static
again.
is there a solution to this ?
Nadav
Apr 26 '06 #1
6 3656
I'm not sure that it makes much sense for either a Control (class) to be
declared static, nor for a Control (instance) on a form to be in a static
field (on the form).

In the former, you couldn't ever create an instance to place on any form; in
the latter, you have *no* thread safety (think affinity), *no* proper
control ownership (think TopLevelControl, disposal etc)...

In a very controlled situation, this /might/ be reasonable, but I can't
think of any...

Have I misunderstood your question? If so, could you post a short
illustration of what you are trying to do?

If this is, for instance, to share information between forms, then perhaps a
standalone class that the forms make use of (and get notifications from via
events) is a better model.

Marc
Apr 26 '06 #2
Hi,

First you have to ask yourself why this control is static??
static controls are a pain , remember that a control is contained in a form,
it has a parent and is in dependand of this to receive events.

IF for some weird need you have no choise but make it static, then you
cannot use the designer for it, the designer can change at will the code it
use to declare/initialize the controls

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation
"Nadav" <na****@gmail.com> wrote in message
news:ul**************@TK2MSFTNGP03.phx.gbl...
Hi
I have a control on my form, and in the code I decided to define it
static.
every change i make to the design of the form, this control is back to
being non-static, and i have to change the code over and over to make it
static again.
is there a solution to this ?
Nadav

Apr 26 '06 #3
"Nadav" <na****@gmail.com> a écrit dans le message de news:
ul**************@TK2MSFTNGP03.phx.gbl...

| I have a control on my form, and in the code I decided to define it
static.
| every change i make to the design of the form, this control is back to
being
| non-static, and i have to change the code over and over to make it static
| again.
| is there a solution to this ?

Yes, don't define it as static !!

Why would you want a control as static ? This means that you will only ever
have one instance of the control for all instances of the form. Every change
you make to such a control's properties would be reflected in all instances
of the form.

I would guess that you are editing the section of form code that is
commented as designer generated and should not be changed.

If you are using VS2005, you can add your control to the non-designer part
of the partial class and should not have the same problems.

Joanna

--
Joanna Carter [TeamB]
Consultant Software Engineer
Apr 27 '06 #4
Hi, thanks for the reply..
I did it static because I had to change it from another class.
Maybe you can tell me how I can get to the controls on the main form from
code of another class and not of the main form itself ?

thanks.

"Marc Gravell" <ma**********@gmail.com> wrote in message
news:%2****************@TK2MSFTNGP04.phx.gbl...
I'm not sure that it makes much sense for either a Control (class) to be
declared static, nor for a Control (instance) on a form to be in a static
field (on the form).

In the former, you couldn't ever create an instance to place on any form;
in the latter, you have *no* thread safety (think affinity), *no* proper
control ownership (think TopLevelControl, disposal etc)...

In a very controlled situation, this /might/ be reasonable, but I can't
think of any...

Have I misunderstood your question? If so, could you post a short
illustration of what you are trying to do?

If this is, for instance, to share information between forms, then perhaps
a standalone class that the forms make use of (and get notifications from
via events) is a better model.

Marc

Apr 27 '06 #5
Well, there is no such thing as "the main form" - it's just your app that
invents this.
Possible solutions:
* Pass a reference to the main form to each child form (can get messy when
lots of forms involved)
* If there genuinely is only ever one of these "main" forms, then you could
place it's reference into a static property of the main form; any other code
can then access it
* If the child forms are effectively notifying the main form of changes,
then use events
* If the child forms are reading data, then bind to data classes (i.e. each
form only knows about the *data*; it doesn't know about the UI on any other
form)
* About 20 other models...

Personally, I hate strongly coupling forms together; it makes it a pain to
re-use or refactor, painful to change the UI flow, and can lead to threading
issues etc. I recomment the use of events and separate data classes

If you have something specific, small and demonstrable, then post it - but
not reams and reams of stuff - just the essence.

Marc
Apr 27 '06 #6
I'm assuming that you want only one of your "main form" to exist at any
time, that you never want two of them.

If so, just make it a singleton:

public class MainForm : Form
{
private static MainForm _instance = null;

private MainForm()
{
... the usual constructor stuff ... note that it's "private"
....
}

public static MainForm Instance
{
get
{
if (MainForm._instance == null)
{
MainForm._instance = new MainForm();
}
return MainForm._instance;
}
}
}

Now whenever you want to get at your main form, just say:

MainForm.Instance

and you will get the (only) main form.

On top of this, it's very bad style to go directly into your form an
look at / monkey with controls on the form. It's far better to expose
properties and methods that allow other forms to ask for and do
specific things. For example, if you wanted to know what customer name
the user entered on the main form, this would be nasty:

string name = MainForm.Instance.customerTextBox.Text;

Yuck. This is much nicer:

string name = MainForm.Instance.CustomerName;

where CustomerName is a field on your main form:

public string CustomerName
{
get { return this.customerTextBox.Text; }
}

This accomplishes two things: 1) it gives you more control; perhaps you
want other forms to be able to see the customer name but not change it;
2) it allows you to later change the control for the customer name from
a text box to, say, a combo box, or a list view, and outside forms
don't need to know what happened.

Apr 27 '06 #7

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

Similar topics

0
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...
8
by: Cybertof | last post by:
Hello, I have a simple question : In the below code, why do i get an (ERROR) saying "An object reference is required for the nonstatic field, method, or property 'mLockeEnv3'" I don't...
2
by: superseed | last post by:
Hi, I'm pretty new to C#, and I'm quite stuck on the following problem. I would like to add to my application a Windows.Form (singleton) on which I could display a message of one of the...
1
by: Hadar | last post by:
Hi, I'm getting "object is currently in use elsewhere" when I use System.Drawing.Graphics.MesureString. This is what I do: My controls use a utility class the helps it to mesure strings. To...
3
by: Doug Handler | last post by:
I have a static method that needs to assign a type to an instance of an object. What's the best way to address this? e.g. private Panel pnlMain = new Panel(); ..... public static void...
1
by: Nadav | last post by:
hello, I have a form, with some controls I decided to define as static. Every time I change something in the form's design, InitializeComponent() is re-written and screws up the static...
1
by: uday | last post by:
hi all, i want to Increase the Font of the Labels before all the Controls. is there any approach to increase the Font of a Static Control that we palce before all the Controls to Identify. i...
7
by: Jon Slaughter | last post by:
I have some static fields in a class to keep track of "global" information but this information is local to each form that the class is used on. e.g., the class represents a base...
9
by: Tarscher | last post by:
hi all, I have this seemingly simple problem. I have lost a lot of time on it though. When a user selects a value from a dropdownlist (static control) a dynamic control is generated. I have...
0
by: Mahmoud Al-Qudsi | last post by:
Hi all, What is the recommended way of binding controls to a *static* datasource? In my project, I have to use the same ADO.net data source/ bindingsource in mulitple forms and across...
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: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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
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,...
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...

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.