views:

41

answers:

1

Hi,

C# question: I have a textBox in a default form Form1 and I want to access (write on) it from a constructor in another class that is in another different file. How can I do it?

Tx in advance, Gerard

+1  A: 

You need to make a property in the Form1 class that exposes the textbox or its Text property, then pass an instance of the Form1 class to the constructor. (Or as a static proeprty)

SLaks
I like the static option, since constructor param would defocus from the feature I want to emphasize: Constructor overloading ;-).Tx SLacks
Well static seemed a good alternative but it raises an Error: "An object reference is required for the non-static field, method, or property 'Form1.richTextBox1'". Any clue?
You need to make a static property of type `Form1`, then access the property. For example, if your property is on the `Form1` class and is named `Instance`, you would write `Form1.Instance.richTextBox1`. Make sure to make it public.
SLaks
oops did you tried out? When declare the in the class Form1 the property: public static Form1 frm { get { return frm; } set { frm = value; } }In my class I can't access the controls like you mentioned Form1.frm.richTextBox1, no richTextBox1 or other controls from the form are accessible.What am I missing!?Tx onde more.
I tried no make the richTextBox1 public (I think I was missing it) but now I get a runtime error: "An unhandled exception of type 'System.StackOverflowException' occurred in myapp.exe" Can you help?
*sorry I meant to write "I tried TO make the richTextBox1 public"
Got itJust declared a "private static Form1 frm = null" and redefined the Property to public static Form1 Frm { get { return frm; } set { frm = value; } } and most important and the source off all problems I initialized the frm = this in the Form1 construtor.Tx anyway.