2017-02-15 4 views
-1

私は3つのボタンを持っていますが、それぞれが特定のタスクを行います。デスクトップ上のWebページまたはアプリケーションを開きます。しかし、私はこれらのボタンに従う一連の指示をしたいので、なぜ私はTextblockを使用しています。私がボタンをクリックすると、どのボタンが押されたかを反映させる指示が欲しく、それは3の間で絶えず変化しています。これはどのように行われますか?WPFクリック時のTextBlockの更新

protected void passButton_Click(object sender, RoutedEventArgs e) 
    { 
     // I want to be able to surround this is a try catch block statement 
     // because there is a "Run as Admin" A lot of users may say no and it will crash. 
     Process myProcess = new Process(); 
     String pathWay = "C:\\blah.txt"; 

     try 
     //Although we have a working enviornment here we still need to be able to run it as an administrator always 
     //This would eliminate the need of a "Try/Catch" statement. 
     { 
      myProcess.StartInfo.FileName = pathWay; 
      myProcess.StartInfo.UseShellExecute = true; 
      myProcess.StartInfo.Verb = "runas"; 
      myProcess.Start(); 

     } 
     //Fixed the bug where the app would crash when the user would enter "No" instead of "yes" the App would not launch in this case. 
     catch 
     { 
      Console.WriteLine("Enter so value here for logs later down the road if we run into the problem."); 
     } 

     // Here is where I want to be able to change the value incase the user 
     // clicks another button as of right now the text block updates once 
     // the block is clicked with a simple "Hello World" but if I click of the value remains the same 

     textBlock.Text = "Hello world"; 
+0

他の人がそれをデバッグするのに役立つように、アプリケーションコードの簡略化されたバージョンを投稿できますか? – laylarenee

+0

ちょっとLaylarenee、私はちょうど上記のコードのいくつかを追加しました。私はそれがいくつかをクリアすることを願っています! – RyanWilliamWest

+0

3つのボタンのクリックハンドラを追加できますか?テキストを設定するビットを含めるだけです。 – laylarenee

答えて

0

どのボタンがクリックされたかに応じてテキストボックスを更新するのがいいですか?その場合は(@laylareneeが提案のように)、私はこれに似た何かをすることにより、クリックハンドラをお勧めします:

private bool btn1_Clicked = false; 

private void button1_Click(object sender, EventArgs e) 
{ 
    btn1_Clicked = true; 
} 

繰り返しをすべての3つのボタン(btn2_Clicked、btn3_Clicked)のためのこと。これはかもしれ

if(btn1_Clicked == true) 
{ 
    // put your textbox update in here 
} 

:あなたは(あなたが上記の行っているような)ボタンハンドラにロジックを入れたいなら、あなたは、あなたが設定され、これらの変数をチェックして、テキストボックスを更新update_Textと呼ばれる別の関数を作ることができます少し長い道のりではあるが、確かに仕事を終わらせるだろう。他のボタンをクリックしてクリーンな状態にするには、他のボタンのクリック変数をfalseに戻してください。

+0

ありがとうございました!うん、Click Handlingが実際に私のために働いてしまったのですが、私は各トピックについてこういうことをしました。 var tb = sentender as TextBlock; if(tb == null) { textBlock.Text = "それを処理中"; } – RyanWilliamWest

+0

これがあなたの質問に答えた場合は、完全とマークする必要があります。 –

関連する問題