取当前年月日时分秒
currentTime=System.DateTime.Now;
取中文日期显示——年月日时分
string strY=currentTime.ToString("f"); //不显示秒
取中文日期显示_年月
string strYM=currentTime.ToString("y");
取中文日期显示_月日
string strMD=currentTime.ToString("m");
取中文年月日
string strYMD=currentTime.ToString("D");
取当前时分,格式为:14:24
string strT=currentTime.ToString("t");
取当前时间,格式为:2003-09-23 14:48
string strT=currentTime.ToString("g");
获得当前时间 n 天后的日期时间
DateTime newDay = DateTime.Now.AddDays(100);
显示当前时间
label2.Text = DateTime.Now.ToString("HH:mm:ss");
--------------------------------动态显示当前时间
设置interval为1000 enable=true
private void timer1_Tick(object sender, EventArgs e)
{
label2.Text = DateTime.Now.ToString("HH:mm:ss");
}
-------------------------------点击图片之间的切换
int count=0;
private void pictureBox3_Click(object sender, EventArgs e)
{
count++;
pictureBox3.Image = (count%2==1) ? imageList1.Images[1] :
imageList1.Images[0];
}
给出3个text,输入结束时间之后,倒计时
private void timer3_Tick(object sender, EventArgs e)
{
//得到当前时间的时间戳
int datetime = this.ConvertDateTimeInt(DateTime.Now);
//MessageBox.Show(datetime.ToString());
//得到输入的日期
String stime = DateTime.Now.ToShortDateString();
String endtime = stime + " " + hour + ":" + min + ":" + sec;
//MessageBox.Show(endtime);
//输入结束日期字符串格式化为时间
DateTime end_time = Convert.ToDateTime(endtime);
//MessageBox.Show("结束日期"+end_time.ToString());
//得到结束日期的时间戳
int endtimes = this.ConvertDateTimeInt(end_time);
//MessageBox.Show(endtimes.ToString());
//得到时间戳差值 即为之间的秒的差值
int shortimes = endtimes - datetime;
//MessageBox.Show(shortimes.ToString());
//把时间戳转化为真实时间
int minutes, hours, seconds;
hours = 0;
minutes = shortimes / 60;
if (minutes >= 60)
{
hours = minutes / 60;
minutes = minutes % 60;
}
seconds = shortimes % 60;
//显示剩余的时间
label2.Text=hours + ":" + minutes + ":" + seconds;
ssec = seconds;
smin = minutes;
shour = hours;
//开始倒计时
if (ssec == 0)
{
if (smin == 0)
{
if (shour == 0)
{
}
else
{
shour--;
smin = 59;
ssec = 59;
}
}
else
{
smin--;
ssec = 59;
}
}
else
{
ssec--;
}
if (shour == 0 && smin == 0)
{
if (ssec <= 10 && ssec > 0)//10秒的倒计时
{
label2.ForeColor = Color.Red;
axWindowsMediaPlayer1.URL = "倒计时.mp3";
axWindowsMediaPlayer1.Ctlcontrols.play();
}
if (ssec == 0)
{
axWindowsMediaPlayer1.URL = label3.Text;
axWindowsMediaPlayer1.Ctlcontrols.play();
timer3.Stop();
}
}
label2.Text = TimeToString(shour) + ":" + TimeToString(smin) + ":" +
TimeToString(ssec);
}
private string TimeToString(int n) //如果时间不足两位数则前面补0
{
if (n < 10)
return "0" + n.ToString();
else
return n.ToString();
}
/// <summary>
/// 时间戳转为C#格式时间
/// </summary>
/// <param name=”timeStamp”></param>
/// <returns></returns>
private DateTime GetTime(string timeStamp)
{
DateTime dtStart = TimeZone.CurrentTimeZone.ToLocalTime(new
DateTime(1970, 1, 1));
long lTime = long.Parse(timeStamp + "0000000");
TimeSpan toNow = new TimeSpan(lTime); return dtStart.Add(toNow);
}
/// <summary>
/// DateTime时间格式转换为Unix时间戳格式
/// </summary>
/// <param name=”time”></param>
/// <returns></returns>
private int ConvertDateTimeInt(System.DateTime time)
{
System.DateTime startTime = TimeZone.CurrentTimeZone.ToLocalTime(new
System.DateTime(1970, 1, 1));
return (int)(time - startTime).TotalSeconds;
}
-------------------------------------点击text出现下拉菜单 下拉日历 下拉时间部分
.TextBox控件 联想输入
属性更改:
AutoCompleteMode 值为:SuggestAppend
AutoCompleteSource 值为:CustomSource
后台数据绑定:
string[] tmp = new string[8] { "A", "AA", "AAA", "AB", "ABB", "ABCC", "AC","ACC"
}; this.textBox1.AutoCompleteCustomSource.Clear();
this.textBox1.AutoCompleteCustomSource.AddRange(tmp);
ComboBox控件 联想输入
属性更改:
AutoCompleteMode 值为:SuggestAppend
AutoCompleteSource 值为:ListItems
后台数据绑定:
this.combobox1.DataSource = list or dataTable or ...
---------------------------------------------------C# WINFORM 避免打开重复窗体
在触发事件的页面 添加全局变量
private Form tmpForm = null;
然后在打开窗体事件中判断此窗体对象是否存在或者是否被释放
如果不存在,且没释放,则创建新窗体
反之,就激活该窗体
if (tmpForm == null || tmpForm .IsDisposed)
{
tmpForm = new Form ();
tmpForm .Show();
}
else
{
tmpForm .Activate();
}
:
-----------------------------------------------解决最大化后铺满全屏的问题
在构造函数中加入以下代码:
this.MaximizedBounds = Screen.PrimaryScreen.WorkingArea;
用代码设置窗体最小化:this.WindowState=FormWindowState.Minimized;
用代码设置窗体最大化:this.WindowState=FormWindowState.Maximized;
formboderstyle 设置为fixedsingle 可以使窗体不调整大小
-------------------------------------FormBorderStyle的属性及意义
属 性
意 义
FormBorderStyle.None
无边框
FormBorderStyle.FixedSingle
固定的单行边框
FormBorderStyle.Fixed3D
固定的三维样式边框
FormBorderStyle.FixedDialog
固定的对话框样式的粗边框
FormBorderStyle.Sizable
可调整大小的边框
FormBorderStyle.FixedToolWindow
不可调整大小的工具窗口边框
FormBorderStyle.SizableToolWindow
可调整大小的工具窗口边框
currentTime=System.DateTime.Now;
取中文日期显示——年月日时分
string strY=currentTime.ToString("f"); //不显示秒
取中文日期显示_年月
string strYM=currentTime.ToString("y");
取中文日期显示_月日
string strMD=currentTime.ToString("m");
取中文年月日
string strYMD=currentTime.ToString("D");
取当前时分,格式为:14:24
string strT=currentTime.ToString("t");
取当前时间,格式为:2003-09-23 14:48
string strT=currentTime.ToString("g");
获得当前时间 n 天后的日期时间
DateTime newDay = DateTime.Now.AddDays(100);
显示当前时间
label2.Text = DateTime.Now.ToString("HH:mm:ss");
--------------------------------动态显示当前时间
设置interval为1000 enable=true
private void timer1_Tick(object sender, EventArgs e)
{
label2.Text = DateTime.Now.ToString("HH:mm:ss");
}
-------------------------------点击图片之间的切换
int count=0;
private void pictureBox3_Click(object sender, EventArgs e)
{
count++;
pictureBox3.Image = (count%2==1) ? imageList1.Images[1] :
imageList1.Images[0];
}
给出3个text,输入结束时间之后,倒计时
private void timer3_Tick(object sender, EventArgs e)
{
//得到当前时间的时间戳
int datetime = this.ConvertDateTimeInt(DateTime.Now);
//MessageBox.Show(datetime.ToString());
//得到输入的日期
String stime = DateTime.Now.ToShortDateString();
String endtime = stime + " " + hour + ":" + min + ":" + sec;
//MessageBox.Show(endtime);
//输入结束日期字符串格式化为时间
DateTime end_time = Convert.ToDateTime(endtime);
//MessageBox.Show("结束日期"+end_time.ToString());
//得到结束日期的时间戳
int endtimes = this.ConvertDateTimeInt(end_time);
//MessageBox.Show(endtimes.ToString());
//得到时间戳差值 即为之间的秒的差值
int shortimes = endtimes - datetime;
//MessageBox.Show(shortimes.ToString());
//把时间戳转化为真实时间
int minutes, hours, seconds;
hours = 0;
minutes = shortimes / 60;
if (minutes >= 60)
{
hours = minutes / 60;
minutes = minutes % 60;
}
seconds = shortimes % 60;
//显示剩余的时间
label2.Text=hours + ":" + minutes + ":" + seconds;
ssec = seconds;
smin = minutes;
shour = hours;
//开始倒计时
if (ssec == 0)
{
if (smin == 0)
{
if (shour == 0)
{
}
else
{
shour--;
smin = 59;
ssec = 59;
}
}
else
{
smin--;
ssec = 59;
}
}
else
{
ssec--;
}
if (shour == 0 && smin == 0)
{
if (ssec <= 10 && ssec > 0)//10秒的倒计时
{
label2.ForeColor = Color.Red;
axWindowsMediaPlayer1.URL = "倒计时.mp3";
axWindowsMediaPlayer1.Ctlcontrols.play();
}
if (ssec == 0)
{
axWindowsMediaPlayer1.URL = label3.Text;
axWindowsMediaPlayer1.Ctlcontrols.play();
timer3.Stop();
}
}
label2.Text = TimeToString(shour) + ":" + TimeToString(smin) + ":" +
TimeToString(ssec);
}
private string TimeToString(int n) //如果时间不足两位数则前面补0
{
if (n < 10)
return "0" + n.ToString();
else
return n.ToString();
}
/// <summary>
/// 时间戳转为C#格式时间
/// </summary>
/// <param name=”timeStamp”></param>
/// <returns></returns>
private DateTime GetTime(string timeStamp)
{
DateTime dtStart = TimeZone.CurrentTimeZone.ToLocalTime(new
DateTime(1970, 1, 1));
long lTime = long.Parse(timeStamp + "0000000");
TimeSpan toNow = new TimeSpan(lTime); return dtStart.Add(toNow);
}
/// <summary>
/// DateTime时间格式转换为Unix时间戳格式
/// </summary>
/// <param name=”time”></param>
/// <returns></returns>
private int ConvertDateTimeInt(System.DateTime time)
{
System.DateTime startTime = TimeZone.CurrentTimeZone.ToLocalTime(new
System.DateTime(1970, 1, 1));
return (int)(time - startTime).TotalSeconds;
}
-------------------------------------点击text出现下拉菜单 下拉日历 下拉时间部分
.TextBox控件 联想输入
属性更改:
AutoCompleteMode 值为:SuggestAppend
AutoCompleteSource 值为:CustomSource
后台数据绑定:
string[] tmp = new string[8] { "A", "AA", "AAA", "AB", "ABB", "ABCC", "AC","ACC"
}; this.textBox1.AutoCompleteCustomSource.Clear();
this.textBox1.AutoCompleteCustomSource.AddRange(tmp);
ComboBox控件 联想输入
属性更改:
AutoCompleteMode 值为:SuggestAppend
AutoCompleteSource 值为:ListItems
后台数据绑定:
this.combobox1.DataSource = list or dataTable or ...
---------------------------------------------------C# WINFORM 避免打开重复窗体
在触发事件的页面 添加全局变量
private Form tmpForm = null;
然后在打开窗体事件中判断此窗体对象是否存在或者是否被释放
如果不存在,且没释放,则创建新窗体
反之,就激活该窗体
if (tmpForm == null || tmpForm .IsDisposed)
{
tmpForm = new Form ();
tmpForm .Show();
}
else
{
tmpForm .Activate();
}
:
-----------------------------------------------解决最大化后铺满全屏的问题
在构造函数中加入以下代码:
this.MaximizedBounds = Screen.PrimaryScreen.WorkingArea;
用代码设置窗体最小化:this.WindowState=FormWindowState.Minimized;
用代码设置窗体最大化:this.WindowState=FormWindowState.Maximized;
formboderstyle 设置为fixedsingle 可以使窗体不调整大小
-------------------------------------FormBorderStyle的属性及意义
属 性
意 义
FormBorderStyle.None
无边框
FormBorderStyle.FixedSingle
固定的单行边框
FormBorderStyle.Fixed3D
固定的三维样式边框
FormBorderStyle.FixedDialog
固定的对话框样式的粗边框
FormBorderStyle.Sizable
可调整大小的边框
FormBorderStyle.FixedToolWindow
不可调整大小的工具窗口边框
FormBorderStyle.SizableToolWindow
可调整大小的工具窗口边框