简体   繁体   English

如何使C#按钮仅单击一次并使文本框显示两个结果?

[英]How to make C# button click only one time and make a textbox show two result?

I write below simple code. 我写下面的简单代码。 I want to make this happen: 1. Click button. 我想做到这一点:1.单击按钮。 2. Show the first string. 2.显示第一个字符串。 3. Wait for 3 seconds. 3.等待3秒钟。 4. Show the second string. 4.显示第二个字符串。 But with this code, i can only see the second string after i click the button and wait for 3 seconds. 但是使用此代码,我仅在单击按钮并等待3秒钟后才能看到第二个字符串。 i can't see the first string. 我看不到第一个字符串。 is there any way to make this happen? 有什么办法可以做到这一点?

namespace clicktest
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {

        public MainWindow()
        {
            InitializeComponent();
        }
        private void button_Click(object sender, RoutedEventArgs e)
        {
            this.textBox.Text = "Show 1st time";
            Thread.Sleep(3000);
            this.textBox.Text = "Show 2nd time";
        }
    }
}

Blocking a thread, such as when using Thread.Sleep , in WPF prevents the GUI from refreshing. 阻塞线程(例如在WPF中使用Thread.Sleep时)会阻止GUI刷新。

Instead you can use await Task.Delay which won't block the thread: 相反,您可以使用await Task.Delay ,它不会阻塞线程:

private async void button_Click(object sender, RoutedEventArgs e)
{
    this.textBox.Text = "Show 1st time";
    await Task.Delay(3000);
    this.textBox.Text = "Show 2nd time";
}

As @Alexander Higgins said, you can use await Task.Delay , but you have to add the async type to your method, like this: 正如@Alexander Higgins所说的,您可以使用await Task.Delay ,但是您必须将异步类型添加到您的方法中,如下所示:

private async void button_Click(object sender, RoutedEventArgs e)
{
    this.textBox.Text = "Show 1st time";
    await Task.Delay(3000);
    this.textBox.Text = "Show 2nd time";
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我如何使messageBox在单击按钮的时限内不显示? C# - How would I make a messageBox not show within a time limit of a button click? c# 如何返回两个值但仅在文本框中显示一个并在 C# 中隐藏另一个 - How can I return two values but only show one in a textbox and hide the other in C# 如何使文本框仅在用户单击C#中的按钮后出现 - How to make textbox appear only after user clicks button in c# c#单击按钮时将变量值设置为“”,但仅在对话框中 - c# Make a variable value “” on button click, but only to the dialog box 如何使文本框只接受出生日期格式 c# - how to make a textbox accept only date of birth format c# 如何在C#中制作一个必须在特定时间范围内在特定位置单击的按钮 - How to make a button that you must click at certain places within a certain time frame in C# C#:两步文本框计算仅在两次单击“计算”按钮时才产生正确的结果 - C#: A two step textbox calculation is only yielding the correct result when clicking my calculate button twice 如何在 c# 应用程序的 showdialog() 上自动单击确定按钮? - how to make automatic click ok button on showdialog() on c# application? 如何使方法接收两种不同的数据类型并只返回其中一种C#? - How to make a method receive two different data types and return only one of them C#? C# 如何使 UWP XAML 按钮在悬停时不显示边框 - C# How to make UWP XAML button not show border on hover
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM