By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,979 Members | 1,349 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,979 IT Pros & Developers. It's quick & easy.

static controls

P: n/a
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
Share this Question
Share on Google+
6 Replies


P: n/a
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

P: n/a
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

P: n/a
"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

P: n/a
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

P: n/a
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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.