C# 设计一个倒计时程序,需要在文本框中输入倒计时分钟数。用Windows窗体设计

C# 设计一个倒计时程序,需要在文本框中输入倒计时分钟数。用Windows窗体设计,第1张

namespace CountDown

{

    public partial class Form1 : Form

    {

        public Form1()

        {

            InitializeComponent();

        }

int timeData = 0;//时间数据变量,以秒为单位

        bool btFlag = true;//计时器标签

        private void button1_Click(object sender, EventArgs e)

        {

            if (btFlag)//如果是启动计时器标签为true

            {

                    if (timeData>= 0)

                    {

                        ShowTimeLabel(ref timeData);//显示具体是时间数

                        timer1Start();//启动计时器

                        button1Text = "停止计时";

                        btFlag = false;

                    }

            }

            else

            {

                timer1Stop();//关闭计时器

                button1Text = "启动计时器";

                btFlag = true;

            }

        }

        /// <summary>

        /// 把时间显示为时分秒的格式

        /// </summary>

        /// <param name="timeData"></param>

        private void ShowTimeLabel(ref int timeData)

        {

                 string hourStr = (timeData / 3600)ToString();

                 string minuteStr ="0";

                 string secondStr = "0";

                 //label2Text = hourStr + " 小时 " + (timeData % 60)ToString()+" 分钟";

                 if (timeData % 3600 >= 60)

                 {

                     minuteStr = ((timeData % 3600) / 60)ToString();

                     secondStr = ((timeData % 3600) % 60)ToString();

                 }

                 else

                 {

                     secondStr = ((timeData % 3600) % 60)ToString();

                 }

                 label2Text = hourStr + "小时" + minuteStr + "分钟" + secondStr + "秒";

        }

private void Form1_Load(object sender, EventArgs e)

        {

            timer1Interval = 1000;

            timer1Tick += new EventHandler(timer1_Tick);//添加计时器的tick事件

            button1Text = "启动计时器";

            btFlag = true;

            textBox1TextChanged += new EventHandler(textBox1_TextChanged);//添加事件

        }

void textBox1_TextChanged(object sender, EventArgs e)

        {

            timer1Stop();

            label2Text = stringEmpty;

            try

            {

                if(textBox1Text!=stringEmpty)

                timeData = ConvertToInt32(textBox1Text) 60;

            }

            catch

            {

                MessageBoxShow("请输入合法数据","温馨提示");

                textBox1Text = stringEmpty;

            }

            button1Text = "启动计时器";

            btFlag = true;

        }

void timer1_Tick(object sender, EventArgs e)

        {

            timeData--;//时间数减一秒

            ShowTimeLabel(ref timeData);

            if (timeData == 0)

            {

                timer1Stop();

            }

        }

    }

}

在VB中新建个EXE工程,在窗体上添加按钮控件,标签控件和Timer控件。

按的代码结构编写,当然需要完善。

先用以下代码例子演示一个数字时钟,体验下Timer控件的用法:

Private Sub Form_Load ()

   Timer1Interval = 1000   ' 设置计时器时间间隔。

End Sub

Private Sub Timer1_Timer ()

   Label1Caption = Time   ' 更新时间显示。

End Sub

不用这么麻烦,只要用两个控件就可以一个是text或caption,另一个就是timer

将timer的时间间隔改成1000ms,在timer1中加入2句代码:

Private

Sub

Timer1_Timer()

text1forntsize=50

text1text=time

End

Sub

完成

解释一下"text1forntsize=50"这句的意思是text1控件的字号为50你也可以自己改"text1text=time"这句的意思是每隔一秒将系统的时间显示在text中

回答会自动过滤缩进,需要按照上图调整语法缩进,下方是代码

import time

# 设置倒计时时间(单位:秒)

countdown_time = 20

# 开始循环

while countdown_time > 0:

# 打印当前倒计时时间

print(countdown_time)

# 等待 1 秒

timesleep(1)

# 倒计时时间减 1

