Reputation: 55
`I want to change a textbox text in a static method. how can I do that, considering that i cannot use "this" keyword in a static method. In another words, how can I make an object refrence to a the textbox text property?
this is my code
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
public delegate void myeventhandler(string newValue);
public class EventExample
{
private string thevalue;
public event myeventhandler valuechanged;
public string val
{
set
{
this.thevalue = value;
this.valuechanged(thevalue);
}
}
}
static void func(string[] args)
{
EventExample myevt = new EventExample();
myevt.valuechanged += new myeventhandler(last);
myevt.val = result;
}
public delegate string buttonget(int x);
public static buttonget intostring = factory;
public static string factory(int x)
{
string inst = x.ToString();
return inst;
}
public static string result = intostring(1);
static void last(string newvalue)
{
Form1.textBox1.Text = result; // here is the problem it says it needs an object reference
}
private void button1_Click(object sender, EventArgs e)
{
intostring(1);
}`
Upvotes: 3
Views: 2243
Reputation: 5433
You got a perfect answer from dasblinkenlight. Here is an example of the 3rd method :
public static string result = intostring(1);
static void last(string newvalue)
{
Form1 form = (Form1)Application.OpenForms["Form1"];
form.textBox1.Text = result;
}
I'm not entirely sure why you are passing a string parameter and not using it though.
Upvotes: 0
Reputation: 726639
If you want to change an attribute of a non-static object from inside a static method, you need to obtain a reference to that object in one of several ways:
In any case, your static method must get a reference to the object in order to examine its non-static properties or call its non-static methods.
Upvotes: 3