Access Form Controls From Another Class?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Michael Ramey

    Access Form Controls From Another Class?

    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.myContr ol

    Thanks.

    Michael


  • 100

    #2
    Re: Access Form Controls From Another Class?

    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.myConrt rol.....

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

    However, if you have

    MyForm frmMain = <reference to MyForm object>

    frmMain.myContr ol....

    has to work.

    HTH
    B\rgds
    100


    "Michael Ramey" <rameymt@hotmai l.com> wrote in message
    news:eu8LKjffDH A.956@TK2MSFTNG P09.phx.gbl...[color=blue]
    > How can controls on a Windows Form be accessed (or referenced) from[/color]
    another[color=blue]
    > 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.myContr ol
    >
    > Thanks.
    >
    > Michael
    >
    >[/color]


    Comment

    • Michael Ramey

      #3
      Re: Access Form Controls From Another Class?

      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)frmMai n.ActiveForm;
      frmActive.myCon trol...

      Is there a better way?

      Thanks.

      Michael

      "100" <100@100.com> wrote in message
      news:O2rc8vffDH A.3024@tk2msftn gp13.phx.gbl...[color=blue]
      > Hi Michael,
      > What does *doesn't work* mean? Is there some compiler error message? It[/color]
      has[color=blue]
      > to work as long as the myControl is public. I suppose frmMain is the[/color]
      member[color=blue]
      > variable of the *other* class and it is initilized with the reference to[/color]
      the[color=blue]
      > 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.myConrt rol.....
      >
      > it won't work because Form class doesn't have myControl member variable.
      >
      > However, if you have
      >
      > MyForm frmMain = <reference to MyForm object>
      >
      > frmMain.myContr ol....
      >
      > has to work.
      >
      > HTH
      > B\rgds
      > 100
      >
      >
      > "Michael Ramey" <rameymt@hotmai l.com> wrote in message
      > news:eu8LKjffDH A.956@TK2MSFTNG P09.phx.gbl...[color=green]
      > > How can controls on a Windows Form be accessed (or referenced) from[/color]
      > another[color=green]
      > > Class? I know how to do it from another Form. The following doesn't[/color][/color]
      work[color=blue][color=green]
      > > even though the Control Modifiers property is set to "Public":
      > >
      > > frmMain.myContr ol
      > >
      > > Thanks.
      > >
      > > Michael
      > >
      > >[/color]
      >
      >[/color]


      Comment

      • Tyler Dixon

        #4
        Re: Access Form Controls From Another Class?

        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 MyControlAccess or
        {
        public MyControlAccess or(frmMain mainForm)
        {
        mainForm.myCont rol.....
        }
        }

        "Michael Ramey" <rameymt@hotmai l.com> wrote in message
        news:uGPJ6MgfDH A.2152@tk2msftn gp13.phx.gbl...[color=blue]
        > 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)frmMai n.ActiveForm;
        > frmActive.myCon trol...
        >
        > Is there a better way?
        >
        > Thanks.
        >
        > Michael
        >
        > "100" <100@100.com> wrote in message
        > news:O2rc8vffDH A.3024@tk2msftn gp13.phx.gbl...[color=green]
        > > Hi Michael,
        > > What does *doesn't work* mean? Is there some compiler error message? It[/color]
        > has[color=green]
        > > to work as long as the myControl is public. I suppose frmMain is the[/color]
        > member[color=green]
        > > variable of the *other* class and it is initilized with the reference to[/color]
        > the[color=green]
        > > 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.myConrt rol.....
        > >
        > > it won't work because Form class doesn't have myControl member[/color][/color]
        variable.[color=blue][color=green]
        > >
        > > However, if you have
        > >
        > > MyForm frmMain = <reference to MyForm object>
        > >
        > > frmMain.myContr ol....
        > >
        > > has to work.
        > >
        > > HTH
        > > B\rgds
        > > 100
        > >
        > >
        > > "Michael Ramey" <rameymt@hotmai l.com> wrote in message
        > > news:eu8LKjffDH A.956@TK2MSFTNG P09.phx.gbl...[color=darkred]
        > > > How can controls on a Windows Form be accessed (or referenced) from[/color]
        > > another[color=darkred]
        > > > Class? I know how to do it from another Form. The following doesn't[/color][/color]
        > work[color=green][color=darkred]
        > > > even though the Control Modifiers property is set to "Public":
        > > >
        > > > frmMain.myContr ol
        > > >
        > > > Thanks.
        > > >
        > > > Michael
        > > >
        > > >[/color]
        > >
        > >[/color]
        >
        >[/color]


        Comment

        Working...