2017-02-15 95 views
-1

我現在有3個按鈕,每個按鈕都執行特定的任務。在桌面上打開網頁或應用程序。但是,我想要一系列指令來遵循這些按鈕,因此我使用了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

你可以爲這三個按鈕中的每一個添加點擊處理程序嗎?您只需要包含設置文本的位。 – laylarenee

回答

0

什麼是你想要的聲音是一個文本框來更新取決於哪個按鈕被點擊?如果是這樣的話,我會建議點擊處理程序(如@laylarenee建議)做一些與此類似:

private bool btn1_Clicked = false; 

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

重複,對於所有3個按鈕(btn2_Clicked,btn3_Clicked)。如果你想把你的邏輯在按鈕處理程序(如你在上面所做的),那麼你可以創建一個名爲UPDATE_TEXT另一個函數,你檢查你所設置的變量和更新文本框:

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

這可能是一個有點長途跋涉,但肯定會完成工作。只要確保將其他按鈕點擊變量設置爲false,如果單擊另一個按鈕以保持乾淨。

+0

謝謝!是的,點擊處理實際上最終爲我工作我最終爲每個主題做了這樣的事情: var tb = sender as TextBlock; if(tb == null) { textBlock.Text =「Working on it」; } – RyanWilliamWest

+0

如果這回答了您的問題,則應將其標記爲完整。 –