C#自定义一个时间类

自定义一个时间类,该类包括时、分、秒字段与属性,具有将时间增加1秒、1分和1小时的方法,具有分别显示时、分、秒和同时显示时分秒的方法。
namespace WindowsFormsApplication10
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void button3_Click(object sender, EventArgs e)
        {
            time t = new time();
            t.second = int.Parse(textBox3.Text);
            t.minute = int.Parse(textBox2.Text);
            t.hour = int.Parse(textBox1.Text);
            if (textBox1.Text == "" || textBox2.Text == "" || textBox3.Text == "")
                return;
            if (t.second == 59)
            {
                textBox3.Text = "00";
                t.minute++;
                if(t.minute==60)
                {
                    textBox2.Text = "00";
                    if (t.hour == 23)
                        textBox1.Text = "0";
                    if(t.hour < 23&&t.hour>=0)
                        textBox1.Text= t.Hour(t.hour).ToString();
                    if (t.hour > 24 || t.hour < 0)
                        label1.Text = "→_→";
                  
                }
                if (t.minute < 59 && t.minute >= 0)
                    textBox2.Text = t.minute.ToString();
                if (t.minute > 60 || t.minute < 0)
                    label1.Text = "→_→";
               
            }
            if (t.second < 59 && t.second >= 0)
            {
                textBox3.Text = t.Second(t.second).ToString();
            }
            if (t.second > 60 || t.second < 0)
                label1.Text = "→_→";
           
        }

        private void button2_Click(object sender, EventArgs e)
        {
            time t = new time();
            t.hour = int.Parse(textBox1.Text);
            t.minute = int.Parse(textBox2.Text);
            if (textBox1.Text == "" || textBox2.Text == "")
                return;
            if (t.minute == 59)
            {
                textBox2.Text = "00";
                t.hour++;
                if (t.hour == 24)
                    textBox1.Text = "00";
                if (t.hour < 23 && t.hour >= 0)
                    textBox1.Text = t.hour.ToString();
                if (t.hour > 24 || t.hour < 0)
                    label1.Text = "→_→";
            }
            if (t.minute < 59&&t.minute>=0)
            {
                textBox2.Text = t.Minute(t.minute).ToString();
            }
            if (t.minute > 60 || t.minute < 0)
                label1.Text = "→_→";
            
        }

        private void button1_Click(object sender, EventArgs e)
        {
            time t = new time();
            t.hour = int.Parse(textBox1.Text);
            if (textBox1.Text == "")
                return;
            if (t.hour == 23)
                textBox1.Text = "00";
            if (t.hour < 23 && t.hour >= 0)
            {
                textBox1.Text = t.Hour(t.hour).ToString();
            }
            else
                label1.Text = "→_→";
        }

        private void button4_Click(object sender, EventArgs e)
        {
            label1.Text= textBox1.Text + "小时";
        }

        private void button5_Click(object sender, EventArgs e)
        {
            label1.Text = textBox2.Text + "分";
        }

        private void button6_Click(object sender, EventArgs e)
        {
            label1.Text = textBox3.Text + "秒";
        }

        private void button7_Click(object sender, EventArgs e)
        {
            label1.Text = textBox1.Text + "时" + textBox2.Text + "分" + textBox3.Text + "秒";
        }
    }
    class time
    {
        public int hour, minute, second;//声明字段
        public int hourr//声明属性
        {
            get { return hour; }
            set { hour = value; }
        }
        public int minutee
        {
            get { return minute; }
            set { minute = value; }
        }
        public int secondd
        {
            get { return second; }
            set { second = value; }
        }
        public int Hour(int hour)//声明方法
        {
            hour++;
            return hour;
        }
        public int Minute(int minute)
        {
            minute++;
            return minute;
        }
        public int Second(int second)
        {
            second++;
            return second;
        }
    }
}
 
 

  • 6
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值