2017-02-15 55 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

如果这回答了您的问题,则应将其标记为完整。 –