C# Code Formatting Demo


FormatCode Code Formatter is based on serveral powerful syntax parser engines so you can exactly control the appearance of your source code. Here is the code formatting demo of FormatCode C# Code Formatter to prove the ability of code formatting functions of FormatCode:

   /* Before Code Formatting */

   namespace DataSetExample{using System;using System.Drawing;using
   System.Collections;using System.ComponentModel;using System.WinForms;using
   System.Data;using System.Data.SQL;/// <summary>
   ///    Summary description for Form1.
   /// </summary>
   public class Form1:System.WinForms.Form{/// <summary>
   ///    Required designer variable.
   /// </summary>
   private System.ComponentModel.Container components;private
   System.WinForms.MenuItem ExitTest;private System.WinForms.MenuItem RunTest;
   private System.WinForms.MenuItem menuItem1;private System.WinForms.MainMenu
   mainMenu1;private System.WinForms.DataGrid grid;public Form1(){//
   // Required for Windows Form Designer support
   //
   InitializeComponent();//
   // TODO: Add any constructor code after InitializeComponent call
   //
   }/// <summary>
   ///    Clean up any resources being used.
   /// </summary>
   public override void Dispose(){base.Dispose();components.Dispose();}
   /// <summary>
   ///    Required method for Designer support - do not modify
   ///    the contents of this method with the code editor.
   /// </summary>
   private void InitializeComponent(){this.components=new
   System.ComponentModel.Container();this.mainMenu1=new System.WinForms.MainMenu()
   ;this.RunTest=new System.WinForms.MenuItem();this.grid=new
   System.WinForms.DataGrid();this.menuItem1=new System.WinForms.MenuItem();
   this.ExitTest=new System.WinForms.MenuItem();grid.BeginInit();
   //@this.TrayHeight = 90;
   //@this.TrayLargeIcon = false;
   //@this.TrayAutoArrange = true;
   //@mainMenu1.SetLocation (new System.Drawing.Point (7, 7));
   mainMenu1.MenuItems.All=new System.WinForms.MenuItem[1]{this.menuItem1};
   RunTest.Text="&Run Test";RunTest.Index=0;RunTest.Click+=new System.EventHandler
   (this.RunTest_Click);grid.Location=new System.Drawing.Point(8,8);grid.Size=new
   System.Drawing.Size(488,456);grid.DataMember="";grid.TabIndex=0;grid.Anchor=
   System.WinForms.AnchorStyles.All;menuItem1.Text="&File";menuItem1.Index=0;
   menuItem1.MenuItems.All=new System.WinForms.MenuItem[2]{this.RunTest,
   this.ExitTest};ExitTest.Text="&Exit";ExitTest.Index=1;ExitTest.Click+=new
   System.EventHandler(this.ExitItem_Click);this.Text="DataSet Example Project";
   this.StartPosition=System.WinForms.FormStartPosition.CenterScreen;
   this.AutoScaleBaseSize=new System.Drawing.Size(5,13);this.Menu=this.mainMenu1;
   this.ClientSize=new System.Drawing.Size(504,469);this.Controls.Add(this.grid);
   grid.EndInit();}protected void RunTest_Click(object sender,System.EventArgs e){
   SQLConnection conn=new SQLConnection("www.adoguy.com","adonet","adonet",
   "ADONET");try{conn.Open();}catch(Exception ex){DumpError(
   "Failed to Open Connection.",ex);return;}// Create Dataset and Command Objects
   DataSet ds=new DataSet();SQLDataSetCommand dscPatients=new SQLDataSetCommand(
   "SELECT * FROM Patients",conn);SQLDataSetCommand dscVisits=new
   SQLDataSetCommand("SELECT * FROM Visits",conn);try{
   // Set Mapping of Patients and fill the dataset with a Table of Patients
   dscPatients.TableMappings.Add("Table","Patients");dscPatients.FillDataSet(ds,
   "Patients");
   // Set Mapping of Visits and fill the dataset with a Table of Visits
   dscVisits.TableMappings.Add("Table","Visits");dscVisits.FillDataSet(ds,"Visits"
   );// Set Relation
   //DataColumn dcPatient = ds.Tables["Patients"].Columns["PatientID"];
   //DataColumn dcVisit = ds.Tables["Visits"].Columns["PatientID"];
   //DataRelation dr = new DataRelation("PatientVisits", dcPatient, dcVisit);
   ds.Relations.Add(ds.Tables["Patients"].Columns["PID"],ds.Tables["Visits"]
   .Columns["PID"]);}catch(Exception ex){DumpError("Failed to Fill Dataset.",ex);
   return;}// Bind to the Grid
   try{DataSetView dsv=new DataSetView(ds);grid.DataSource=dsv;grid.DataMember=
   "Patients";}catch(Exception ex){DumpError("Failed to Bind. ",ex);return;}
   // Close the Connection since the DataSet has cached the data!
   conn.Close();return;}public void DumpError(string sProblem,Exception ex){
   String sMsg=sProblem+" Reason: "+ex.Message;Exception innerEx=ex.InnerException
   ;while(innerEx!=null){sMsg+="\r\nInner->"+innerEx.Message;innerEx=
   innerEx.InnerException;}MessageBox.Show(sMsg);}protected void ExitItem_Click(
   object sender,System.EventArgs e){Application.Exit();}/// <summary>
   /// The main entry point for the application.
   /// </summary>
   public static void Main(string[]args){Application.Run(new Form1());}}}


   /* After Code Formatting */

   namespace DataSetExample
   {
     using System;
     using System.Drawing;
     using System.Collections;
     using System.ComponentModel;
     using System.WinForms;
     using System.Data;
     using System.Data.SQL;

     /// <summary>
     ///    Summary description for Form1.
     /// </summary>
     public class Form1: System.WinForms.Form
     {
       /// <summary>
       ///    Required designer variable.
       /// </summary>
       private System.ComponentModel.Container components;
       private System.WinForms.MenuItem ExitTest;
       private System.WinForms.MenuItem RunTest;
       private System.WinForms.MenuItem menuItem1;
       private System.WinForms.MainMenu mainMenu1;
       private System.WinForms.DataGrid grid;

       public Form1()
       {
         //
         // Required for Windows Form Designer support
         //
         InitializeComponent();

         //
         // TODO: Add any constructor code after InitializeComponent call
         //
       }

       /// <summary>
       ///    Clean up any resources being used.
       /// </summary>
       public override void Dispose()
       {
         base.Dispose();
         components.Dispose();
       }

       /// <summary>
       ///    Required method for Designer support - do not modify
       ///    the contents of this method with the code editor.
       /// </summary>
       private void InitializeComponent()
       {
         this.components = new System.ComponentModel.Container();
         this.mainMenu1 = new System.WinForms.MainMenu();
         this.RunTest = new System.WinForms.MenuItem();
         this.grid = new System.WinForms.DataGrid();
         this.menuItem1 = new System.WinForms.MenuItem();
         this.ExitTest = new System.WinForms.MenuItem();
         grid.BeginInit();
         //@this.TrayHeight = 90;
         //@this.TrayLargeIcon = false;
         //@this.TrayAutoArrange = true;
         //@mainMenu1.SetLocation (new System.Drawing.Point (7, 7));
         mainMenu1.MenuItems.All = new System.WinForms.MenuItem[1]
         {
           this.menuItem1
         };
         RunTest.Text = "&Run Test";
         RunTest.Index = 0;
         RunTest.Click += new System.EventHandler(this.RunTest_Click);
         grid.Location = new System.Drawing.Point(8, 8);
         grid.Size = new System.Drawing.Size(488, 456);
         grid.DataMember = "";
         grid.TabIndex = 0;
         grid.Anchor = System.WinForms.AnchorStyles.All;
         menuItem1.Text = "&File";
         menuItem1.Index = 0;
         menuItem1.MenuItems.All = new System.WinForms.MenuItem[2]
         {
           this.RunTest, this.ExitTest
         };
         ExitTest.Text = "&Exit";
         ExitTest.Index = 1;
         ExitTest.Click += new System.EventHandler(this.ExitItem_Click);
         this.Text = "DataSet Example Project";
         this.StartPosition = System.WinForms.FormStartPosition.CenterScreen;
         this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
         this.Menu = this.mainMenu1;
         this.ClientSize = new System.Drawing.Size(504, 469);
         this.Controls.Add(this.grid);
         grid.EndInit();
       }

       protected void RunTest_Click(object sender, System.EventArgs e)
       {
         SQLConnection conn = new SQLConnection("www.adoguy.com", "adonet",
           "adonet", "ADONET");
         try
         {
           conn.Open();
         }
         catch (Exception ex)
         {
           DumpError("Failed to Open Connection.", ex);
           return ;
         }

         // Create Dataset and Command Objects
         DataSet ds = new DataSet();
         SQLDataSetCommand dscPatients = new SQLDataSetCommand(
           "SELECT * FROM Patients", conn);
         SQLDataSetCommand dscVisits = new SQLDataSetCommand(
           "SELECT * FROM Visits", conn);
         try
         {
           // Set Mapping of Patients and fill the dataset with a Table of Patients
           dscPatients.TableMappings.Add("Table", "Patients");
           dscPatients.FillDataSet(ds, "Patients");
           // Set Mapping of Visits and fill the dataset with a Table of Visits
           dscVisits.TableMappings.Add("Table", "Visits");
           dscVisits.FillDataSet(ds, "Visits");
           // Set Relation
           //DataColumn dcPatient = ds.Tables["Patients"].Columns["PatientID"];
           //DataColumn dcVisit = ds.Tables["Visits"].Columns["PatientID"];
           //DataRelation dr = new DataRelation("PatientVisits", dcPatient, dcVisit);
           ds.Relations.Add(ds.Tables["Patients"].Columns["PID"], ds.Tables[
             "Visits"].Columns["PID"]);

         }
         catch (Exception ex)
         {
           DumpError("Failed to Fill Dataset.", ex);
           return ;
         }

         // Bind to the Grid
         try
         {
           DataSetView dsv = new DataSetView(ds);
           grid.DataSource = dsv;
           grid.DataMember = "Patients";
         }
         catch (Exception ex)
         {
           DumpError("Failed to Bind. ", ex);
           return ;
         }

         // Close the Connection since the DataSet has cached the data!
         conn.Close();
         return ;
       }

       public void DumpError(string sProblem, Exception ex)
       {
         String sMsg = sProblem + " Reason: " + ex.Message;
         Exception innerEx = ex.InnerException;
         while (innerEx != null)
         {
           sMsg += "\r\nInner->" + innerEx.Message;
           innerEx = innerEx.InnerException;
         }
         MessageBox.Show(sMsg);
       }

       protected void ExitItem_Click(object sender, System.EventArgs e)
       {
         Application.Exit();
       }

       /// <summary>
       /// The main entry point for the application.
       /// </summary>
       public static void Main(string[] args)
       {
         Application.Run(new Form1());
       }
     }
   }


Click here to preview the C# Code Formatting Demo 2.

With FormatCode C# Code Formatters, you can format and transform any foreign C# source code to meet your preferred coding style or any common code convention in several seconds!