2016-08-31 10 views
1

私はC#を始めましたが、レンガの壁に当たっています。私は、次のしている
他のクラス/スコープからvarを取得

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から)outputlabel1でそれを使用することができますか?

+1

のために行くされているものであればよく、あなたは 'ます。Label1.Text =出力を使用することができます;' 'button1_Click'に。それはあなたが意味することですか?これがWinFormsアプリケーションなどの場合は、他の作業をしている間はUIスレッドをブロックしないでください。 –

+0

'public string labelOutput {get;}のようなグローバルプロパティを定義します。 set;} 'あなたがそのプロパティに出力する代入。次にどこでも使えます – SilentCoder

答えて

3

これらのメソッドはすべて同じクラスにしている場合は、メンバ変数を使用します。

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; 
    } 
} 
+0

私はそれを試してみましたが、それは私のためにはうまくいかなかったのです。 –

2

あなたは、ボタンのクリックイベントから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(); 
    } 

それはあなたが

関連する問題