AYR's Blog

Friday, March 28, 2008

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\adomsadox.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檔的內容,其中bkaprefixurn:samplesnamespace(urn:samples裡,有一個bkaprefix的字串)







C# 的程式碼:

AddNamespace("bka", "urn:samples")            使用參數加入規則。   

SelectNodes("/bookstore/book/@bka:ISBN", nsmgr);   選擇Nodepath。    







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);

}











Pride And Prejudice



Jane

Austen















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 m_VoltagePanelList;

List

0 Comments:

Post a Comment

<< Home