tags:

views:

2361

answers:

4

Hello,

In the form.cs file I have two buttons,a memo and a timer.My question is: How do I access the timer or the memo from another cs file?

I've tried to make the objects public,but it didn't work,please give me a source or a project,so I can see where I'm mistaken.

Thanks!

+4  A: 
Koistya Navin
throw new StackOverflowException(); ;)
Mehrdad Afshari
http://pastebin.com/m183c1bcfIt throws an error,I have marked where the error is placed.Doesn't work.
John
You've edited your answer,I tried that already.When I try to write "Form1.MyButton" it doesnt recognize it.
John
@John: please see my answer below. Forms in .NET don't work like forms in VB6.
Jon B
@John, try to recompile your app.
Koistya Navin
+4  A: 

I worry whenever I hear someone talking about "another .cs file" or "another .vb file". It often (though not always) indicates a lack of understanding of programming, at least of OO programming. What's in the files? One class? Two?

You're not trying to access these things from another file, you're trying to access them from a method of a class, or possibly of a module in VB.

The answer to your question will depend on the nature of the class and method from which you're trying to access these things, and the reason why you want to access them.

Once you edit your question to include this information, the answers you receive will probably show you that you shouldn't be accessing these private pieces of the form in classes other than the form class itself.

John Saunders
+1 I suspect the problem goes much deeper than just the visibility of the properties.
tvanfosson
+1  A: 

Although I agree with John Saunders, one thing you may be doing wrong, assuming that you have everything accessible through public modifiers, is that you don't have the instance of that form.

For example, this is how you would do it:

Form1 myForm = new Form1;
string theButtonTextIAmLookingFor = myForm.MyButton.Text;

I am assuming that you may be trying to access it like it's static, like this:

string theButtonTextIAmLookingFor = Form1.MyButton.Text;

Just something you might want to check.

Aaron Daniels
A: 

From one of your comments to another answer it appears you're trying to access Form1 without a reference to it. Forms in .NET don't work the way forms in VB6 worked. You can't simply access Form1 with Form1.MyButton. The other form or other class from which you're accessing that form needs a reference to it.

This post has more info. VB.NET 2.0 actually mimics the VB6 behavior, but C# does not.

Jon B