473,385 Members | 1,326 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

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 :)
Nov 15 '05 #1
12 12456
What was the problem with Form1.CheckBox1? Is Form1 a class or an object? It
should be an object.

Eliyahu

"Tobias Froehlich" <re*******@gmx.net> wrote in message
news:hp********************************@4ax.com...
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 :)

Nov 15 '05 #2
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.
Nov 15 '05 #3
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,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation
"Tobias Froehlich" <re*******@gmx.net> wrote in message
news:hp********************************@4ax.com...
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 :)

Nov 15 '05 #4
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..
Nov 15 '05 #5
make all the Checkboxes public members. or internal (if you want to maintain
some encapsulation within the assembly)
"Tobias Froehlich" <re*******@gmx.net> wrote in message
news:0p********************************@4ax.com...
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.

Nov 15 '05 #6
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..

Nov 15 '05 #7
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" <re*******@gmx.net> wrote in message
news:44********************************@4ax.com...
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..

Nov 15 '05 #8
thanks, i'll try that out.
Nov 15 '05 #9
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?
Nov 15 '05 #10
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!
Nov 15 '05 #11
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


"Tobias Froehlich" <re*******@gmx.net> wrote in message
news:69********************************@4ax.com...
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?

Nov 15 '05 #12
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?

Nov 15 '05 #13

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Peter Frost | last post by:
Please help I don't know if this is possible but what I would really like to do is to use On Error Goto to capture the code that is being executed when an error occurs. Any help would be much...
7
by: dog | last post by:
I've seen plenty of articles on this topic but none of them have been able to solve my problem. I am working with an Access 97 database on an NT4.0 machine, which has many Access reports. I...
11
by: Grasshopper | last post by:
Hi, I am automating Access reports to PDF using PDF Writer 6.0. I've created a DTS package to run the reports and schedule a job to run this DTS package. If I PC Anywhere into the server on...
55
by: AnandaSim | last post by:
I just had a google through this NG but have not seen mention of Erik Rucker's blog entry and the new Jet: http://blogs.msdn.com/access/archive/2005/10/05/477549.aspx mentioned by Mike...
5
by: Lyle Fairfield | last post by:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dndotnet/html/callnetfrcom.asp The Joy of Interoperability Sometimes a revolution in programming forces you to abandon all...
4
by: SteveKlett | last post by:
I have a subset of form items that I need to perform different operations on (enable/disable, clear values, change style, etc) rather than hard code the IDs or names I would like to recursively...
11
by: Rik | last post by:
Hello guys, now that I'm that I'm working on my first major 'open' forms (with uncontrolled users I mean, not a secure backend-interface), I'd like to add a lot of possibilities to check wether...
5
by: wpmccormick | last post by:
What is the cleanest way to gain access to object methods and properties across classes and files in the same namespace? Example: A form object frmForm in file frmForm.cs creates obj1 defined in...
3
by: ibeehbk | last post by:
Hi. I have a form made in xhtml. I test via vbscript to make sure none of the fields are empty and properly formatted (ie email). All the regular fields work. However, I have two drop down menus...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.