How Visual C# Helps You Code—Edit - Advanced


Jump to: navigation, search
CSharp-Online.NET:Articles
Visual Studio Articles

How Visual CSharp Helps You Code

© 2006 Peter Wright

Edit -> Advanced

The Advanced submenu is really all about reformatting the code in your code window.


Try It Out: The Edit -> Advanced Menu

On paper many of the features of the Edit -> Advanced menu look pretty dull. It’s only when you see them in action and think about how you might put them to use yourself that their usefulness really shines through. So with that in mind, let’s explore the menu in action.

The best way to do this is to work with a project with quite a lot of code in it. Rather than key this all in, let’s take a look at one of the starter kit apps. Start up a new project in the usual way and choose Movie Collection Starter Kit from the list of project templates, as shown in Figure 7-18.

When C# Express has finished working its magic and the solution is loaded up, double-click the MainForm.cs file; then press F7 to see the code editor view of the form. Your editor will look like mine in Figure 7-19.



Figure 7-18. Choose the Movie Collection Starter Kit to instantly get a project with a fair amount of code inside.



Figure 7-19. Load up the main form and press F7 to see all the code behind it.


The first options you’ll take a look at are the Format options. C# Express’s code editor is pretty good at making your code look good by automatically inserting tabs and indents where they should be, and so on. So, you’re going to have to go out of your way to make the code look a little odd.

First, find the block of code that looks like this:

internal ListDetails ListDetailsPart
{
   get
   {
      //Initialize the variable with a new
      // object instance if nothing exists
      if (this.m_ListDetailsPart == null)
      //creating object
      {
         this.m_ListDetailsPart = new ListDetails();
         //site the control on the host user control and dock fill it
         this.TargetPanel.Controls.Add(this.m_ListDetailsPart);
         this.m_ListDetailsPart.Dock = DockStyle.Fill;
      }
      return this.m_ListDetailsPart;
   }
}

Select this entire block of code by clicking and dragging the mouse over it and press the Shift and Tab keys together a few times to move the whole block up against the left edge of the editor window. Don’t worry if you choose slightly more than the whole block, or slightly less—it doesn’t alter what you’re about to do. When you’re finished, your editor should look like mine in Figure 7-20.

The Format Document and Format Selection items on the Edit -> Advanced menu are designed to rectify just this sort of ugliness, putting the code back the way it should be, according to Microsoft’s code style standards. If you go ahead and choose Format Document from the menu at this point, you’ll see the code jump nicely back into line.

If you really want to give this feature a workout, just press Ctrl+A to select all the text in the text editor, and then force the entire class left in the code editor window before choosing Format Document to put it back the way it should be.

While it was probably pretty obvious just what Format Document and Format Selection were going to do, you may be scratching your head at the Tabify and Untabify options on the menu (see Figure 7-21).



Figure 7-20. Pressing Shift+Tab moves the selected block of text to the left.


Image:VCSExpressfig-7-21.jpg
Figure 7-21. The Tabify and Untabify menu items are a little odd on first sight.


These are most often used when you want to cut and paste between C# Express and another application (usually something to do with the Internet). For example, when I post code to my blog, my blog host doesn’t work very well with tabs in my code. So when I cut and paste nicely formatted code into my blog, it invariably ends up all left-aligned and screwy. Untabify fixes this, replacing any tabs used to format the code with spaces. Tabify, logically, does the opposite.

The next really interesting item on the Edit -> Advanced menu is the Incremental Search item, also accessible by pressing Ctrl+I. This feature has been in Visual Studio for some time, but it’s surprising just how many people have not discovered its usefulness yet.

Incremental Search is a way of “honing in” on something in code, rather than just pressing Ctrl+F to access the Find dialog box, filling it out, and then clicking the Find Next button to move to the next match. It’s absolutely perfect for that inevitable moment when you try to remember just what you named that variable a while ago; was it OnlineSearch or SearchOnline? Scroll the code in the window back up to the very top line, click in that line, and then press Ctrl+I.

The cursor will change to a down arrow with binoculars, like mine in Figure 7-22.


Image:VCSExpressfig-7-22.jpg
Figure 7-22. Starting an incremental search changes the cursor to the incremental search symbol.


Now just type in (slowly, so you can see what happens) the word online.

Notice how as you type, items in the code window are highlighted, showing you the next bit of source code that matches what you have typed so far.We’re looking for a variable declaration here, but the first match that gets highlighted for the word online is a comment, as you can see in Figure 7-23.


Image:VCSExpressfig-7-23.jpg
Figure 7-23. Incremental Search will highlight the first match.Hot keys can be used to move from match to match.


To move to the next match, press Ctrl+I. Do it twice now. Notice how the highlight jumps to the next source line that matches the word online, in this case the variable definition that you were looking for. You can move back to the previous matching line by pressing Shift+Ctrl+I. To leave Incremental Search mode, just click anywhere in the source code window or press the Esc key.

When coding, it’s often handy to stop blocks of code from running. I do this when I want to try out an alternate solution to a problem but don’t really want to lose the solution I already have. The Comment Selection and Uncomment Selection items on the Edit -> Advanced menu make this easy.

Select the block of code inside the ListDetailsPart get statement, as in Figure 7-24



Figure 7-24. Select the block of code inside the first get statement.


Now, either choose Comment Selection from the Edit -> Advanced menu, or press Ctrl+K and then Ctrl+C. The selected block will be commented, as you can see in Figure 7-25.



Figure 7-25. Pressing Ctrl+K and then Ctrl+C comments out the selected code.


If you were to run the project now, that block of code would never execute, because it’s commented.

To put the code back the way it was, just choose Uncomment Selection from the Edit _> Advanced menu, or press Ctrl+K and then Ctrl+U. Personally, I find the shortcut keys a lot quicker to work with than having to find and select a menu item.


Previous_Page_.gif Next_Page_.gif


Personal tools