我才开始使用 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.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_Click
)并在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;
}
}
关于c# - 从其他类/范围获取 var,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39241866/