Some Methods (C#)

 

1. Get the last element of a string

    I.    

            string  s  =   " 123 " ;
            
string  a  =   "   " ;
           IEnumerator Enum 
=  s.GetEnumerator();
          
while  (Enum.MoveNext())
  
{
              a 
= Enum.Current.ToString();
         }

 

   II.      

            string  s  =   " 23456 " ;
            
char [] a  =  s.ToCharArray();
            
int  i  =  ( int )a.Length;
            a[i 
-   1 ].ToString();   

  III.        if you want to get at an individual char in a string for any reason, just use the index operator:
                 

                string  str  =   " ABCDEFG " ;
                 
int  index  =   2 ;
                 
char  ch  =  str[index];  

 

2. Get rid of the last element of a string

  I.       

            string  s  =   " 23456 " ;
            
char [] a  =  s.ToCharArray();
            
int  i  =  ( int )a.Length;
            
string  p  =   ""
           
for  ( int  j  =   0 ; j  <  (i  -   1 ); j ++ )
           
{
                        p 
= p + a[j];   
                 }

 

 II.        

   Good Approach

              if  (s.Length  >   0 )
             s 
=  s.Substring( 0 , s.Length  -   1 );

 

3. Write data to a file(.txt or .xls)

   I.     

              String path  =   @" C: "   +  DateTime.Now.ToLongDateString()  +   " .xls "

           FileStream file 
=   new  FileStream(path, FileMode.Append, FileAccess.Write);
                
{
                    StreamWriter sw 
= new StreamWriter(file);
                    sw.WriteLine(String.Format(
"{0} {1} {2} {3} {4} {5} {6} {7} {8}""Time""Channel0""Channel1""Channel2""Channel3""Channel4""Channel5""Channel6""Channel7"));
                    sw.Flush();
                    sw.Close();
                }


 

    II.     

using  (StreamWriter sw  =   new  StreamWriter(( @" C: "   +  DateTime.Now.ToLongDateString()  + " .xls "  ), true ))
            
{
                sw.WriteLine(String.Format(
"{0} {1} {2} {3} {4} {5} {6} {7} {8}""Time""Channel0""Channel1""Channel2""Channel3""Channel4""Channel5""Channel6""Channel7"));
           }

 

 4. Define the format of date and time as you like

    

 
         
       
//  User-Defined Time Format
            public   string  GetTime()

                
{

                    
string TimeInString = "";

                    
int day = DateTime.Now.Day;

                    
int month = DateTime.Now.Month;

                    
int year = DateTime.Now.Year;

                    
int hour = DateTime.Now.Hour;

                    
int min = DateTime.Now.Minute;

                    
int sec = DateTime.Now.Second;

                    
string AM = "AM";

                    
string PM = "PM";

                    TimeInString 
= (day < 10? "0" + day.ToString() : day.ToString();

                    TimeInString 
+= "-" + ((month < 10? "0" + month.ToString() : month.ToString());

                    TimeInString 
+= "-" + ((year < 10? "0" + year.ToString() : year.ToString());

                    TimeInString 
+= " " + ((hour < 10? "0" + hour.ToString() : hour.ToString());

                    TimeInString 
+= ":" + ((min < 10? "0" + min.ToString() : min.ToString());

                    TimeInString 
+= ":" + ((sec < 10? "0" + sec.ToString() : sec.ToString());

                    TimeInString 
+= " " + ((hour < 12? "" + AM : PM);

                    
return TimeInString;

                }

        
        

  

string  GetTime()
{
       
return DateTime.Now.ToString("dd-MM-yyyy hh:mm:ss tt");
}


// However, you should note that you cannot have a colon in the middle of a filename, and you are probably going to have trouble with the space.

string  GetTime()
{
  
return DateTime.Now.ToString("ddmmyy'hh.mm.sstt'");
}

  That gives the time as "2006-09-13'09.50.35AM", which is readable, sorts well and has no troublesome characters.   
   DateTime.Now.ToString("ddMMyyyyHHmmss")
I will propose format to be like "yyyyMMddHHmm". There is not big deal with this format except when the sort of files is by name also will be sorted by date.

The DateTime.Now.Millisecond returns back the current millisecond of the current time.
The formatting only applies when you do DateTime.Now.ToString() .
Use
DateTime.Now.ToString("HH:mm:ss ") + DateTime.Now.MilliSecond.ToString() + DateTime.Now.ToString(" tt");

The time will be like:

12:35:13 437 PM

 5. Manualy location the forms

                 

private   void  Form1_Load( object  sender, EventArgs e)
        
{
            
//manually decide the location of the form
            Location = new System.Drawing.Point(1010);
            Application.EnableVisualStyles();
            FormBorderStyle 
= FormBorderStyle.None;
            
this.ClientSize = this.BackgroundImage.Size;
        }
 
 // Minimize/Maximinze an form:
  // to minimize:

          this.WindowState = FormWindowState.Minimize;

// to maximize:

         this.WindowState = FormWindowState.Maximize;

 

 6. Write data to an Excel file

    

  public   void  port_DataReceived( object  sender, SerialDataReceivedEventArgs e)
           
        
{
            
//This method will be called when there is data waiting in the port's buffer
            comport.ReadTimeout = 300;
            String s 
= comport.ReadExisting();
            CM.Log(LogMsgType.Incoming, s);
             
string path = @"C:" + DateTime.Now.ToLongDateString() + ".xls";
             
if (!File.Exists(path))
             
{
                 FileStream file 
= new FileStream(path, FileMode.Append, FileAccess.Write);
                 
{
                     StreamWriter sw 
= new StreamWriter(file);
                     sw.WriteLine(String.Format(
"{0} {1} {2} {3} {4} {5} {6} {7} {8}""Time""Channel 0""Channel 1""Channel 2""Channel 3""Channel 4""Channel 5""Channel 6""Channel 7"));
                     sw.Flush();
                     sw.Close();
                 }

             }

            
using (StreamWriter sw = new StreamWriter(path,true))
            
{
            
//split the string 
               string[] parts = s.Split(new string[] " "" " }, StringSplitOptions.RemoveEmptyEntries);
                
foreach (string part in parts)
                
{
                    
//convert the string to character array
                    char[] a = part.ToCharArray();
                    
//get the length of the array
                    int i = (int)a.Length;
                  
//Get rid of the last elsment of the original string
                     string p = "";
                    
string blank = "        ";
                    
for (int j = 0; j < (i-1); j++)
                    
{
                        p 
= p + a[j];
                    }

               
//convert chart to string
                    string choose = Convert.ToString(a[i - 1]);
               
//convert hex string to decimal
                    int dec = int.Parse(p, System.Globalization.NumberStyles.HexNumber);
                    
switch (choose)
                    
{
                        
case "0": sw.WriteLine("{0} {1}", DateTime.Now.ToLongTimeString(), dec);
                            
break;
                        
case "1": sw.WriteLine("{0} {1} {2}", DateTime.Now.ToLongTimeString(), blank, dec);
                            
break;
                        
case "2": sw.WriteLine("{0} {1} {2} {3}", DateTime.Now.ToLongTimeString(), blank, blank, dec);
                            
break;
                        
case "3": sw.WriteLine("{0} {1} {2} {3} {4}", DateTime.Now.ToLongTimeString(), blank, blank, blank, dec);
                            
break;
                        
case "4": sw.WriteLine("{0} {1} {2} {3} {4} {5}", DateTime.Now.ToLongTimeString(), blank, blank, blank, blank, dec);
                            
break;
                        
case "5": sw.WriteLine("{0} {1} {2} {3} {4} {5} {6}", DateTime.Now.ToLongTimeString(), blank, blank, blank, blank, blank, dec);
                            
break;
                        
case "6": sw.WriteLine("{0} {1} {2} {3} {4} {5} {6} {7}", DateTime.Now.ToLongTimeString(), blank, blank, blank, blank, blank, blank, dec);
                            
break;
                        
case "7": sw.WriteLine("{0} {1} {2} {3} {4} {5} {6} {7} {8}", DateTime.Now.ToLongTimeString(), blank, blank, blank, blank, blank, blank, blank, dec);
                            
break;
                        
default: CM.Log(LogMsgType.Error, " The Incoming data is with wrong format  Channel not tested ");
                            
break;

                    }
              
               }


            }



        }

 

7. Common Dialog

 // Create and display a Choose Color dialog box.
            ColorDialog cd = new ColorDialog ();
            cd.ShowDialog ();
            cd.Dispose ();
            
            // Create and display a Choose Font dialog box.
            FontDialog fd = new FontDialog ();
            fd.ShowDialog ();
            fd.Dispose ();
                
            // Create and display an Open File dialog box.
            OpenFileDialog ofd = new OpenFileDialog ();
            ofd.ShowDialog ();
            ofd.Dispose ();
            
            // Create and display a Save File dialog box.
            SaveFileDialog sfd = new SaveFileDialog();
            sfd.ShowDialog ();
            sfd.Dispose ();
            
            // Create and display a Page Setup dialog box.
            PrintDocument printDoc = new PrintDocument();
            PageSetupDialog psd = new PageSetupDialog ();
            psd.Document = printDoc;
            psd.ShowDialog ();
            psd.Dispose ();
            
            // Create and display an Print dialog box.
            PrintDialog pd = new PrintDialog ();
            pd.Document = printDoc;
            pd.ShowDialog ();
            pd.Dispose ();
            
            // Create and display an Print Preview File dialog box.
            // This dialog is not a part of the common dialog library.
            PrintPreviewDialog ppd = new PrintPreviewDialog ();
            ppd.ShowDialog ();
            ppd.Dispose ();
            printDoc.Dispose ();


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值