countdown_time -= 1

# 倒计时结束

print("倒计时结束!")

using System;

using SystemCollections;

using SystemTimers;

using SystemRuntimeInteropServices;

using SystemCollectionsGeneric;

using SystemComponentModel;

using SystemData;

using SystemDrawing;

using SystemLinq;

using SystemText;

using SystemWindowsForms;

namespace 计时器

{

public partial class Form1 : Form

{

public Form1()

{

InitializeComponent();

}

[DllImport("kernel32",EntryPoint="Beep")]

public extern static int Beep(int dwfreq,int dwduration);

private void numericUpDown3_ValueChanged(object sender, EventArgs e)

{

string hour, minute, second;

if (numericUpDown3Value == 60)

{

numericUpDown3Value = 0;

numericUpDown2Value = ConvertToInt32(numericUpDown2Value) + 1;

int our = ConvertToInt32(numericUpDown1Value);

int niuit = ConvertToInt32(numericUpDown2Value);

int secon = ConvertToInt32(numericUpDown3Value);

if (our < 10)

{

hour = "0" + ourToString();

}

else

{

hour = ourToString();

}

if (niuit < 10)

{

minute = "0" + niuitToString() + ":";

}

else

{

minute = niuitToString() + ":";

}

if (secon < 10)

{

second = "0" + seconToString() + ":";

}

else

{

second = seconToString() + ":";

}

}

else

{

int our = ConvertToInt32(numericUpDown1Value);

int niuit = ConvertToInt32(numericUpDown2Value);

int secon = ConvertToInt32(numericUpDown3Value);

if (our < 10)

{

hour = "0" + ourToString()+ ":" ;

}

else

{

hour = ourToString()+ ":" ;

}

if (niuit < 10)

{

minute = "0" + niuitToString() + ":";

}

else

{

minute = niuitToString() + ":";

}

if (secon < 10)

{

second = "0" + seconToString();

}

else

{

second = seconToString();

}

}

label2Text = hour + minute + second;

}

private void numericUpDown2_ValueChanged(object sender, EventArgs e)

{

string hour, minute, second;

if (numericUpDown2Value == 60)

{

numericUpDown2Value = 0;

numericUpDown1Value = ConvertToInt32(numericUpDown1Value) + 1;

int our = ConvertToInt32(numericUpDown1Value);

int niuit = ConvertToInt32(numericUpDown2Value);

int secon = ConvertToInt32(numericUpDown3Value);

if (our < 10)

{

hour = "0" + ourToString() + ":" ;

}

else

{

hour = ourToString() + ":" ;

}

if (niuit < 10)

{

minute = "0" + niuitToString() + ":";

}

else

{

minute = niuitToString() + ":";

}

if (secon < 10)

{

second = "0" + seconToString();

}

else

{

second = seconToString();

}

}

else

{

int our = ConvertToInt32(numericUpDown1Value);

int niuit = ConvertToInt32(numericUpDown2Value);

int secon = ConvertToInt32(numericUpDown3Value);

if (our < 10)

{

hour = "0" + ourToString() + ":";

}

else

{

hour = ourToString() + ":";

}

if (niuit < 10)

{

minute = "0" + niuitToString() + ":";

}

else

{

minute = niuitToString() + ":";

}

if (secon < 10)

{

second = "0" + seconToString() ;

}

else

{

second = seconToString();

}

}

label2Text = hour + minute + second;

}

private void numericUpDown1_ValueChanged(object sender, EventArgs e)

{

string hour, minute, second;

if (numericUpDown1Value == 24)

{

numericUpDown1Value = 0;

int our = ConvertToInt32(numericUpDown1Value);

int niuit = ConvertToInt32(numericUpDown2Value);

int secon = ConvertToInt32(numericUpDown3Value);

if (our < 10)

{

hour = "0" + ourToString();

}

else

{

hour = ourToString() ;

}

if (niuit < 10)

{

minute = "0" + niuitToString() + ":";

}

else

{

minute = niuitToString() + ":";

}

if (secon < 10)

{

second = "0" + seconToString() + ":";

}

else

{

second = seconToString() + ":";

}

}

else

{

int our = ConvertToInt32(numericUpDown1Value);

int niuit = ConvertToInt32(numericUpDown2Value);

int secon = ConvertToInt32(numericUpDown3Value);

if (our < 10)

{

hour = "0" + ourToString() + ":";

}

else

{

hour = ourToString() + ":";

}

if (niuit < 10)

{

minute = "0" + niuitToString() + ":";

}

else

{

minute = niuitToString() + ":";

}

if (secon < 10)

{

second = "0" + seconToString();

}

else

{

second = seconToString();

}

}

label2Text = hour + minute +second ;

}

private void button1_Click(object sender, EventArgs e)

{

timer1Interval = 1000;

timer1Enabled = true;

}

private void timer1_Tick(object sender, EventArgs e)

{

label1Text = DateTimeNowToString();

int hours = DateTimeNowHour;

int munit = DateTimeNowMinute;

int second = DateTimeNowSecond;

long dat;

if ((int)numericUpDown1Value > hours)

{

dat = (long)((int)numericUpDown1Value - hours) 3600 + ((int)numericUpDown2Value - munit) 60 + ((int)numericUpDown3Value - second);

}

else

{

if ((int)numericUpDown2Value > munit)

{

dat = (long)((int)numericUpDown1Value - hours) 3600 + ((int)numericUpDown2Value - munit) 60 + ((int)numericUpDown3Value - second);

}

else

{

if ((int)numericUpDown3Value >= second)

{

dat = (long)((int)numericUpDown1Value - hours) 3600 + ((int)numericUpDown2Value - munit) 60 + ((int)numericUpDown3Value - second);

}

else

{

dat = (long)((int)numericUpDown1Value + 24 - hours) 3600 + ((int)numericUpDown2Value - munit) 60 + ((int)numericUpDown3Value - second);

}

}

}

if (dat > 0)

{

label3Text = "闹钟已经启动";

label4Text = "剩余" + datToString() + "秒";

}

if (dat == 0)

{

timer1Enabled = false;

Beep(200, 500);

label4Text = "时间已到";

}

}

private void button2_Click(object sender, EventArgs e)

{

timer1Enabled = false;

label3Text = "闹钟已经停止";

}

}

}

输入1Hz的时钟作为秒信号,秒计数满60后向分计数进1,分计数满60后向时计数进1。当计数到24:60:60自动回到00:00:00;

library ieee;

use ieeestd_logic_1164all;

entity clock is

port(clk:in std_logic;--输入1Hz的时钟作为秒信号

clr:in std_logic;--异步清零信号

s:out integer range 0 to 60;--秒

min:out integer range 0 to 60;--分

h:out integer range 0 to 24--时

);

end clock;

architecture clock of clock is

begin

process(clk,clr)

variable count1 :integer range 0 to 60;--秒计数

variable count2 :integer range 0 to 60;--分计数

variable count3 :integer range 0 to 24;--时计数

begin

s<=count1;

min<=count2;

h<=count3;

if(clr='1')then

count1:=0;

count2:=0;

count3:=0;

elsif(clk'event and clk='1')then

count1:=count1+1;

if (count1=60)then

count1:=0;

count2:=count2+1;

if(count2=60)then

count2:=0;

count3:=count3+1;

if(count3=24)then

count3:=0;

end if;

end if;

end if;

end if;

end process;

end clock;

以上就是关于C# 设计一个倒计时程序,需要在文本框中输入倒计时分钟数。用Windows窗体设计全部的内容,包括:C# 设计一个倒计时程序,需要在文本框中输入倒计时分钟数。用Windows窗体设计、求问,如图用VB设计倒计时程序怎么做啊在线等挺急的、VB怎样编一个正计时程序等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zz/9818882.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-02
下一篇 2023-05-02

发表评论

登录后才能评论

评论列表(0条)

保存