如何在 WPF 中设置计时器开始时间文本框值?

发布于 2025-01-16 19:59:25 字数 2031 浏览 2 评论 0原文

我正在创建计时器。我有两个窗口主窗口和设置。 主窗口有两个按钮。 StartStopBtnShowSettingsWindow设置有一个SetTimeBtntimerTxb

MainWindow.xaml 在此处输入图像描述

我将计时器开始时间声明为 int,如何我可以获取文本框中的值而不是 int start = 10 吗?

MainWindow.xaml.cs

    public partial class MainWindow : Window
{
    DispatcherTimer timer;
    public Settings settings;
    public int start = 10;
    // Here I want to assign settings.timerTxb.Text value to a variable instead of int start = 10;
    int start =Convert.ToInt32(settings.timerTxb.Text); // This code is not working

    public MainWindow()
    {            

        InitializeComponent();
        timer = new DispatcherTimer();            
        timer.Interval = new TimeSpan(0, 0, 1);
        timer.Tick += Timer_Tick;
    }

    private void Timer_Tick(object sender, EventArgs e)
    {
        start--;            
        StartStopBtn.Content = start;          
    }

    private void StartStopBtn_Click(object sender, RoutedEventArgs e)
    {
        
        if (timer.IsEnabled == false)
        {
            timer.Start();
            StartStopBtn.Content = start;
        }
        else
        {
            timer.Stop();
            StartStopBtn.Content = start;
        }
    }

    private void showWindow_Click(object sender, RoutedEventArgs e)
    {
        settings = new Settings();
        settings.Show();
    }        
}

在此处输入图像描述

Settings.xaml 在此处输入图像描述

我在该程序中将计时器开始时间声明为 int 类型。但我需要在文本字段中输入计时器开始时间。主窗口中的 StartStopBtn 按钮应显示计时器时间。该按钮必须同时是开始(恢复)和停止按钮。 我将计时器开始时间声明为 int,如何获取文本框中的值而不是 int start = 10?

I am creating the timer. I have two windows Mainwindow and Settings. The Mainwindow has two buttons. StartStopBtn and ShowSettingsWindow.
Settings has a SetTimeBtn and timerTxb.

MainWindow.xaml
enter image description here

I declared the timer start time as int, how can I get the value in the text box instead of int start = 10?

MainWindow.xaml.cs

    public partial class MainWindow : Window
{
    DispatcherTimer timer;
    public Settings settings;
    public int start = 10;
    // Here I want to assign settings.timerTxb.Text value to a variable instead of int start = 10;
    int start =Convert.ToInt32(settings.timerTxb.Text); // This code is not working

    public MainWindow()
    {            

        InitializeComponent();
        timer = new DispatcherTimer();            
        timer.Interval = new TimeSpan(0, 0, 1);
        timer.Tick += Timer_Tick;
    }

    private void Timer_Tick(object sender, EventArgs e)
    {
        start--;            
        StartStopBtn.Content = start;          
    }

    private void StartStopBtn_Click(object sender, RoutedEventArgs e)
    {
        
        if (timer.IsEnabled == false)
        {
            timer.Start();
            StartStopBtn.Content = start;
        }
        else
        {
            timer.Stop();
            StartStopBtn.Content = start;
        }
    }

    private void showWindow_Click(object sender, RoutedEventArgs e)
    {
        settings = new Settings();
        settings.Show();
    }        
}

enter image description here

Settings.xaml
enter image description here

I declared the timer start time in this program as an int type. But I need to enter the timer start time in the text field. The StartStopBtn button in the Mainwindow should show the timer time. This button must be both start (resume) and stop.
I declared the timer start time as int, how can I get the value in the text box instead of int start = 10?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

灯下孤影 2025-01-23 19:59:25

步骤 1:您可以使用应用程序设置来存储您的值,首先您需要添加一个条件来检查文本框输入是否仅为数字,在“设置”页面上向您的文本框添加一个 PreviewTextInput 事件。

C#:

    private void timerTxb_PreviewTextInput(object sender, TextCompositionEventArgs e)
    {
        System.Text.RegularExpressions.Regex InputChecker = new System.Text.RegularExpressions.Regex("[^0-9]+");
        e.Handled = InputChecker.IsMatch(e.Text);
    }

XAML:

<StackPanel Orientation="Horizontal" Background="LightGray">
    <Label Content="Set Timer" HorizontalAlignment="Left" VerticalAlignment="Top"></Label>
    <TextBox x:Name="timerTxb" Width="Auto" MinWidth="100" HorizontalAlignment="Left" VerticalAlignment="Top" PreviewTextInput="timerTxb_PreviewTextInput"/>
    <Button x:Name="SetTimeBtn" Content="Save and Close" HorizontalAlignment="Left" VerticalAlignment="Top" Click="SetTimeBtn_Click"></Button>
