Access to form elements from a different class / file

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Tobias Froehlich

    Access to form elements from a different class / file

    Hi,

    I have a form called form1 on which there are a lot of checkBoxes
    (they form a 8x6 field). I'd like to create a class in a seperate .cs
    file which gives some control over these checkBoxes (for example to
    make them all unchecked).

    So I now created a .cs file for this class, but I can't access the
    checkBox elements on form1. They are both in the same namespace by the
    way. What do I need to do? I tried to access it via Form1.CheckBox1
    and CheckBox1. I did not use inheritance, do i need to?

    Thanks in advance.

    PS: Yes i am a newbie :)
  • Eliyahu Goldin

    #2
    Re: Access to form elements from a different class / file

    What was the problem with Form1.CheckBox1 ? Is Form1 a class or an object? It
    should be an object.

    Eliyahu

    "Tobias Froehlich" <reptile2k@gmx. net> wrote in message
    news:hpptmvs7o4 nb3vk2offhal1ft apdav37aa@4ax.c om...[color=blue]
    > Hi,
    >
    > I have a form called form1 on which there are a lot of checkBoxes
    > (they form a 8x6 field). I'd like to create a class in a seperate .cs
    > file which gives some control over these checkBoxes (for example to
    > make them all unchecked).
    >
    > So I now created a .cs file for this class, but I can't access the
    > checkBox elements on form1. They are both in the same namespace by the
    > way. What do I need to do? I tried to access it via Form1.CheckBox1
    > and CheckBox1. I did not use inheritance, do i need to?
    >
    > Thanks in advance.
    >
    > PS: Yes i am a newbie :)[/color]


    Comment

    • Tobias Froehlich

      #3
      Re: Access to form elements from a different class / file

      Form1 is what VS.NET created automatically, I think that should be
      correct.

      Maybe I'll just leave it all in one file and sort things up a little
      with #region.

      Comment

      • Ignacio Machin

        #4
        Re: Access to form elements from a different class / file

        Hi Tobias,

        You need to do some things, first you have make accesible the checkboxes to
        the other class, you can do this by declaring them as public, later you need
        to pass a reference for the form1 object to the constructor or a method of
        the controller class, or if you use only one instance of form1 you can
        declare the checkboxes as static and then you do not have to pass the
        reference to the object.


        Said that, I would suggest you what I think is a better way, I would not
        create a separate class for this, I would implement that functionality
        inside the Form1 class, I would create an array ( an ArrayList in case you
        need to add/remove some of them in code ) with the reference to the
        checkboxes to be able to deal with them as a group, instead of specifying
        each one by name. You can create then functions as UncheckAll that would
        looks like this:

        public void SetCheckBoxStat us( bool checked)
        {
        foreach( CheckBox checkbox in checkboxarray )
        {
        checkbox.Checke d = checked;
        }
        }


        Hope this help,

        --
        Ignacio Machin,
        ignacio.machin AT dot.state.fl.us
        Florida Department Of Transportation


        "Tobias Froehlich" <reptile2k@gmx. net> wrote in message
        news:hpptmvs7o4 nb3vk2offhal1ft apdav37aa@4ax.c om...[color=blue]
        > Hi,
        >
        > I have a form called form1 on which there are a lot of checkBoxes
        > (they form a 8x6 field). I'd like to create a class in a seperate .cs
        > file which gives some control over these checkBoxes (for example to
        > make them all unchecked).
        >
        > So I now created a .cs file for this class, but I can't access the
        > checkBox elements on form1. They are both in the same namespace by the
        > way. What do I need to do? I tried to access it via Form1.CheckBox1
        > and CheckBox1. I did not use inheritance, do i need to?
        >
        > Thanks in advance.
        >
        > PS: Yes i am a newbie :)[/color]


        Comment

        • Tobias Froehlich

          #5
          Re: Access to form elements from a different class / file

          hmm I've stumbled upon more problems like this.

          I think i have a general misunderstandin g on how I can access
          different classes and objects in my file from different sources.

          For example, I have the static void Main() in Form1 (all in a
          Namespace called NeuroDemo). How can I access the functions that are
          declared outside the main function? It works if i access e.g. the
          checkBox Functions from the cmdClick event function. But how do i get
          there from Main() ?

          I'm kinda confused..

          Comment

          • Leon Jollans

            #6
            Re: Access to form elements from a different class / file

            make all the Checkboxes public members. or internal (if you want to maintain
            some encapsulation within the assembly)


            "Tobias Froehlich" <reptile2k@gmx. net> wrote in message
            news:0pqtmvk8tj ihbmq5kjd4miq19 cshqauv55@4ax.c om...[color=blue]
            > Form1 is what VS.NET created automatically, I think that should be
            > correct.
            >
            > Maybe I'll just leave it all in one file and sort things up a little
            > with #region.[/color]


            Comment

            • Roman S. Golubin1709176985

              #7
              Re: Access to form elements from a different class / file

              Hi, Tobias Froehlich!
              [color=blue]
              >
              > I have a form called form1 on which there are a lot of checkBoxes
              > (they form a 8x6 field). I'd like to create a class in a seperate .cs
              > file which gives some control over these checkBoxes (for example to
              > make them all unchecked).
              >
              > So I now created a .cs file for this class, but I can't access the
              > checkBox elements on form1. They are both in the same namespace by the
              > way. What do I need to do? I tried to access it via Form1.CheckBox1
              > and CheckBox1. I did not use inheritance, do i need to?[/color]

              Your must change CheckBox modifiers to Public or Internal..



              Comment

              • Leon Jollans

                #8
                Re: Access to form elements from a different class / file

                Main is static.
                The non-static form instance has the controls you're interested in, unless
                you're running a static form - which I don't think you can do.

                in Main()

                Form1 form1 = new Form1();
                form1.Show();
                form1.CheckBox1 .Checked = true;



                "Tobias Froehlich" <reptile2k@gmx. net> wrote in message
                news:44rtmvgvh5 ktklcr1s0le3chi ddmdqdsbt@4ax.c om...[color=blue]
                > hmm I've stumbled upon more problems like this.
                >
                > I think i have a general misunderstandin g on how I can access
                > different classes and objects in my file from different sources.
                >
                > For example, I have the static void Main() in Form1 (all in a
                > Namespace called NeuroDemo). How can I access the functions that are
                > declared outside the main function? It works if i access e.g. the
                > checkBox Functions from the cmdClick event function. But how do i get
                > there from Main() ?
                >
                > I'm kinda confused..[/color]


                Comment

                • Tobias Froehlich

                  #9
                  Re: Access to form elements from a different class / file

                  thanks, i'll try that out.

                  Comment

                  • Tobias Froehlich

                    #10
                    Re: Access to form elements from a different class / file

                    I now made all the form controls public and am trying to do what you
                    said, but i have a problem with how I can get the non-static instance
                    into the Application.Run .

                    I mean, so far it says like this:

                    Form1 form1 = new Form1();
                    form1.Show();
                    Application.Run (new Form1());

                    but this opens two forms obviously. What am i doing wrong here?

                    Comment

                    • Tobias Froehlich

                      #11
                      Re: Access to form elements from a different class / file

                      Thanks for all your help. I'll try this out later (i'll probably have
                      some more questions..)


                      Ignacio Machin:

                      I still think i'm going to do each of the 63 checkboxes seperately. That
                      sounds a bit dumb yes, but you have to see that i'm really a newbie and
                      I like to have things as understandable to myself as possible, and that
                      would be editing each of the checkBoxes seperately. :)







                      *** Sent via Developersdex http://www.developersdex.com ***
                      Don't just participate in USENET...get rewarded for it!

                      Comment

                      • Leon Jollans

                        #12
                        Re: Access to form elements from a different class / file

                        You shouldn't really be doing this, Application.Run shows the form - the new
                        Form1() argument creates another instance - hence two forms. You could do

                        Form1 form = new Form1();
                        Application.Run (form);

                        if you wanted to get another reference, but there's not much point to it
                        unless you wanted to perform some pre-processing on the form before kicking
                        it off.

                        you should do your assignments to the checkboxes in a Form.Load event
                        handler.

                        public Form1(){
                        this.Load += new EventHandler(th is.SomeMethod);
                        }
                        private void SomeMethod(obje ct o, EventArgs e){
                        checkbox1.Check ed = true;
                        }
                        hth
                        Leon




                        "Tobias Froehlich" <reptile2k@gmx. net> wrote in message
                        news:69vtmvsts2 9rljq60p1p37kl4 ntnohae2f@4ax.c om...[color=blue]
                        > I now made all the form controls public and am trying to do what you
                        > said, but i have a problem with how I can get the non-static instance
                        > into the Application.Run .
                        >
                        > I mean, so far it says like this:
                        >
                        > Form1 form1 = new Form1();
                        > form1.Show();
                        > Application.Run (new Form1());
                        >
                        > but this opens two forms obviously. What am i doing wrong here?[/color]


                        Comment

                        • Tobias Froehlich

                          #13
                          Re: Access to form elements from a different class / file

                          But I also need to change the checkBox settings after loading the form
                          (all of them at once.. and not all of them get the same setting!)..
                          will that be possible that way?

                          Comment

                          Working...