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

Access Form Controls From Another Class?

P: n/a
How can controls on a Windows Form be accessed (or referenced) from another
Class? I know how to do it from another Form. The following doesn't work
even though the Control Modifiers property is set to "Public":

frmMain.myControl

Thanks.

Michael
Nov 15 '05 #1
Share this Question
Share on Google+
3 Replies

P: n/a
100
Hi Michael,
What does *doesn't work* mean? Is there some compiler error message? It has
to work as long as the myControl is public. I suppose frmMain is the member
variable of the *other* class and it is initilized with the reference to the
form.
Make sure as well that the frmMain's type is the type that has myControl
field.

for example:
class MyForm: Form
{
public Control myControl = .....
}

if you have

Form frmMain = <reference to MyForm object>
and you try

frmMain.myConrtrol.....

it won't work because Form class doesn't have myControl member variable.

However, if you have

MyForm frmMain = <reference to MyForm object>

frmMain.myControl....

has to work.

HTH
B\rgds
100
"Michael Ramey" <ra*****@hotmail.com> wrote in message
news:eu*************@TK2MSFTNGP09.phx.gbl...
How can controls on a Windows Form be accessed (or referenced) from another Class? I know how to do it from another Form. The following doesn't work
even though the Control Modifiers property is set to "Public":

frmMain.myControl

Thanks.

Michael

Nov 15 '05 #2

P: n/a
The compiler error message is:

An object reference is required for the nonstatic field, method, or
property.

The following code allowed me to access the control:

frmMain frmActive = (frmMain)frmMain.ActiveForm;
frmActive.myControl...

Is there a better way?

Thanks.

Michael

"100" <10*@100.com> wrote in message
news:O2**************@tk2msftngp13.phx.gbl...
Hi Michael,
What does *doesn't work* mean? Is there some compiler error message? It has to work as long as the myControl is public. I suppose frmMain is the member variable of the *other* class and it is initilized with the reference to the form.
Make sure as well that the frmMain's type is the type that has myControl
field.

for example:
class MyForm: Form
{
public Control myControl = .....
}

if you have

Form frmMain = <reference to MyForm object>
and you try

frmMain.myConrtrol.....

it won't work because Form class doesn't have myControl member variable.

However, if you have

MyForm frmMain = <reference to MyForm object>

frmMain.myControl....

has to work.

HTH
B\rgds
100
"Michael Ramey" <ra*****@hotmail.com> wrote in message
news:eu*************@TK2MSFTNGP09.phx.gbl...
How can controls on a Windows Form be accessed (or referenced) from

another
Class? I know how to do it from another Form. The following doesn't work even though the Control Modifiers property is set to "Public":

frmMain.myControl

Thanks.

Michael


Nov 15 '05 #3

P: n/a
The confusion lies in your naming convention.

Since frmMain uses camel notation, a C# developer using MS's conventions
will naturally assume that it's a member/local variable.

Anyways, you need to pass a reference to the form to the object that you
want to access its control.

public class MyControlAccessor
{
public MyControlAccessor(frmMain mainForm)
{
mainForm.myControl.....
}
}

"Michael Ramey" <ra*****@hotmail.com> wrote in message
news:uG**************@tk2msftngp13.phx.gbl...
The compiler error message is:

An object reference is required for the nonstatic field, method, or
property.

The following code allowed me to access the control:

frmMain frmActive = (frmMain)frmMain.ActiveForm;
frmActive.myControl...

Is there a better way?

Thanks.

Michael

"100" <10*@100.com> wrote in message
news:O2**************@tk2msftngp13.phx.gbl...
Hi Michael,
What does *doesn't work* mean? Is there some compiler error message? It

has
to work as long as the myControl is public. I suppose frmMain is the

member
variable of the *other* class and it is initilized with the reference to

the
form.
Make sure as well that the frmMain's type is the type that has myControl
field.

for example:
class MyForm: Form
{
public Control myControl = .....
}

if you have

Form frmMain = <reference to MyForm object>
and you try

frmMain.myConrtrol.....

it won't work because Form class doesn't have myControl member variable.

However, if you have

MyForm frmMain = <reference to MyForm object>

frmMain.myControl....

has to work.

HTH
B\rgds
100
"Michael Ramey" <ra*****@hotmail.com> wrote in message
news:eu*************@TK2MSFTNGP09.phx.gbl...
How can controls on a Windows Form be accessed (or referenced) from

another
Class? I know how to do it from another Form. The following doesn't

work even though the Control Modifiers property is set to "Public":

frmMain.myControl

Thanks.

Michael



Nov 15 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.