您的位置:首页 > 编程语言 > C#

C#自定义一个时间类

2016-12-20 18:17 676 查看
自定义一个时间类,该类包括时、分、秒字段与属性,具有将时间增加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;
}
}
}



内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: