Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to access non-static form elements in C# from a static method?

Status
Not open for further replies.

skhoury

IS-IT--Management
Nov 28, 2003
386
0
0
US
Hello all - I'm sorry for the newbie question, but I am still trying to grasp this.

I have a very simple form, and in the form I've added a RichTextBox control.

Within the Form1 class, I have some static variables and static methods - they have to be static because I am calling some lowlevel winapi functions via interop services.

I would like to write data from this static method into the RichTextBox but I always get the following error upon compilation:

'WindowsApplication1.Form1.richTextBox1' denotes a 'field' where a 'class' was expected.

Can someone please point me in the right direction with respect to solving this issue?

Thanks all,

Sam
 
Declare a variable of type Form1 within that static method.

So,

Code:
Form1 form = new Form1();
form.richTextBox1 = "Some new text";

----------------------------------------

TWljcm8kb2Z0J3MgIzEgRmFuIQ==
 
whaaaaaaaat?

If you are calling static WINAPI calls then you should put those calls into a separate class - not in your form code!

So the instance of your form should make the call:

this.richtextbox1.Text = WinAPIClass.SomeCall();

or however you get your text.
 
Thanks guys...Guru, your method doesn't work.

JurkMonkey - you're correct, it shouldn't be in the form code however I am just alpha testing an idea so this isn't final code by any means.

The way I did resolve it however is to basically declare the RTB as static and add it to the form "manually" rather than reliying on the designer....and it worked!

Thanks all,

Sam
 
This is basic object-oriented design.

You cannot call an instance method from within a static method (no matter what the type of the enclosing class).

The only way is to "cheat" by passing an instance into the method for it to use.

Chip H.


____________________________________________________________________
If you want to get the best response to a question, please read FAQ222-2244 first
 
chiph - while you're correct, my method above did in fact work....and it does follow correct oop design. I simply made the rtb object static to the form1 class rather than private.
 
Yup, that'll work.

Just make sure you aren't doing any multi-threading in this bit of code. Static methods aren't naturally thread-safe, so you'd have to put a lock{} statement in there.

Chip H.


____________________________________________________________________
If you want to get the best response to a question, please read FAQ222-2244 first
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top