C# 从其他类/范围获取var
我刚开始学C,但我遇到了麻烦。 我有以下资料:C# 从其他类/范围获取var,c#,visual-studio,C#,Visual Studio,我刚开始学C,但我遇到了麻烦。 我有以下资料: public void button1_Click(object sender, EventArgs e) { // Start the child process. Process p = new Process(); // Redirect the output stream of the child process. p.StartInfo.UseShellExecute = false; p.Star
public void button1_Click(object sender, EventArgs e)
{
// Start the child process.
Process p = new Process();
// Redirect the output stream of the child process.
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.FileName = "C:\\Windows\\Microsoft.NET\\Framework\\v3.5\\csc.exe";
p.StartInfo.Arguments = textBox1.Text;
p.Start();
// Do not wait for the child process to exit before
// reading to the end of its redirected stream.
// p.WaitForExit();
// Read the output stream first and then wait.
string output = p.StandardOutput.ReadToEnd();
MessageBox.Show(output);
p.WaitForExit();
}
public void label1_Click(object sender, EventArgs e)
{
label1.Text = ;
}
如何从public void button1获取输出?单击并在label1中使用它?如果这些方法都在同一个类中,请使用成员变量:
public class YourObject
{
private string _output;
public void button1_Click(object sender, EventArgs args)
{
// ...
_output = output;
}
public void label1_Click(object sender, EventArgs e)
{
label1.Text = _output;
}
}
如果这些方法都在同一个类中,请使用成员变量:
public class YourObject
{
private string _output;
public void button1_Click(object sender, EventArgs args)
{
// ...
_output = output;
}
public void label1_Click(object sender, EventArgs e)
{
label1.Text = _output;
}
}
您可以从按钮单击事件设置label1
public void button1_Click(object sender, EventArgs e)
{
// Start the child process.
Process p = new Process();
// Redirect the output stream of the child process.
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.FileName = "C:\\Windows\\Microsoft.NET\\Framework\\v3.5\\csc.exe";
p.StartInfo.Arguments = textBox1.Text;
p.Start();
// Do not wait for the child process to exit before
// reading to the end of its redirected stream.
// p.WaitForExit();
// Read the output stream first and then wait.
label1.Text = p.StandardOutput.ReadToEnd();
MessageBox.Show(output);
p.WaitForExit();
}
如果这就是您想要的您可以通过按钮单击事件设置label1
public void button1_Click(object sender, EventArgs e)
{
// Start the child process.
Process p = new Process();
// Redirect the output stream of the child process.
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.FileName = "C:\\Windows\\Microsoft.NET\\Framework\\v3.5\\csc.exe";
p.StartInfo.Arguments = textBox1.Text;
p.Start();
// Do not wait for the child process to exit before
// reading to the end of its redirected stream.
// p.WaitForExit();
// Read the output stream first and then wait.
label1.Text = p.StandardOutput.ReadToEnd();
MessageBox.Show(output);
p.WaitForExit();
}
如果这就是你想要的那么你可以使用label1.Text=output;在按钮1中单击。这就是你的意思吗?请注意,如果这是WinForms应用程序或类似应用程序,您确实不应该在执行其他操作时阻止UI线程。请定义一个全局属性,如public string labelOutput{get;set;},将输出分配给该属性。然后您可以在任何地方使用它,也可以使用label1.Text=output;在按钮1中单击。这就是你的意思吗?请注意,如果这是WinForms应用程序或类似应用程序,您确实不应该在执行其他操作时阻止UI线程。请定义一个全局属性,如public string labelOutput{get;set;},将输出分配给该属性。那你可以用在任何地方我试过这样的东西,但对我不起作用。我试过这样的东西,但对我不起作用。