write to a form textbox from static method

3,388 views
Skip to first unread message

Dickery1

unread,
Oct 31, 2008, 1:42:19 PM10/31/08
to DotNetDevelopment, VB.NET, C# .NET, ADO.NET, ASP.NET, XML, XML Web Services,.NET Remoting
i have issues in writing to a textbox from static method
private static void appendToTextBox(string str){
Form1.textBox1.Text += str + "\r\n";

}

An object reference is required for the nonstatic field, method, or
property 'collectTickData.Form1.textBox1'

how do i fix it. aparrently all the mehods calling appendToTextBox are
static and so i made it static too.
appreciate any help.

Joe Enos

unread,
Oct 31, 2008, 1:48:09 PM10/31/08
to DotNetDevelopment, VB.NET, C# .NET, ADO.NET, ASP.NET, XML, XML Web Services,.NET Remoting
You can't access an instance's textbox from a static method - the
static method does not even know whether or not an instance of the
form even exists, let alone which one is calling it.

If you can change your static methods so their not static, that would
be best - or you can pass the reference to the textbox (or the form
itself) around as a parameter, then you'll be able to access it.

Dickery1

unread,
Oct 31, 2008, 2:00:14 PM10/31/08
to DotNetDevelopment, VB.NET, C# .NET, ADO.NET, ASP.NET, XML, XML Web Services,.NET Remoting
i am pretty green to c#. can u provide an example or a link on how to
pass reference to the form or textbox.
appreciate ur help
> > appreciate any help.- Hide quoted text -
>
> - Show quoted text -

Joe Enos

unread,
Oct 31, 2008, 2:04:28 PM10/31/08
to DotNetDevelopment, VB.NET, C# .NET, ADO.NET, ASP.NET, XML, XML Web Services,.NET Remoting
Your code must start somewhere non-static - for example, a
button_click event handler. From there, you can call a static method
and just pass the thing in.

Suppose the following:
You have a textbox with ID txt1. The code gets started by clicking
btn1.
public void btn1_Click(object sender, EventArgs e)
{
DoSomething(txt1);
}
public static void DoSomething(TextBox textBox)
{
textBox.Text = "AAA";
}

Working with the form is a little different - if you can stick with
the textbox only, that will be the easiest and probably the most
readable.

Dickery1

unread,
Oct 31, 2008, 2:15:22 PM10/31/08
to DotNetDevelopment, VB.NET, C# .NET, ADO.NET, ASP.NET, XML, XML Web Services,.NET Remoting
gotcha. thank you very much
> > > - Show quoted text -- Hide quoted text -

Glenn

unread,
Oct 31, 2008, 2:15:50 PM10/31/08
to DotNetDe...@googlegroups.com
You pass a reference to it the same way as you pass any other parameter to the method.  Just add an additional parameter for the text box.
 
...Glenn

Cerebrus

unread,
Nov 1, 2008, 3:28:52 AM11/1/08
to DotNetDevelopment, VB.NET, C# .NET, ADO.NET, ASP.NET, XML, XML Web Services,.NET Remoting
But doesn't that solution render all the existing static methods which
call your function "appendToTextBox" ? I think you need to re-evaluate
your design and decide just what should be static (and why!)
Reply all
Reply to author
Forward
0 new messages