在線鬧鐘學習筆記
取當前年月日時分秒
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
可調整大小的工具窗口邊框
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.