I need to change a button text using an static function.
static string[] ARRay = new string[10];
[STAThread]
static void Main()
{
string[] args = Environment.GetCommandLineArgs();
for (int i = 1; i != args.Length; ++i)
{
command(i.ToString());
ARRay[0] = i.ToString();
}
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}
static void command(string line)
{
button1.Text = A开发者_如何学JAVARRay[0]; // here error
}
First of all, button1
seems to be the member of the Form Form1
. You need to first store the Form1
instance in the Main
method by first declaring a static
member:
static Form1 myForm;
and then creating and passing that instance to Run
method of Application
:
myForm = new Form1();
Application.Run(myForm);
After you do this, you will have two options:
1. If button1
is a public
Button
, you can use this line in your command
method:
myForm.button1.Text = ARRay[0];
Or 2. You can declare a public
method in Form1
class
to update the button1.Text
.
You can't change the text of a button before it exists.
You have to start the application first, or at least create the form that it will use, then you can get a reference to the button and store it in a static variable, where you can reach it from a static method.
Is button1 static? it should be to use it in a static method. check out this article for details.
精彩评论