</StackPanel>

第 2 步:现在创建应用程序设置,您可以在此处存储文本框中的值。
转到项目\[应用程序名称]属性。

项目设置1

将您的设置命名为“TimerStart”,确保它是 int 类型并将其值设置为 10。

Project Settings 2

步骤 3:准备好设置后,就可以进行设置它的值,将此代码添加到您的 SetTimeBtn 单击事件中:

    private void SetTimeBtn_Click(object sender, RoutedEventArgs e)
    {
        Properties.Settings.Default.TimerStart = Int32.Parse(timerTxb.Text);
        this.Close();
    }

步骤 4:这就是设置页面的全部内容,现在这是我放入 MainWindow.cs 中的代码:

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
        timer.Interval = new TimeSpan(0, 0, 1);
        timer.Tick += Timer_Tick;
    }

    DispatcherTimer timer = new DispatcherTimer();
    private int start = new int();
    private int CheckPosition = 0;
    private bool ButtonStateCheck = false;

    private void StartStopBtn_Click(object sender, RoutedEventArgs e)
    {          
        if (ButtonStateCheck == false)
        {
            if (CheckPosition == 0)
            {
                start = Properties.Settings.Default.TimerStart;
            }
            else if (CheckPosition != 0)
            {
                start = CheckPosition;
            }
            timer.Start();
            ButtonStateCheck = true;
        }
        else if (ButtonStateCheck == true)
        {
            CheckPosition = start;
            ButtonStateCheck = false;
            timer.Stop();
        }
    }

    private void Timer_Tick(object sender, EventArgs e)
    {
        if (start > 0)
        {
            start--;
            StartStopBtn.Content = start;
        }
        else if (start <= 0)
        {
            timer.Stop();
            CheckPosition = 0;
            ButtonStateCheck= false;
        }
    }
}

Step 1: You could use application settings to store your value, first you need to add a condition to check that textbox input is only numeric, Add a PreviewTextInput event to your TextBox on Settings page.

C#:

    private void timerTxb_PreviewTextInput(object sender, TextCompositionEventArgs e)
    {
        System.Text.RegularExpressions.Regex InputChecker = new System.Text.RegularExpressions.Regex("[^0-9]+");
        e.Handled = InputChecker.IsMatch(e.Text);
    }

XAML:

<StackPanel Orientation="Horizontal" Background="LightGray">
    <Label Content="Set Timer" HorizontalAlignment="Left" VerticalAlignment="Top"></Label>
    <TextBox x:Name="timerTxb" Width="Auto" MinWidth="100" HorizontalAlignment="Left" VerticalAlignment="Top" PreviewTextInput="timerTxb_PreviewTextInput"/>
    <Button x:Name="SetTimeBtn" Content="Save and Close" HorizontalAlignment="Left" VerticalAlignment="Top" Click="SetTimeBtn_Click"></Button>
</StackPanel>

Step 2: Now create app setting, this is where you can store the value from the TextBox.
Go to Project\[App's name] Properties.

Project Settings 1

Name your setting "TimerStart", make sure it's an int type and set it's value to 10.

Project Settings 2

Step 3: After you have your setting ready it's time to set it's value, add this code to your SetTimeBtn click event:

    private void SetTimeBtn_Click(object sender, RoutedEventArgs e)
    {
        Properties.Settings.Default.TimerStart = Int32.Parse(timerTxb.Text);
        this.Close();
    }

Step 4: That's all for the settings page, now this is the code I put in MainWindow.cs:

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
        timer.Interval = new TimeSpan(0, 0, 1);
        timer.Tick += Timer_Tick;
    }

    DispatcherTimer timer = new DispatcherTimer();
    private int start = new int();
    private int CheckPosition = 0;
    private bool ButtonStateCheck = false;

    private void StartStopBtn_Click(object sender, RoutedEventArgs e)
    {          
        if (ButtonStateCheck == false)
        {
            if (CheckPosition == 0)
            {
                start = Properties.Settings.Default.TimerStart;
            }
            else if (CheckPosition != 0)
            {
                start = CheckPosition;
            }
            timer.Start();
            ButtonStateCheck = true;
        }
        else if (ButtonStateCheck == true)
        {
            CheckPosition = start;
            ButtonStateCheck = false;
            timer.Stop();
        }
    }

    private void Timer_Tick(object sender, EventArgs e)
    {
        if (start > 0)
        {
            start--;
            StartStopBtn.Content = start;
        }
        else if (start <= 0)
        {
            timer.Stop();
            CheckPosition = 0;
            ButtonStateCheck= false;
        }
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文