From 6ce345ba9030471bc6fe5a30c5a39fe76d5a71af Mon Sep 17 00:00:00 2001 From: lindexi Date: Tue, 18 Dec 2018 14:25:23 +0800 Subject: [PATCH 1/3] Format the code MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit In [corefx/coding-style.md at master · dotnet/corefx](https://github.com/dotnet/corefx/blob/master/Documentation/coding-guidelines/coding-style.md ) NO.1, we use Allman style braces, where each brace begins on a new line. A single line statement block can go without braces but the block must be properly indented on its own line and must not be nested in other statement blocks that use braces. --- .../Application/CS/source.cs | 117 +++++++++++------- 1 file changed, 74 insertions(+), 43 deletions(-) diff --git a/snippets/csharp/VS_Snippets_Winforms/Application/CS/source.cs b/snippets/csharp/VS_Snippets_Winforms/Application/CS/source.cs index 645b5cd3562..47c9052332e 100644 --- a/snippets/csharp/VS_Snippets_Winforms/Application/CS/source.cs +++ b/snippets/csharp/VS_Snippets_Winforms/Application/CS/source.cs @@ -9,25 +9,30 @@ namespace MyApplication { // A simple form that represents a window in our application - public class AppForm2 : System.Windows.Forms.Form { - public AppForm2(){ - this.Size = new System.Drawing.Size(300,300); + public class AppForm2 : System.Windows.Forms.Form + { + public AppForm2() + { + this.Size = new System.Drawing.Size(300, 300); this.Text = "AppForm2"; } } // A simple form that represents a window in our application - public class AppForm1 : System.Windows.Forms.Form { - public AppForm1(){ - this.Size = new System.Drawing.Size(300,300); + public class AppForm1 : System.Windows.Forms.Form + { + public AppForm1() + { + this.Size = new System.Drawing.Size(300, 300); this.Text = "AppForm1"; } } // // The class that handles the creation of the application windows - class MyApplicationContext : ApplicationContext { - + class MyApplicationContext : ApplicationContext + { + private int formCount; private AppForm1 form1; private AppForm2 form2; @@ -38,19 +43,23 @@ class MyApplicationContext : ApplicationContext { private FileStream userData; // - private MyApplicationContext() { + private MyApplicationContext() + { formCount = 0; // Handle the ApplicationExit event to know when the application is exiting. Application.ApplicationExit += new EventHandler(this.OnApplicationExit); - try { + try + { // Create a file that the application will store user specific data in. userData = new FileStream(Application.UserAppDataPath + "\\appdata.txt", FileMode.OpenOrCreate); - } catch(IOException e) { + } + catch (IOException e) + { // Inform the user that an error occurred. - MessageBox.Show("An error occurred while attempting to show the application." + + MessageBox.Show("An error occurred while attempting to show the application." + "The error is:" + e.ToString()); // Exit the current thread instead of showing the windows. @@ -60,22 +69,23 @@ private MyApplicationContext() { // Create both application forms and handle the Closed event // to know when both forms are closed. form1 = new AppForm1(); - form1.Closed += new EventHandler(OnFormClosed); - form1.Closing += new CancelEventHandler(OnFormClosing); + form1.Closed += new EventHandler(OnFormClosed); + form1.Closing += new CancelEventHandler(OnFormClosing); formCount++; form2 = new AppForm2(); - form2.Closed += new EventHandler(OnFormClosed); - form2.Closing += new CancelEventHandler(OnFormClosing); + form2.Closed += new EventHandler(OnFormClosed); + form2.Closing += new CancelEventHandler(OnFormClosing); formCount++; // Get the form positions based upon the user specific data. - if (ReadFormDataFromFile()) { + if (ReadFormDataFromFile()) + { // If the data was read from the file, set the form // positions manually. form1.StartPosition = FormStartPosition.Manual; form2.StartPosition = FormStartPosition.Manual; - + form1.Bounds = form1Position; form2.Bounds = form2Position; } @@ -85,41 +95,48 @@ private MyApplicationContext() { form2.Show(); } - private void OnApplicationExit(object sender, EventArgs e) { + private void OnApplicationExit(object sender, EventArgs e) + { // When the application is exiting, write the application data to the // user file and close it. WriteFormDataToFile(); - try { + try + { // Ignore any errors that might occur while closing the file handle. userData.Close(); - } catch {} + } + catch { } } // - private void OnFormClosing(object sender, CancelEventArgs e) { + private void OnFormClosing(object sender, CancelEventArgs e) + { // When a form is closing, remember the form position so it // can be saved in the user data file. - if (sender is AppForm1) + if (sender is AppForm1) form1Position = ((Form)sender).Bounds; else if (sender is AppForm2) form2Position = ((Form)sender).Bounds; } // - private void OnFormClosed(object sender, EventArgs e) { + private void OnFormClosed(object sender, EventArgs e) + { // When a form is closed, decrement the count of open forms. // When the count gets to 0, exit the app by calling // ExitThread(). formCount--; - if (formCount == 0) { + if (formCount == 0) + { ExitThread(); } } // - private bool WriteFormDataToFile(){ + private bool WriteFormDataToFile() + { // Write the form positions to the file. UTF8Encoding encoding = new UTF8Encoding(); @@ -129,36 +146,44 @@ private bool WriteFormDataToFile(){ byte[] dataToWrite = encoding.GetBytes("~" + form1pos + "~" + form2pos); - try { + try + { // Set the write position to the start of the file and write - userData.Seek(0,SeekOrigin.Begin); + userData.Seek(0, SeekOrigin.Begin); userData.Write(dataToWrite, 0, dataToWrite.Length); userData.Flush(); userData.SetLength(dataToWrite.Length); return true; - } catch { + } + catch + { // An error occurred while attempting to write, return false. return false; } } - private bool ReadFormDataFromFile(){ + private bool ReadFormDataFromFile() + { // Read the form positions from the file. UTF8Encoding encoding = new UTF8Encoding(); String data; - if (userData.Length != 0) { + if (userData.Length != 0) + { byte[] dataToRead = new Byte[userData.Length]; - try { + try + { // Set the read position to the start of the file and read. userData.Seek(0, SeekOrigin.Begin); userData.Read(dataToRead, 0, dataToRead.Length); - } catch (IOException e) { + } + catch (IOException e) + { String errorInfo = e.ToString(); // An error occurred while attempt to read, return false. return false; @@ -167,34 +192,40 @@ private bool ReadFormDataFromFile(){ // Parse out the data to get the window rectangles data = encoding.GetString(dataToRead); - try { + try + { // Convert the string data to rectangles RectangleConverter rectConv = new RectangleConverter(); - String form1pos = data.Substring(1,data.IndexOf("~",1)-1); + String form1pos = data.Substring(1, data.IndexOf("~", 1) - 1); form1Position = (Rectangle)rectConv.ConvertFromString(form1pos); - String form2pos = data.Substring(data.IndexOf("~",1)+1); + String form2pos = data.Substring(data.IndexOf("~", 1) + 1); form2Position = (Rectangle)rectConv.ConvertFromString(form2pos); return true; - } catch { + } + catch + { // Error occurred while attempting to convert the rectangle data. // Return false to use default values. return false; } - } else { + } + else + { // No data in the file, return false to use default values. return false; } - } - + } + // [STAThread] - static void Main(string[] args) { - + static void Main(string[] args) + { + // Create the MyApplicationContext, that derives from ApplicationContext, // that manages when the application should exit. @@ -209,4 +240,4 @@ static void Main(string[] args) { } // } -// \ No newline at end of file +// From 7bc4f0ec5f469e5830cf28a594c8a1de55855b97 Mon Sep 17 00:00:00 2001 From: lindexi Date: Tue, 18 Dec 2018 14:28:37 +0800 Subject: [PATCH 2/3] Change the fields. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit In [corefx/coding-style.md at master · dotnet/corefx](https://github.com/dotnet/corefx/blob/master/Documentation/coding-guidelines/coding-style.md ) NO.3, we use _camelCase for internal and private fields --- .../Application/CS/source.cs | 78 +++++++++---------- 1 file changed, 39 insertions(+), 39 deletions(-) diff --git a/snippets/csharp/VS_Snippets_Winforms/Application/CS/source.cs b/snippets/csharp/VS_Snippets_Winforms/Application/CS/source.cs index 47c9052332e..f60c8603f28 100644 --- a/snippets/csharp/VS_Snippets_Winforms/Application/CS/source.cs +++ b/snippets/csharp/VS_Snippets_Winforms/Application/CS/source.cs @@ -33,19 +33,19 @@ public AppForm1() class MyApplicationContext : ApplicationContext { - private int formCount; - private AppForm1 form1; - private AppForm2 form2; + private int _formCount; + private AppForm1 _form1; + private AppForm2 _form2; - private Rectangle form1Position; - private Rectangle form2Position; + private Rectangle _form1Position; + private Rectangle _form2Position; - private FileStream userData; + private FileStream _userData; // private MyApplicationContext() { - formCount = 0; + _formCount = 0; // Handle the ApplicationExit event to know when the application is exiting. Application.ApplicationExit += new EventHandler(this.OnApplicationExit); @@ -53,7 +53,7 @@ private MyApplicationContext() try { // Create a file that the application will store user specific data in. - userData = new FileStream(Application.UserAppDataPath + "\\appdata.txt", FileMode.OpenOrCreate); + _userData = new FileStream(Application.UserAppDataPath + "\\appdata.txt", FileMode.OpenOrCreate); } catch (IOException e) @@ -68,31 +68,31 @@ private MyApplicationContext() // Create both application forms and handle the Closed event // to know when both forms are closed. - form1 = new AppForm1(); - form1.Closed += new EventHandler(OnFormClosed); - form1.Closing += new CancelEventHandler(OnFormClosing); - formCount++; + _form1 = new AppForm1(); + _form1.Closed += new EventHandler(OnFormClosed); + _form1.Closing += new CancelEventHandler(OnFormClosing); + _formCount++; - form2 = new AppForm2(); - form2.Closed += new EventHandler(OnFormClosed); - form2.Closing += new CancelEventHandler(OnFormClosing); - formCount++; + _form2 = new AppForm2(); + _form2.Closed += new EventHandler(OnFormClosed); + _form2.Closing += new CancelEventHandler(OnFormClosing); + _formCount++; // Get the form positions based upon the user specific data. if (ReadFormDataFromFile()) { // If the data was read from the file, set the form // positions manually. - form1.StartPosition = FormStartPosition.Manual; - form2.StartPosition = FormStartPosition.Manual; + _form1.StartPosition = FormStartPosition.Manual; + _form2.StartPosition = FormStartPosition.Manual; - form1.Bounds = form1Position; - form2.Bounds = form2Position; + _form1.Bounds = _form1Position; + _form2.Bounds = _form2Position; } // Show both forms. - form1.Show(); - form2.Show(); + _form1.Show(); + _form2.Show(); } private void OnApplicationExit(object sender, EventArgs e) @@ -104,7 +104,7 @@ private void OnApplicationExit(object sender, EventArgs e) try { // Ignore any errors that might occur while closing the file handle. - userData.Close(); + _userData.Close(); } catch { } } @@ -115,9 +115,9 @@ private void OnFormClosing(object sender, CancelEventArgs e) // When a form is closing, remember the form position so it // can be saved in the user data file. if (sender is AppForm1) - form1Position = ((Form)sender).Bounds; + _form1Position = ((Form)sender).Bounds; else if (sender is AppForm2) - form2Position = ((Form)sender).Bounds; + _form2Position = ((Form)sender).Bounds; } // @@ -127,8 +127,8 @@ private void OnFormClosed(object sender, EventArgs e) // When the count gets to 0, exit the app by calling // ExitThread(). - formCount--; - if (formCount == 0) + _formCount--; + if (_formCount == 0) { ExitThread(); } @@ -141,19 +141,19 @@ private bool WriteFormDataToFile() UTF8Encoding encoding = new UTF8Encoding(); RectangleConverter rectConv = new RectangleConverter(); - String form1pos = rectConv.ConvertToString(form1Position); - String form2pos = rectConv.ConvertToString(form2Position); + String form1pos = rectConv.ConvertToString(_form1Position); + String form2pos = rectConv.ConvertToString(_form2Position); byte[] dataToWrite = encoding.GetBytes("~" + form1pos + "~" + form2pos); try { // Set the write position to the start of the file and write - userData.Seek(0, SeekOrigin.Begin); - userData.Write(dataToWrite, 0, dataToWrite.Length); - userData.Flush(); + _userData.Seek(0, SeekOrigin.Begin); + _userData.Write(dataToWrite, 0, dataToWrite.Length); + _userData.Flush(); - userData.SetLength(dataToWrite.Length); + _userData.SetLength(dataToWrite.Length); return true; } @@ -171,15 +171,15 @@ private bool ReadFormDataFromFile() UTF8Encoding encoding = new UTF8Encoding(); String data; - if (userData.Length != 0) + if (_userData.Length != 0) { - byte[] dataToRead = new Byte[userData.Length]; + byte[] dataToRead = new Byte[_userData.Length]; try { // Set the read position to the start of the file and read. - userData.Seek(0, SeekOrigin.Begin); - userData.Read(dataToRead, 0, dataToRead.Length); + _userData.Seek(0, SeekOrigin.Begin); + _userData.Read(dataToRead, 0, dataToRead.Length); } catch (IOException e) @@ -198,10 +198,10 @@ private bool ReadFormDataFromFile() RectangleConverter rectConv = new RectangleConverter(); String form1pos = data.Substring(1, data.IndexOf("~", 1) - 1); - form1Position = (Rectangle)rectConv.ConvertFromString(form1pos); + _form1Position = (Rectangle)rectConv.ConvertFromString(form1pos); String form2pos = data.Substring(data.IndexOf("~", 1) + 1); - form2Position = (Rectangle)rectConv.ConvertFromString(form2pos); + _form2Position = (Rectangle)rectConv.ConvertFromString(form2pos); return true; From 21ef140d6df263aa343e45c417cd349d9f846392 Mon Sep 17 00:00:00 2001 From: lindexi Date: Tue, 18 Dec 2018 14:31:18 +0800 Subject: [PATCH 3/3] Use keyword MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit In [corefx/coding-style.md at master · dotnet/corefx](https://github.com/dotnet/corefx/blob/master/Documentation/coding-guidelines/coding-style.md ) NO.11, we use language keywords instead of BCL types. --- .../VS_Snippets_Winforms/Application/CS/source.cs | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/snippets/csharp/VS_Snippets_Winforms/Application/CS/source.cs b/snippets/csharp/VS_Snippets_Winforms/Application/CS/source.cs index f60c8603f28..9a9ef1443f9 100644 --- a/snippets/csharp/VS_Snippets_Winforms/Application/CS/source.cs +++ b/snippets/csharp/VS_Snippets_Winforms/Application/CS/source.cs @@ -141,8 +141,8 @@ private bool WriteFormDataToFile() UTF8Encoding encoding = new UTF8Encoding(); RectangleConverter rectConv = new RectangleConverter(); - String form1pos = rectConv.ConvertToString(_form1Position); - String form2pos = rectConv.ConvertToString(_form2Position); + string form1pos = rectConv.ConvertToString(_form1Position); + string form2pos = rectConv.ConvertToString(_form2Position); byte[] dataToWrite = encoding.GetBytes("~" + form1pos + "~" + form2pos); @@ -169,11 +169,11 @@ private bool ReadFormDataFromFile() { // Read the form positions from the file. UTF8Encoding encoding = new UTF8Encoding(); - String data; + string data; if (_userData.Length != 0) { - byte[] dataToRead = new Byte[_userData.Length]; + byte[] dataToRead = new byte[_userData.Length]; try { @@ -184,7 +184,7 @@ private bool ReadFormDataFromFile() } catch (IOException e) { - String errorInfo = e.ToString(); + string errorInfo = e.ToString(); // An error occurred while attempt to read, return false. return false; } @@ -196,11 +196,11 @@ private bool ReadFormDataFromFile() { // Convert the string data to rectangles RectangleConverter rectConv = new RectangleConverter(); - String form1pos = data.Substring(1, data.IndexOf("~", 1) - 1); + string form1pos = data.Substring(1, data.IndexOf("~", 1) - 1); _form1Position = (Rectangle)rectConv.ConvertFromString(form1pos); - String form2pos = data.Substring(data.IndexOf("~", 1) + 1); + string form2pos = data.Substring(data.IndexOf("~", 1) + 1); _form2Position = (Rectangle)rectConv.ConvertFromString(form2pos); return true;