Ok, lets say I make a new project in VS05. I create a button named button1 on a form named form1. Now I create another file and another class (Right Click, Add, Item... C# class). Now I am in another class in another file, but if I do "form1.BackColor" it doesnt work. I cant access the button either. I can also make a variable
public int test = 34;
Cant access it outside the form1 class either. I tried a lot of things (yes, including putting "public" keywords everywhere) but it wont work. Any ideas
Thanks
=== Edited by AlexTurp @ 12 Feb 2007 11:26 PM UTC===
Both classes are in the same namespace of course.

Class accessibility?
Christian Sparre
Thank you a lot for taking the time to answer me. Yet, this is no what I want. Lets forget about files and controls for a second. My real problem is that I cant access anything in Form1 for some reason... Here is an example:
using Blablabla;
namespace TestProject
{
public partial class Form1 : Form
{
public int Foo;
public Form1()
{
InitializeComponent();
}
}
public class Bar
{
public Bar()
{
Form.ActiveForm.Foo = 30; //Wont work
Form1.ActiveForm.Foo = 30; //Wont work
Form1.Foo = 30; //Wont work
Foo = 30; //Obiously wont work
}
}
}
Now the class Bar cant access Form1's Foo variable. Now this is understandable, since Form1 isnt a static class, I would need to specify the instance of the object I want to get Foo from. But where do I get that This class is constructed in Program.cs (a VS generated file) there:
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}
How can I access things inside this class in another way I couldnt even change the backcolor of the form if I wanted! (Actually, I had managed it once, doing Form1.ActiveForm.BackColor but I dont quite remember how I had managed to get it working).
So, to make things clear, my ultimate question is: How would I access Foo from the Bar class
ahmedilyas
Hi AlexTurp,
Some time back I started a thread on something similar regarding class visibility in a C# solution and I was happy with the response I got. Here is the link if you're interested.
http://forums.microsoft.com/MSDN/ShowPost.aspx PostID=1200237&SiteID=1
Patrice
Henrik Skak Pedersen
using NamespaceOfForm1;
to the top of the file using whatever namespace form1 is actually defined in.
Bjørn Liene Gundersen
Alex
Keith Newton
The "typical" workflow is to let Dev Studio do this work for you. There are two popular choices that I use:
First, (an maybe not exactly what you want) is to just drag a button object from the toolbox into your form. Then while the button is selected in the designer, you can look at it's properites to handle events on that button. A side benefit of this is that if you look inside the form1.Designer.cs class, you can see how that button's other attributes are handled (such as color). Just be sure that you don't play with that code in the region it says "hey... don't play with the code". You can use the normal form1.cs code after the InitializeCompent() call to set up other attributes of your button.
Method 2 (which I think might be what you are asking):
Under the assumption that you want to make a custom button that is reusable (the only reason I can come up with why you would want it in a seperate file from the form), then you can make a "User control". This is done by right-clicking your project in the solution view, then selecting "User control..." (and then redundantly insuring on the next screen that "User control" is what is selected.)...
After making your control by draging a button on to the new designer page, compile it (even if not done with it yet). After you compile, your new control will show up in the toolbox, where you can drag it onto your form, then you handle it just like any control you used in the toolbox.
Hope this is helps,
Jim Tomasko
FcoLomas
One way of doing this is by injecting it into the constructor of "Bar":
public class Bar
{
public Bar(Form1 form)
{
form.Foo = 30; //Obviously will work ;o)
}
}
You could also provide static members on Bar:
public class Bar
{
public Bar() {}
public static void DoSomeCrazyStuff(Form1 form)
{
form.Foo = 30; //Obviously will work ;o)
}
}
And then:
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Form1 form = new Form1();
Application.Run(form);
//Either one of the following
//Bar bar = new Bar(form);
//Bar.DoSomeCrazyStuff(form);
}
GiriKrishna
thanks for answering. I forgot to point out that both classes are in the same namespace tough. Thats why I really dont understand whats happening.
peter_fischer
Hi Alex,
Just to get this out of the way, I would set up the code like in my suggestion, and let Dev studio do the work for you... with that said, here we go...
If you look in Program.cs (which was created for you also, you will have a static Main function with a line of code that looks something like:
Application.Run(new Form1()); it should be in the same namespace "TestProject" as your example.
You could make a static member in your "Program" class for the form, and change the startup code as follows:
in class Program: static Form MyForm = null;
Then change the Run code to:
MyForm = new Form1();
Application.Run(MyForm);
Then you have a global variable to get the form. Once again, I have reservations about this, and think that if you need to do this you need to rethink your layout.
Or... I have not looked at this yet, but the Application class has a member named OpenForms, which returns a list of all open forms in your app, which would be a step up from making a global variable.
Jim Tomasko