Access to form elements from a different class / file

T

Tobias Froehlich

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 :)
 
T

Tobias Froehlich

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.
 
R

Roman S. Golubin1709176985

Hi, Tobias Froehlich!
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?

Your must change CheckBox modifiers to Public or Internal..
 
I

Ignacio Machin

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 SetCheckBoxStatus( bool checked)
{
foreach( CheckBox checkbox in checkboxarray )
{
checkbox.Checked = checked;
}
}


Hope this help,
 
T

Tobias Froehlich

hmm I've stumbled upon more problems like this.

I think i have a general misunderstanding 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..
 
L

Leon Jollans

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

Eliyahu Goldin

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

Eliyahu
 
L

Leon Jollans

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;
 
T

Tobias Froehlich

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?
 
T

Tobias Froehlich

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. :)
 
L

Leon Jollans

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(this.SomeMethod);
}
private void SomeMethod(object o, EventArgs e){
checkbox1.Checked = true;
}
hth
Leon
 
T

Tobias Froehlich

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?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top