如何在 WPF 中设置计时器开始时间文本框值?
我正在创建计时器。我有两个窗口主窗口和设置。 主窗口有两个按钮。 StartStopBtn 和 ShowSettingsWindow。 设置有一个SetTimeBtn和timerTxb。
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();
}
}
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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
步骤 1:您可以使用应用程序设置来存储您的值,首先您需要添加一个条件来检查文本框输入是否仅为数字,在“设置”页面上向您的文本框添加一个 PreviewTextInput 事件。
C#:
XAML:
第 2 步:现在创建应用程序设置,您可以在此处存储文本框中的值。
转到项目\[应用程序名称]属性。
将您的设置命名为“TimerStart”,确保它是 int 类型并将其值设置为 10。
步骤 3:准备好设置后,就可以进行设置它的值,将此代码添加到您的 SetTimeBtn 单击事件中:
步骤 4:这就是设置页面的全部内容,现在这是我放入 MainWindow.cs 中的代码:
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#:
XAML:
Step 2: Now create app setting, this is where you can store the value from the TextBox.
Go to Project\[App's name] Properties.
Name your setting "TimerStart", make sure it's an int type and set it's value to 10.
Step 3: After you have your setting ready it's time to set it's value, add this code to your SetTimeBtn click event:
Step 4: That's all for the settings page, now this is the code I put in MainWindow.cs: