c#第一題

1 定時(shí)器

第一頁
<Window x:Class="_6._13.MainWindow"
http://schemas.microsoft.com/winfx/2006/xaml/presentation"
http://schemas.microsoft.com/winfx/2006/xaml"
        Title="定時(shí)器" Height="600" Width="825">
    <Grid>
        <Label Name="TimeLabel" Width=" 200" Height=" 80" Background="#0f0ffccc" Foreground="Black"  VerticalContentAlignment="Center" HorizontalContentAlignment="Center"  FontSize ="16 " Content=" 2021-6-13 15:34:00" />
        <Label Name="CountLabel" Width=" 200" Height=" 80" Background="#0f0ffccc" Foreground="Black" VerticalAlignment="Top"  VerticalContentAlignment="Center" HorizontalContentAlignment="Center"  FontSize ="16 " Content=" 1" />
        <Button Width=" 120" Height=" 60" HorizontalAlignment="Left" VerticalAlignment="Top"  Content="開始" Click="Start_Click"/>
        <Button Width=" 120" Height=" 60" HorizontalAlignment="Left"    Content="暫停" Click="Stop_Click" Margin="0,171,0,330" />
        <Button Width=" 120" Height=" 60" HorizontalAlignment="Left"   Content="計(jì)數(shù)" Click="Count_Click" Margin="0,329,0,172" />
        <Button Width=" 120" Height=" 60" HorizontalAlignment="Left"   Content="重置" Click="Reset_Click" Margin="0,501,0,0" />
        <TextBox  Name="RecordTextBox" Width=" 300" Height=" 400"  HorizontalAlignment="Right" />
    </Grid>
</Window>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Threading;

namespace _6._13
{
    /// <summary>
    /// MainWindow.xaml 的交互邏輯
    /// </summary>
    public partial class MainWindow : Window
    {
        DispatcherTimer _timer;
        int _cnt;
        public MainWindow()
        {
            InitializeComponent();
            TimeLabel.Content = DateTime.Now.ToString();
            _timer = new DispatcherTimer();
            _timer.Interval = new TimeSpan(0, 0, 0, 0, 50);
            _timer.Tick += new EventHandler(Timer_Tick);
            _cnt = 0;
        }
        private void Timer_Tick(object sender, EventArgs e)
        {
            _cnt++;
            TimeLabel.Content = DateTime.Now.ToString();
            CountLabel.Content = _cnt.ToString();
        }
        private void Start_Click(object sender, RoutedEventArgs e)
        {
            _timer.Start();
        }

        private void Stop_Click(object sender, RoutedEventArgs e)
        {
            _timer.Stop();
        }

        private void Count_Click(object sender, RoutedEventArgs e)
        {
            RecordTextBox.Text += string.Format("{0},{1}\n", _cnt, TimeLabel.Content);
        }

        private void Reset_Click(object sender, RoutedEventArgs e)
        {
            _timer.Stop();
            _cnt = 0;
            CountLabel.Content = _cnt.ToString();
            RecordTextBox.Text = string.Format("");

        }
    }
}
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

推薦閱讀更多精彩內(nèi)容