// Cell 顏色
private void DataGridView_Item_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)
{
if (e.RowIndex > -1 && e.ColumnIndex > -1)
{
if (DataGridView_Item.Rows[e.RowIndex].Cells["狀態"].Value.ToString() == "逾期")
DataGridView_Item.Rows[e.RowIndex].DefaultCellStyle.BackColor = System.Drawing.Color.Orange;//改送審顏色
else if (DataGridView_Item.Rows[e.RowIndex].Cells["狀態"].Value.ToString() == "完成")
DataGridView_Item.Rows[e.RowIndex].DefaultCellStyle.BackColor = System.Drawing.Color.LimeGreen;//改送審顏色
else if (DataGridView_Item.Rows[e.RowIndex].Cells["狀態"].Value.ToString() == "執行中")
DataGridView_Item.Rows[e.RowIndex].DefaultCellStyle.BackColor = System.Drawing.Color.SkyBlue;//改送審顏色
}
}
ASP.Net C# By Laurence
2014年4月1日 星期二
2014年3月20日 星期四
[C#]假日判斷(可指定日期為假日)
public static bool IsHolidays(DateTime date)
{
// 週休二日
if (date.DayOfWeek == DayOfWeek.Saturday || date.DayOfWeek == DayOfWeek.Sunday)
{
return true;
}
// 國定假日(國曆)
if (date.ToString("MM/dd").Equals("01/01"))
{
return true;
}
if (date.ToString("MM/dd").Equals("02/28"))
{
return true;
}
if (date.ToString("MM/dd").Equals("04/04"))
{
return true;
}
if (date.ToString("MM/dd").Equals("04/05"))
{
return true;
}
if (date.ToString("MM/dd").Equals("05/01"))
{
return true;
}
if (date.ToString("MM/dd").Equals("10/10"))
{
return true;
}
// 國定假日(農曆)
if (GetLeapDate(date, false).Equals("12/" + GetDaysInLeapMonth(date)))
{
return true;
}
if (GetLeapDate(date, false).Equals("1/1"))
{
return true;
}
if (GetLeapDate(date, false).Equals("1/2"))
{
return true;
}
if (GetLeapDate(date, false).Equals("1/3"))
{
return true;
}
if (GetLeapDate(date, false).Equals("1/4"))
{
return true;
}
if (GetLeapDate(date, false).Equals("1/5"))
{
return true;
}
if (GetLeapDate(date, false).Equals("5/5"))
{
return true;
}
if (GetLeapDate(date, false).Equals("8/15"))
{
return true;
}
// 公司假日
//日期是否在特定節日,資料庫有資料則為假日
if (CheckHoliday(date))
{
return true;
}
//日期是否在公司設定節日,資料庫有資料則為假日
if (CheckDeductionSh(date))
{
return true;
}
return false;
}
{
// 週休二日
if (date.DayOfWeek == DayOfWeek.Saturday || date.DayOfWeek == DayOfWeek.Sunday)
{
return true;
}
// 國定假日(國曆)
if (date.ToString("MM/dd").Equals("01/01"))
{
return true;
}
if (date.ToString("MM/dd").Equals("02/28"))
{
return true;
}
if (date.ToString("MM/dd").Equals("04/04"))
{
return true;
}
if (date.ToString("MM/dd").Equals("04/05"))
{
return true;
}
if (date.ToString("MM/dd").Equals("05/01"))
{
return true;
}
if (date.ToString("MM/dd").Equals("10/10"))
{
return true;
}
// 國定假日(農曆)
if (GetLeapDate(date, false).Equals("12/" + GetDaysInLeapMonth(date)))
{
return true;
}
if (GetLeapDate(date, false).Equals("1/1"))
{
return true;
}
if (GetLeapDate(date, false).Equals("1/2"))
{
return true;
}
if (GetLeapDate(date, false).Equals("1/3"))
{
return true;
}
if (GetLeapDate(date, false).Equals("1/4"))
{
return true;
}
if (GetLeapDate(date, false).Equals("1/5"))
{
return true;
}
if (GetLeapDate(date, false).Equals("5/5"))
{
return true;
}
if (GetLeapDate(date, false).Equals("8/15"))
{
return true;
}
// 公司假日
//日期是否在特定節日,資料庫有資料則為假日
if (CheckHoliday(date))
{
return true;
}
//日期是否在公司設定節日,資料庫有資料則為假日
if (CheckDeductionSh(date))
{
return true;
}
return false;
}
[C#]國曆轉農曆
///<summary>
/// 取得農曆日期
///</summary>
public static string GetLeapDate(DateTime Date, bool bShowYear = true, bool bShowMonth = true)
{
int L_ly, nLeapYear, nLeapMonth;
ChineseLunisolarCalendar MyChineseLunisolarCalendar = new ChineseLunisolarCalendar();
nLeapYear = MyChineseLunisolarCalendar.GetYear(Date);
nLeapMonth = MyChineseLunisolarCalendar.GetMonth(Date);
if (MyChineseLunisolarCalendar.IsLeapYear(nLeapYear)) //判斷此農曆年是否為閏年
{
L_ly = MyChineseLunisolarCalendar.GetLeapMonth(nLeapYear); //抓出此閏年閏何月
if (nLeapMonth >= L_ly)
{
nLeapMonth--;
}
}
else
{
nLeapMonth = MyChineseLunisolarCalendar.GetMonth(Date);
}
if (bShowYear)
{
return "" + MyChineseLunisolarCalendar.GetYear(Date) + "/" + nLeapMonth + "/" + MyChineseLunisolarCalendar.GetDayOfMonth(Date);
}
else if (bShowMonth)
{
return "" + nLeapMonth + "/" + MyChineseLunisolarCalendar.GetDayOfMonth(Date);
}
else
{
return "" + MyChineseLunisolarCalendar.GetDayOfMonth(Date);
}
}
///<summary>
/// 取得某一日期的該農曆月份的總天數
///</summary>
public static string GetDaysInLeapMonth(DateTime date)
{
ChineseLunisolarCalendar MyChineseLunisolarCalendar = new ChineseLunisolarCalendar();
return "" + MyChineseLunisolarCalendar.GetDaysInMonth(MyChineseLunisolarCalendar.GetYear(date), date.Month);
}
/// 取得農曆日期
///</summary>
public static string GetLeapDate(DateTime Date, bool bShowYear = true, bool bShowMonth = true)
{
int L_ly, nLeapYear, nLeapMonth;
ChineseLunisolarCalendar MyChineseLunisolarCalendar = new ChineseLunisolarCalendar();
nLeapYear = MyChineseLunisolarCalendar.GetYear(Date);
nLeapMonth = MyChineseLunisolarCalendar.GetMonth(Date);
if (MyChineseLunisolarCalendar.IsLeapYear(nLeapYear)) //判斷此農曆年是否為閏年
{
L_ly = MyChineseLunisolarCalendar.GetLeapMonth(nLeapYear); //抓出此閏年閏何月
if (nLeapMonth >= L_ly)
{
nLeapMonth--;
}
}
else
{
nLeapMonth = MyChineseLunisolarCalendar.GetMonth(Date);
}
if (bShowYear)
{
return "" + MyChineseLunisolarCalendar.GetYear(Date) + "/" + nLeapMonth + "/" + MyChineseLunisolarCalendar.GetDayOfMonth(Date);
}
else if (bShowMonth)
{
return "" + nLeapMonth + "/" + MyChineseLunisolarCalendar.GetDayOfMonth(Date);
}
else
{
return "" + MyChineseLunisolarCalendar.GetDayOfMonth(Date);
}
}
///<summary>
/// 取得某一日期的該農曆月份的總天數
///</summary>
public static string GetDaysInLeapMonth(DateTime date)
{
ChineseLunisolarCalendar MyChineseLunisolarCalendar = new ChineseLunisolarCalendar();
return "" + MyChineseLunisolarCalendar.GetDaysInMonth(MyChineseLunisolarCalendar.GetYear(date), date.Month);
}
2013年12月8日 星期日
[C#]System.Net.Mail.SmtpClient寄信(無需帳密)
用樣寄是很好啦不需要登入帳密
但垃圾信太多小心被封了哈!!
1.web.config要加下面這段
2.收件人的格式為
3.使用時要new出來喔
4.Function
但垃圾信太多小心被封了哈!!
1.web.config要加下面這段
<system.net>
<mailSettings>
<smtp deliveryMethod="Network">
<network defaultCredentials="false" host="msa.hinet.net" port="25"/>
</smtp>
</mailSettings>
</system.net>
2.收件人的格式為
<abc@gmail.com> ,<def@gmail.com> ,
3.使用時要new出來喔
PublicClass Class = new PublicClass();//假如這function放在PublicClass裡時
Class.SendByMail(收件人,標題,內文, 寄件人會顯示的標題, 寄件的Email);
4.Function
/// <summary>
/// 用Client寄信 需New出來
/// </summary>
/// <param name="pMail">收件者可以,分格寄多人</param>
/// <param name="pSubject">信件標題</param>
/// <param name="pBody">信件內容</param>
/// <param name="pFromMailName">寄件人會顯示的標題</param>
/// <param name="pFromMail">寄件的Email</param>
public void SendByMail(string pMail, string pSubject, string pBody, string pFromMailName, string pFromMail)
{
System.Net.Mail.MailMessage Mail = new System.Net.Mail.MailMessage();
Mail.From = new System.Net.Mail.MailAddress(pFromMail, pFromMailName);
Mail.Subject = pSubject;
Mail.IsBodyHtml = true;
Mail.BodyEncoding = System.Text.Encoding.UTF8;
Mail.SubjectEncoding = System.Text.Encoding.UTF8;
Mail.Body = pBody;
//這邊多人可以回圈
char[] delimiterChars = { ',' };
string[] words = pMail.Split(delimiterChars);
foreach (string s in words)
{
if (s.Trim() != "")
{
Mail.To.Add(s);
}
}
System.Net.Mail.SmtpClient SMTPServer = new System.Net.Mail.SmtpClient(System.Web.Configuration.WebConfigurationManager.AppSettings["SmtpHost"]);
SMTPServer.Send(Mail);
System.Threading.Thread.Sleep(5);
GC.Collect();
GC.WaitForPendingFinalizers();
}
[C#]圖片轉二進位
using System.IO;
using System.Drawing;
/// <summary>
/// 將 Image 轉換為 Byte 陣列。
/// </summary>
/// <param name="ImgPath">圖片路徑 。</param>
public static byte[] ImageToBuffer(string ImgPath)
{
byte[] _ImageBytes;
if (File.Exists(ImgPath))
{
Image _Image = Image.FromFile(ImgPath);
MemoryStream ms = new MemoryStream();
_Image.Save(ms, System.Drawing.Imaging.ImageFormat.Bmp);
_ImageBytes = ms.GetBuffer();
ms.Dispose();
_Image.Dispose();
}
else
{
_ImageBytes = null;
}
return _ImageBytes;
}
using System.Drawing;
/// <summary>
/// 將 Image 轉換為 Byte 陣列。
/// </summary>
/// <param name="ImgPath">圖片路徑 。</param>
public static byte[] ImageToBuffer(string ImgPath)
{
byte[] _ImageBytes;
if (File.Exists(ImgPath))
{
Image _Image = Image.FromFile(ImgPath);
MemoryStream ms = new MemoryStream();
_Image.Save(ms, System.Drawing.Imaging.ImageFormat.Bmp);
_ImageBytes = ms.GetBuffer();
ms.Dispose();
_Image.Dispose();
}
else
{
_ImageBytes = null;
}
return _ImageBytes;
}
2013年11月28日 星期四
[C#][TCP] TCP 傳接收Byte資料
string TcpConnect(string message, string hostNameOrAddress, string port)
{
Cursor.Current = Cursors.WaitCursor;
Cursor.Show();
bool IsNumber = true;
foreach (char c in port)
{
IsNumber = Char.IsNumber(c);
if (!IsNumber)
break;
}
if (!IsNumber)
{
Cursor.Current = Cursors.Default;
Cursor.Hide();
return "S";
}
//throw new ArgumentException("Port must be \"Number\"!!");
Encoding encode = Encoding.ASCII;
TcpClient client = new TcpClient(AddressFamily.InterNetwork);
client.Client.Poll(2000, SelectMode.SelectError);
client.Client.Poll(2000, SelectMode.SelectRead);
client.Client.Poll(2000, SelectMode.SelectWrite);
NetworkStream stream = null;
ArrayList buffer = null;
Char splitchar = '^';
try
{
IPEndPoint point = new IPEndPoint(Dns.GetHostEntry(hostNameOrAddress).AddressList[0], int.Parse(port));
client.Connect(point);
stream = client.GetStream();
string sentdata = String.Format("{1}{0}{2}", splitchar.ToString(), message.Length, message);
buffer = new ArrayList(encode.GetBytes(sentdata));
stream.WriteByte(1);
stream.Write((byte[])buffer.ToArray(typeof(System.Byte)), 0, buffer.Count);
stream.WriteByte(2);
int iTimeOut = 2000;
while (client.Client.Available == 0 && !stream.DataAvailable)
{
if (iTimeOut == 0)
break;
Thread.Sleep(1);
iTimeOut--;
}
buffer.Clear();
int i = stream.ReadByte();
while (i > -1)
{
if (i == 1)
buffer.Clear();
else if (i == 2)
break;
else
buffer.Add((byte)i);
i = stream.ReadByte();
}
}
catch (SocketException)
{
if (errCount == 3)
{
//SharedMethod.NextAddr = !SharedMethod.NextAddr;
errCount = 1;
}
else
{
errCount++;
}
Cursor.Current = Cursors.Default;
Cursor.Hide();
return "S";
}
catch (Exception)
{
Cursor.Current = Cursors.Default;
Cursor.Hide();
return "G";
}
finally
{
if (stream != null)
stream.Close();
if (client != null)
client.Close();
}
if (buffer != null && buffer.Count > 0)
{
byte[] rbyte = (byte[])buffer.ToArray(typeof(System.Byte));
string result = encode.GetString(rbyte, 0, rbyte.Length);
if (String.IsNullOrEmpty(result) || result.IndexOf(splitchar) < 0)
{
Cursor.Current = Cursors.Default;
Cursor.Hide();
return "R";
//throw new InvalidOperationException("Receive data struct incurrect!!...");
}
else
{
string slength = result.Substring(0, result.IndexOf(splitchar));
int datalength = 0;
try
{
datalength = int.Parse(slength);
}
catch
{
Cursor.Current = Cursors.Default;
Cursor.Hide();
return "R";
//throw new InvalidOperationException("LEN must be \"Number\"!!");
}
if (datalength == result.Length - slength.Length - 1)
{
Cursor.Current = Cursors.Default;
Cursor.Hide();
return result.Substring(slength.Length + 1);
}
else
{
Cursor.Current = Cursors.Default;
Cursor.Hide();
return "R";
}
//throw new InvalidOperationException("Data lost!!");
}
}
else
{
Cursor.Current = Cursors.Default;
Cursor.Hide();
return "G";
}
}
{
Cursor.Current = Cursors.WaitCursor;
Cursor.Show();
bool IsNumber = true;
foreach (char c in port)
{
IsNumber = Char.IsNumber(c);
if (!IsNumber)
break;
}
if (!IsNumber)
{
Cursor.Current = Cursors.Default;
Cursor.Hide();
return "S";
}
//throw new ArgumentException("Port must be \"Number\"!!");
Encoding encode = Encoding.ASCII;
TcpClient client = new TcpClient(AddressFamily.InterNetwork);
client.Client.Poll(2000, SelectMode.SelectError);
client.Client.Poll(2000, SelectMode.SelectRead);
client.Client.Poll(2000, SelectMode.SelectWrite);
NetworkStream stream = null;
ArrayList buffer = null;
Char splitchar = '^';
try
{
IPEndPoint point = new IPEndPoint(Dns.GetHostEntry(hostNameOrAddress).AddressList[0], int.Parse(port));
client.Connect(point);
stream = client.GetStream();
string sentdata = String.Format("{1}{0}{2}", splitchar.ToString(), message.Length, message);
buffer = new ArrayList(encode.GetBytes(sentdata));
stream.WriteByte(1);
stream.Write((byte[])buffer.ToArray(typeof(System.Byte)), 0, buffer.Count);
stream.WriteByte(2);
int iTimeOut = 2000;
while (client.Client.Available == 0 && !stream.DataAvailable)
{
if (iTimeOut == 0)
break;
Thread.Sleep(1);
iTimeOut--;
}
buffer.Clear();
int i = stream.ReadByte();
while (i > -1)
{
if (i == 1)
buffer.Clear();
else if (i == 2)
break;
else
buffer.Add((byte)i);
i = stream.ReadByte();
}
}
catch (SocketException)
{
if (errCount == 3)
{
//SharedMethod.NextAddr = !SharedMethod.NextAddr;
errCount = 1;
}
else
{
errCount++;
}
Cursor.Current = Cursors.Default;
Cursor.Hide();
return "S";
}
catch (Exception)
{
Cursor.Current = Cursors.Default;
Cursor.Hide();
return "G";
}
finally
{
if (stream != null)
stream.Close();
if (client != null)
client.Close();
}
if (buffer != null && buffer.Count > 0)
{
byte[] rbyte = (byte[])buffer.ToArray(typeof(System.Byte));
string result = encode.GetString(rbyte, 0, rbyte.Length);
if (String.IsNullOrEmpty(result) || result.IndexOf(splitchar) < 0)
{
Cursor.Current = Cursors.Default;
Cursor.Hide();
return "R";
//throw new InvalidOperationException("Receive data struct incurrect!!...");
}
else
{
string slength = result.Substring(0, result.IndexOf(splitchar));
int datalength = 0;
try
{
datalength = int.Parse(slength);
}
catch
{
Cursor.Current = Cursors.Default;
Cursor.Hide();
return "R";
//throw new InvalidOperationException("LEN must be \"Number\"!!");
}
if (datalength == result.Length - slength.Length - 1)
{
Cursor.Current = Cursors.Default;
Cursor.Hide();
return result.Substring(slength.Length + 1);
}
else
{
Cursor.Current = Cursors.Default;
Cursor.Hide();
return "R";
}
//throw new InvalidOperationException("Data lost!!");
}
}
else
{
Cursor.Current = Cursors.Default;
Cursor.Hide();
return "G";
}
}
2013年11月27日 星期三
[Java] public/private/protected
在說明這四個關鍵字之前,我想就class之間的關係做一個簡單的定義,對於繼承自己的class,base class可以認為他們都是自己的子女,而對於和自己一個目錄下的classes,認為都是自己的朋友。
1、public:public表明該數據成員、成員函數是對所有用戶開放,所有用戶都可以直接進行調用
2、private:private表示私有,私有的意思就是除了class自己之外,任何人都不可以直接使用,私有財產神聖不可侵犯嘛,即便是子女,朋友,都不可以使用。
3、protected:protected對於子女、朋友來說,就是public的,可以自由使用,沒有任何限制,而對於其他的外部class,protected就變成private。
作用域 當前類 同一package 子孫類 其他package
public V V V V
protected V V V X
friendly V V X X
private V X X X
不寫時默認為friendly
1、public:public表明該數據成員、成員函數是對所有用戶開放,所有用戶都可以直接進行調用
2、private:private表示私有,私有的意思就是除了class自己之外,任何人都不可以直接使用,私有財產神聖不可侵犯嘛,即便是子女,朋友,都不可以使用。
3、protected:protected對於子女、朋友來說,就是public的,可以自由使用,沒有任何限制,而對於其他的外部class,protected就變成private。
作用域 當前類 同一package 子孫類 其他package
public V V V V
protected V V V X
friendly V V X X
private V X X X
不寫時默認為friendly
訂閱:
文章 (Atom)