Creating a timer

本文介绍了一个使用WPF和C#实现的简单计时器应用程序。该计时器通过用户输入小时、分钟和秒数启动,并具备暂停与重置功能。文章提供了完整的XAML界面代码和后台逻辑实现细节。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >


Window1.xaml


 

<Window x:Class="timer.Window1"

    xmlns=http://schemas.microsoft.com/winfx/avalon/2005

    xmlns:x=http://schemas.microsoft.com/winfx/xaml/2005

    Title="timer" BorderThickness="1" Height="90" Width ="280"

    >


      <StackPanel>

            <Grid >

                  <Grid.ColumnDefinitions >

                        <ColumnDefinition Width="auto"/>

                        <ColumnDefinition Width="auto"/>

                        <ColumnDefinition Width="auto"/>

                        <ColumnDefinition Width="auto"/>

                        <ColumnDefinition Width="auto"/>

                        <ColumnDefinition Width="auto"/>

                        <ColumnDefinition Width="auto"/>

                  </Grid.ColumnDefinitions>


                  <Grid.RowDefinitions>

                        <RowDefinition/>

                        <RowDefinition/>

                  </Grid.RowDefinitions>


                  <TextBox Grid.Column="0" Grid.Row="0" Text="0" Name="hr"
Width="30" BorderThickness="0"/>

                  <Label Grid.Column="1" Grid.Row="0" Content=":"/>

                  <TextBox Grid.Column="2" Grid.Row="0" Text="0" Name="min"
Width="30" BorderThickness="0"/>

                  <Label Grid.Column="3" Grid.Row="0" Content=":"/>

                  <TextBox Grid.Column="4" Grid.Row="0" Text="0" Name="sec"
Width="30" BorderThickness="0"/>

                  <Button Grid.Column="5" Grid.Row="0" Content="Start" Click="OnClick" />

                  <Button Grid.Column="6" Grid.Row="0" Content="Reset" Click="OnClick1" />

                  <TextBox Grid.ColumnSpan="7" Grid.Row="1" Text="Enter Timer Value"
IsEnabled="False" Name="status" BorderThickness="1"/>


            </Grid>

      </StackPanel>

</Window>

Window1.xaml.cs


 

    public partial class Window1 : Window
    {
        private Clock clk; //This is a class which keeps track of the time being displayed
        private System.Windows.Threading.DispatcherTimer TimerClock;


        private void OnClick(object sender, RoutedEventArgs e)
        {
            if (ValidateValues(hr.Text, min.Text, sec.Text))
            {
                TimerClock = new System.Windows.Threading.DispatcherTimer();
//Creates a timerClock and enables it
                TimerClock.Interval = new TimeSpan(0, 0, 1);
                TimerClock.IsEnabled = true;
                clk = new Clock(hr.Text, min.Text, sec.Text);
                TimerClock.Tick += new EventHandler(TimerClock_Tick);
            }
        }


        private void OnClick1(object sender, RoutedEventArgs e)
        {
            TimerClock.IsEnabled = false;
            Clock.hr = Clock.min = Clock.sec = 0;
            hr.Text = Clock.hr.ToString();
            min.Text = Clock.min.ToString();
            sec.Text = Clock.sec.ToString();
        }


        void TimerClock_Tick(object sender, EventArgs e)
        {
            clk.Decrement();
            hr.Text = Clock.hr.ToString();
            min.Text = Clock.min.ToString();
            sec.Text = Clock.sec.ToString();
            if (Clock.completed)
            {
                TimerClock.IsEnabled = false;


                SoundPlayer player = new SoundPlayer(@"gurgle.wav");
//Plays the spoilsport of your sleep ;)
                player.Play();


            }
        }


        private bool ValidateValues(string hr1, string min1, string sec1)
        {
               //Validate values

        }


    }


    public partial class Clock
    {
        public static int hr, min, sec;
        public static bool completed;
        public Clock() { }


        public Clock(string hr1, string min1, string sec1)
        {
            completed = false;
            hr = int.Parse(hr1);
            min = int.Parse(min1);
            sec = int.Parse(sec1);
        }


        public void Decrement()
        {
               //Decrement the hours/mins/secs

        }


        public string TimeLeft()
        {
            return (hr.ToString() + ":" + min.ToString() + ":" + sec.ToString());
        }
    }


 

The new things in play that we see here are the SoundPlayer and the Timer which fit in seamlessly with the Avalon code.

So cut short your naps with a simple timer!!

转载于:https://www.cnblogs.com/myoungho/archive/2009/03/18/1415805.html

03-03 14:15:59.888 2805 3006 D BluetoothSystemServer: BluetoothManagerService: MESSAGE_BLUETOOTH_STATE_CHANGE: prevState=ON newState=TURNING_OFF 03-03 14:15:59.888 2805 3006 D BluetoothSystemServer: BluetoothManagerService: No support for AutoOn feature: Not creating a timer 03-03 14:15:59.888 2805 3006 D BluetoothSystemServer: BluetoothManagerService: broadcastIntentStateChange: action=BLE_STATE_CHANGED prevState=ON newState=TURNING_OFF 03-03 14:15:59.888 3589 5506 D BluetoothAdapter: isLeEnabled(): ON 03-03 14:15:59.888 2805 3006 D BluetoothSystemServer: BluetoothManagerService: broadcastIntentStateChange: action=STATE_CHANGED prevState=ON newState=TURNING_OFF 03-03 14:15:59.889 3295 3613 I bt_btif_dm: system/btif/src/btif_dm.cc:2652 btif_dm_cancel_discovery: Cancel search 03-03 14:15:59.889 3295 3613 I bt_bta_sd: system/bta/dm/bta_dm_device_search.cc:853 bta_dm_search_sm_execute: state:BTA_DM_SEARCH_IDLE, event:BTA_DM_API_SEARCH_CANCEL_EVT[0x1] 03-03 14:15:59.890 3295 3295 I A2dpService: stop() 03-03 14:15:59.890 3295 3295 I bluetooth-a2dp: system/btif/src/btif_av.cc:4079 cleanup_src: 03-03 14:15:59.891 3295 3613 I bluetooth-a2dp: system/btif/src/btif_av.cc:1336 Cleanup: 03-03 14:15:59.891 3295 3613 I bluetooth-a2dp: system/btif/src/btif_av.cc:4254 btif_av_source_execute_service: enable=false 03-03 14:15:59.891 3295 3613 I bt_btif_storage: system/btif/src/btif_storage.cc:638 btif_storage_get_adapter_property: Service_mask=0x62500040 03-03 14:15:59.891 3295 3613 I bluetooth-a2dp: system/btif/src/btif_av.cc:1539 CleanupAllPeers: 03-03 14:15:59.891 3295 3613 I bluetooth-a2dp: system/btif/src/btif_av.cc:530 SetActivePeer: peer=00:00:00:00:00:00 active_peer=00:00:00:00:00:00 03-03 14:15:59.891 3295 3613 I bluetooth-a2dp: system/btif/src/btif_a2dp_source.cc:529 btif_a2dp_source_cleanup: state=STATE_OFF 03-03 14:15:59.891 3295 3613 I bluetooth-a2dp: system/btif/src/btif_a2dp_source.cc:492 btif_a2dp_source_shutdown: state=STATE_OFF 03-03 14:15:59.891 1133 1133 D vendor.qti.bluetooth@1.0-ibs_handler: SerialClockVote: vote for UART CLK ON
03-08
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值