// User-Defined Time Format publicstring 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
privatevoid Form1_Load(object sender, EventArgs e) ...{ //manually decide the location of the form Location =new System.Drawing.Point(10, 10); 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
publicvoid 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(newstring[] ...{ "", "" }, 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 ();