Aligning And Formatting The C# And ASPX/CSHTML Code Using Visual Studio 2017

Introduction

The Visual Studio Code Editor gives you the ability to format your code with options such as hiding text, displaying URLs, and so forth. The language engine also provides features to auto-format your code as you type through Smart Indenting.

 
Click the below link to get Visual Studio 2017 keyboard shortcuts
Indenting

You can choose three different styles of text indenting. You can also specify how many spaces compose a single indentation or tab, and whether the editor uses tabs or space characters when indenting.

To automatically indent all of your code

  1. In Tools>> Options, select Text Editor >> All Languages >> Tabs, and then select Smart from the Indenting group box.
  2. To select this option on a per-language basis, select Smart in the appropriate folder, starting by selecting Options from the Tools menu. For example, to set Smart formatting for Visual Basic, select Smart from the Indenting group box in Text Editor, Basic, Tabs.

To automatically indent selected code

  1. Select the text you want to automatically indent.
  2. Click Format Selection in Edit, Advanced, or press CTRL+K, CTRL+F.
  3. Format Selection applies the smart indenting rules for the language in which you are programming to the selected text.

Converting Text to Upper and Lower Case

You can use commands to convert text to all upper or all lower case.

To switch text to upper or lower case

  1. Select the text you want to convert.
  2. To convert text to upper case, click CTRL+SHIFT+U, or click Make Uppercase in Edit >> Advanced.
  3. To convert text to lower case, click CTRL+U, or click Make Lowercase in Edit >> Advanced.

Displaying and Linking to URLs

You can create and display active URLs (Uniform Resource Locators) in your code. You can then click the link and be taken to the web page in a browser. By default, the URLs:

  • Appear underlined.
  • Change the mouse pointer to a hand when you move over them.
  • Open the URL site when single-clicked, if the URL is valid.

To display a clickable URL

  1. On the Tools menu, click Options.
  2. Click Text Editor.
  3. To change the option for only one language, click that language and then click General. To change the option for all languages, click All Languages and then click General.
  4. Under Display, select Enable single-click URL navigation.

The case conversion commands do not affect the case of intrinsic commands.

Description

Align the code to make it more understandable to the developer quickly and easily.

Steps To Be Followed


In case of a code-behind file scenario,

I have written some code in c# that is not in proper alignment as shown below.
  1. public static void ErrorLogDetails(string sPathName, string sErrMsg, string sStackTrace, string source, string methodname)  
  2.   
  3.   CreateLogFiles();  
  4.               StreamWriter sw = new StreamWriter(sPathName + sErrorTime + ".txt"true);  
  5.   sw.WriteLine(sLogFormat + sErrMsg);  
  6.   sw.WriteLine(sStackTrace);  
  7.               sw.WriteLine(source);  
  8.   sw.WriteLine(methodname);  
  9.   sw.WriteLine("________________________________________________________________________________________________________________");  
  10.   sw.Flush();  
  11.              sw.Close(); 
 
 
You can find some lines in c# that are not in proper alignment.

Solution

Just select the whole code as above and use the button combination accordingly.

Ctrl + K + F



 
In case of a front-end file scenario

I have written some code in ASPX that is not in proper alignment as shown below.
  1. <div class="footer">  
  2.         <div class="footerdiv1">  
  3.             <table width="100%" border="0" cellspacing="0" cellpadding="2">  
  4.                 <tr>  
  5.                     <td>  
  6.                         © Copyright 2012 <span class="color1">satya</span>. All Rights Reserved.  
  7.                         Application designed and developed by <span class="color1">satya</span>  
  8.                     </td>  
  9.                                   <td align="right">  
  10.                         Best Viewed at 1024 x 768 Resolution  
  11.                     </td>  
  12.                 </tr>  
  13.                       </table>  
  14.               </div>  
  15.     </div> 
 
 
Solution

Just select the whole code as above and use the button combination accordingly.

Ctrl + K + D
 
 
 
NOTE

The same steps used on the ASPX file are applicable to a CSHTML file.

SUMMARY
  1. We learned how to align the text in proper format.
  2. Ctrl + K + D for front end pages like .aspx or .cshtml
  3. Ctrl + K + F for .cs page
  4. Note that you must press all buttons in sequence accordingly..

Up Next
    Ebook Download
    View all
    Learn
    View all