WPF在单击时更新TextBlock

时间:2017-02-15 20:14:39

标签: c# asp.net wpf

我现在有3个按钮,每个按钮都执行一项特定任务。在桌面上打开网页或应用程序。但是,我想要一系列指令来遵循这些按钮,这就是我使用Textblock的原因。当我点击按钮时,我希望指示反映按下了哪个按钮,并在3之间不断更改。如何做到这一点?

    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";

1 个答案:

答案 0 :(得分: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。