C# Note
MessageBox.Show("Import Finished!!", "Process Message", MessageBoxButtons.OK, MessageBoxIcon.Information);
2008.03.26
*DateTime format convert
using System;
using System.Globalization;
public class MainClass {
public static void Main(string[] args) {
DateTime dt = DateTime.Now;
String[] format = {
"d", "D",
"f", "F",
"g", "G",
"m",
"r",
"s",
"t", "T",
"u", "U",
"y",
"dddd, MMMM dd yyyy",
"ddd, MMM d \"'\"yy",
"dddd, MMMM dd",
"M/yy",
"dd-MM-yy",
};
String date;
for (int i = 0; i < format.Length; i++) {
date = dt.ToString(format[i], DateTimeFormatInfo.InvariantInfo);
Console.WriteLine(String.Concat(format[i], " :" , date));
}
/** Output.
*
* d :08/17/2000
* D :Thursday, August 17, 2000
* f :Thursday, August 17, 2000 16:32
* F :Thursday, August 17, 2000 16:32:32
* g :08/17/2000 16:32
* G :08/17/2000 16:32:32
* m :August 17
* r :Thu, 17 Aug 2000 23:32:32 GMT
* s :2000-08-17T16:32:32
* t :16:32
* T :16:32:32
* u :2000-08-17 23:32:32Z
* U :Thursday, August 17, 2000 23:32:32
* y :August, 2000
* dddd, MMMM dd yyyy :Thursday, August 17 2000
* ddd, MMM d "'"yy :Thu, Aug 17 '00
* dddd, MMMM dd :Thursday, August 17
* M/yy :8/00
* dd-MM-yy :17-08-00
*/
}
}
2008.03.25
*SMTPResponse:int
private enum SMTPResponse: int
{
CONNECT_SUCCESS = 220,
GENERIC_SUCCESS = 250,
DATA_SUCCESS = 354,
QUIT_SUCCESS = 221
}
2008.03.21
*MessageBox 可以使用整理為參數
MessageBox.Show(pictureBox1.Image.Width + "\t" + pictureBox1.Image.Height);
^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^
整數 整數
2008.03.21
*C# 讀取圖片像數大小
MessageBox.Show(pictureBox1.Image.Width + "\t" + pictureBox1.Image.Height);
pictureBox1.Image.Width pictureBox1.Image.Height
^^^ ^^^^
上下兩句得到的結果一樣。
pictureBox1.Image.PhysicalDimension.Width pictureBox1.Image.PhysicalDimension.Height
^^^ ^^^^
2008.03.21
*C#產生出Access 的.mdb檔
如何透過使用 ADOX 和 Visual C# . NET 來建立 Access 資料庫
http://support.microsoft.com/kb/317881
程式設計人員時常需要利用程式設計方式來建立資料庫。. 雖然 ActiveX Data Objects (ADO) 或 ADO . NET 都提供方法來建立 Microsoft Access 資料庫, 自動您可以使用 Microsoft Jet OLE DB Provider 與 Microsoft ADO Ext . 2.7 透過 COM Interop 層以手動方式建立資料庫為 DDL 和安全性 (ADOX)。
會使用到的DLL C:\Program Files\Common Files\System\ado\msadox.dll
ADOX.CatalogClass cat = new ADOX.CatalogClass();
cat.Create("Provider=Microsoft.Jet.OLEDB.4.0;" +
"Data Source=D:\\\\AccessDB\\\\NewMDB.mdb;" +
"Jet OLEDB:Engine Type=5");
Console.WriteLine("Database Created Successfully");
cat = null;
2008.03.12
*XML的實際應用
XmlDocument doc = new XmlDocument();
doc.Load("Config.xml");
XmlNode RootNode = doc.SelectSingleNode("Config");
XmlNode CatalogNode = RootNode.SelectSingleNode("Catalog");
CatalogNode = CatalogNode.NextSibling;
MessageBox.Show(CatalogNode.InnerText);
XmlNamespaceManager xnmgr = new XmlNamespaceManager(doc.NameTable);
xnmgr.AddNamespace("Name", "");
XmlNodeList NodeList = CatalogNode.SelectNodes("Item/@Name", xnmgr);
if (NodeList.Count == 0) { MessageBox.Show("Node's count is small than 0"); }
for (int i = 0; i < NodeList.Count; i++)
{
MessageBox.Show(NodeList[i].Value);
}
doc.Save("Config.xml");
2008.03.09
*SplashScreen.cs
using (Font font = new Font("Sans Serif", 4))
{
using (Graphics g = Graphics.FromImage(bitmap))
{
g.DrawString(versionText, font, Brushes.Black, 100, 142);
}
}
2008.03.09
*Sharp Develop Splash Screen form setting
using System.Reflection;
[assembly: System.Runtime.InteropServices.ComVisible(false)]
[assembly: AssemblyCompany("ic#code")]
[assembly: AssemblyProduct("SharpDevelop")]
[assembly: AssemblyCopyright("2000-2007 AlphaSierraPapa")]
[assembly: AssemblyVersion(RevisionClass.FullVersion)]
2008.03.08
*XML的規則
上面是XML檔的內容,其中bka為prefix;urn:samples為namespace。(在urn:samples裡,有一個bka為prefix的字串)
C# 的程式碼:
AddNamespace("bka", "urn:samples") 使用參數加入規則。
SelectNodes("/bookstore/book/@bka:ISBN", nsmgr); 選擇Node的path。
XmlDocument doc = new XmlDocument();
doc.Load("booksort.xml");
//Create an XmlNamespaceManager for resolving namespaces.
XmlNamespaceManager nsmgr = new XmlNamespaceManager(doc.NameTable);
nsmgr.AddNamespace("bka", "urn:samples");
//Select and display the value of all the ISBN attributes.
XmlNodeList nodeList;
XmlElement root = doc.DocumentElement;
nodeList = root.SelectNodes("/bookstore/book/@bka:ISBN", nsmgr);
foreach (XmlNode isbn in nodeList)
{
MessageBox.Show(isbn.Value);
}
2008.03.07
*Detect OS Platform
if (Environment.OSVersion.Platform == PlatformID.WinCE)
2008.03.06
*為Control 加上Event
// Declare delegate for submit button clicked.
// Most action events (like the Click event) in Windows Forms
// use the EventHandler delegate and the EventArgs arguments.
// We will define our own delegate that does not specify parameters.
// Mostly, we really don't care what the conditions of the
// click event for the Submit button were, we just care that
// the Submit button was clicked.
public delegate void SubmitClickedHandler();
// Declare the event, which is associated with our
// delegate SubmitClickedHandler(). Add some attributes
// for the Visual C# control property.
[Category("Action")]
[Description("Fires when the Submit button is clicked.")]
public event SubmitClickedHandler SubmitClicked; ----------------> //Type is public delegate void SubmitClickedHandler();
// calling the event.
private void btnSubmit_Click(object sender, System.EventArgs e)
{
if (txtName.Text.Length == 0)
{
MessageBox.Show("Please enter your name.");
}
else
{
OnSubmitClicked();---------------------------------> //Call public event SubmitClickedHandler SubmitClicked;
}
}
protected virtual void OnSubmitClicked()
{
// If an event has no subscribers registerd, it will
// evaluate to null. The test checks that the value is not
// null, ensuring that there are subsribers before
// calling the event itself.
if (SubmitClicked != null)
{
SubmitClicked(); // Notify Subscribers
}
}
2008.03.06
*Control 的Category
Category("Progress control"),
Description("The progress control style"),
* Convert hex string value to integer
Convert.ToString(Convert.ToByte(tb_16Value.Text, 16), 2));
* Detect the length of string and append "0" for you string.
private string AppendZero(string _str)
{
int addCount = 8 - _str.Length;
if (addCount != 0)
{
for (int i = 0; i < addCount; i++)
{
_str = _str.Insert(0, "0");
}
}
return _str;
}
* 別人的求時間差方法
startTime = DateTime.Now;
stopTime = DateTime.Now;
elapsedTime = stopTime.Subtract(startTime);
elapsedMilliseconds = (int)elapsedTime.TotalMilliseconds;
* List
List
List
* Error MessageBox
MessageBox.Show(Exp.InnerException + "\r\n\r\n" + Exp.Message + "\r\n\r\n" + Exp.Source + "\r\n\r\n" + Exp.StackTrace + "\r\n\r\n" + Exp.TargetSite);
* 隱藏Main Form
private void Form1_Load(object sender, EventArgs e)
{
this.Hide();
this.ShowInTaskbar = false;
}
[ ERROR MESSAGE ]
* must declare a body because it is not marked abstract or extern
C# abstract class cant obtain virtual method without body!!!
*使用系統顏色 System Color
Namespace: System.Drawing
Assembly: System.Drawing (in system.drawing.dll)
// Get all the values from the KnownColor enumeration.
KnownColor.Control
*Pass by reference in C#
private unsafe void button1_Click(object sender, EventArgs e)
{
int a = 100;
ABC(ref a);
textBox1.Text = a.ToString();
}
private unsafe void ABC(ref int abc)
{
abc = 5;
}
*Pass by reference though Fixed
[DllImport("Susi.dll")]
unsafe static extern bool SusiCoreAccessBootCounter(UInt32 mode, UInt32 OPFlag, bool* enable, UInt32* value);
static unsafe public bool SusiCoreAccessBootCounter_(UInt32 mode, UInt32 OPFlag, ref bool enable, ref UInt32 value)
{
bool[] enable1 = new bool[1];
uint[] value1 = new uint[1];
enable1[0]=enable;
value1[0] = value;
bool RTN_Value;
fixed (bool* pBool = enable1)
{
fixed (uint* pValue = value1)
{
RTN_Value= SusiCoreAccessBootCounter(mode, OPFlag, pBool, pValue);
}
}
enable = enable1[0];
value = value1[0];
return RTN_Value;
}
*Pass by reference though Fixed
[System.Runtime.InteropServices.DllImport("Kernel32")]
static extern unsafe bool GetComputerName(byte* lpBuffer,long* nSize);
static void Main()
{
byte[] buffor = new byte[512];
long size = buffor.Length;
unsafe
{
long* pSize = &size;
fixed (byte* pBuffor = buffor)
{
GetComputerName(pBuffor,pSize);
}
2008.03.26
*得到執行檔目前路徑
int IndexStart = System.Reflection.Assembly.GetExecutingAssembly().Location.LastIndexOf("\\");
Properties.Settings.Default.g_RootPath =
System.Reflection.Assembly.GetExecutingAssembly().Location.Substring(0, IndexStart);
*Get execute file's path(取得目前執行檔所在的路徑)
Solved
int IndexStart = System.Reflection.Assembly.GetExecutingAssembly().Location.LastIndexOf("\\");
Properties.Settings.Default.g_RootPath = System.Reflection.Assembly.GetExecutingAssembly().Location.Substring(0, IndexStart);
Waitting for Solving
Environment.CurrentDirectory

0 Comments:
Post a Comment
<< Home