Archive

Archive for August, 2009

Measuring the text to draw

With GDI+, you can easilly measure a text to know the size of drawing.

string text = "Measure string hello world!";

SizeF size = e.Graphics.MeasureString(text, this.Font);
e.Graphics.DrawRectangle(Pens.Green, 10,10, size.Width, size.Height);

e.Graphics.DrawString(text, this.Font, Brushes.Black,10,10);

Here’s the result:
MeasureString

Multiple lines
The measure works with multiple lines of text if the text is delimited with \r\n.

string text = "Measure string hello world!\r\nA second line.";

SizeF size = e.Graphics.MeasureString(text, this.Font);
e.Graphics.DrawRectangle(Pens.Green, 10,10, size.Width, size.Height);

e.Graphics.DrawString(text, this.Font, Brushes.Black,10,10);

Here’s the result:
MeasureString2lines

Render multiple lines of text

Inf GDI+, the Graphics.DrawString method process the \r\n to produce multiples lines.

string text = "Measure string hello world!\r\nA second line.";
e.Graphics.DrawString(text, this.Font, Brushes.Black,10,10);

Here’s the result:
MultipleLines

Visual Studio shortcuts

Clipboard
When there’s a selection:
   Ctrl-X : Cut the selection into the clipboard
   Ctrl-C : Copy the selection into the clipboard
   Ctrl-V : Replace the selection with the clipboard content

When there’s no selection:
   Ctrl-X : Cut the current line into the clipboard (Same as Shift-Del)
   Ctrl-C : Copy the current line into the clipboard
   Ctrl-V : Paste the clipboard into a new line (Same as Shift-Ins)

Undo/Redo
Ctrl-Z : Undo previous edition
Ctrl-Y : Redo the previous edition

Multiple clipboard buffers
You can copy or cut multiples times and use the Ctrl-Shift-V to circle into the clipboard buffer. Visual Studio remember the latest copy/cut.

Example: If you copy TEST1 and after TEST2. When you press Ctrl-Shift-V, TEST2 will be pasted. Press again Ctrl-Shift-V and TEST2 wwill be replaced by TEST1.

Find
Ctrl-F : Show the find dialog
Ctrl-Shift-F : Show the find in files dialog
F3 : Go to the next occurence of the search
Shift-F3 : Go to the previous occurence of the search

Ctrl-F3 : Find the next occurence of the current selection.
Note: If there’s no selection, find the current word

Ctrl-/ : Select the Find box to make a search
Ctrl-I : Start a incremental search. Type and the first correspondance will be showed

Edition
Ctrl-] : Move to the matching brace
Ctrl-K + Ctrl-C : Comment the selection
Ctrl-K + Ctrl-U : Uncomment the section

Ctrl-Enter : Insert a blank line above the cursor
Ctrl-Shift-Enter : Insert a blank line below the cursor

Ctrl-Shift-Spacebar : Display the current parameter tooltips informations

Selection
Shift is used to select.

Shift-Left Arrow : Move the cursor to the left and extend the selection
Shift-Right Arrow : Move the cursor to the right and extend the selection
Ctrl-Shift-Left Arrow : Move the cursor one word to the left and extend the selection
Ctrl-Shift-Right Arrow : Move the cursor one word to the right and extend the selection

Ctrl-Shift-Home : Move the cursor to the beginning of the line adn extend the selection
Ctrl-Shift-End : Move the cursor to the end of the line adn extend the selection

Ctrl-Shift-PageUp : Move the cursor to the previous page and extend the selection
Ctrl-Shift-PageDn : Move the cursor to the next page and extend the selection

Ctrl-Shift-] : Move the cursor to the matching brace and extend the selection
Ctrl-A : Select the entire document

Windows
F7 : Switches from the design view to the code view in the editor
Shift-F7 : Switches from the code view to the design view in the editor

Ctrl-+ : Goes back to the previous location history in the document
Ctrl-Shift-+ : Goes forward in the navigation history in the document

Ctrl-F4 : Close the current document
Ctrl-TAB : Cycle through the opened MDI documents
Ctrl-Shift-TAB : Move to the previous MDI document

Document formatting
Ctrl-E + Ctrl-D : format the entire document
Ctrl-E + Ctrl-F : format the selection

Edition tips
F12 : Go to the defintion. Place your cursor on a word and press F12 to navigate to the definition of the word. You can place your cursor on a method, a class, a variable and F12 will open the file where the word is defined.

Ctrl-. : Show the contextual options of the word where your cursor is on. Try this: Type Car c = new Car();. Place you cursor on the Car and press Ctrl-., select Generate class for Car. Press F12 to go to the newly created class. You can use the same approach to create new methods/properties on a existing class. the Ctrl-. is a greate shortcut that boost your productivity. So play a little with this shortcut!!!

Ctrl-K + Ctrl-S : Show the snippet window allowing to choose a snippet you want to execute.
region example: To insert a #region tag, you select the text you want to be in the region and you press Ctrl-K + Ctrl-S, next you select the region snippet, press enter, type the region name and press enter.

foreach example: You can reduce the among of character you need to type by using the snippets. You can press Ctrl-K + Ctrl-S select foreach and press enter. This will create a foreach statement with the braces. By default, the variable type is var, you can leave it by pressing TAB. TAB go to the next parameter that you can customize. In this example, it place you on the variable name. You can type the name you want and press TAB to enter the collection name in which you want to loop.

Categories: VisualStudio

Debug/Release condtional compilation switch using Condtional Attribute

Do you know the Debug.Write(string message) method? It allow to print a message only when you are in Debug, not in release.

If you look in the MSDN, the method signature is as follow:

[ConditionalAttribute("DEBUG")]
public static void Write(string message)

I realized that the ConditionalAttribute is used to force the inclusion into the compilation only when DEBUG flag is defined.

This approach is really great. You can define a method using this attribute and is will be available only in Debug. You don’t need to bother to remove it of surround it with #if and #endif preprocessor directives. Simpler and quick.

public Class1()
{
   Debug.Write("Test");
}

[ConditionalAttribute("DEBUG")]
public static void WriteLine(string message)
{
   System.Diagnostics.Trace.WriteLine(message);
}

When executed in Debug, a Test trace appear in the output window. In Release, no trace. No need to remove the code, it compile.

If you decompile the release version of the executable, you’ll see that the WriteLine method is defined, but never called. It just take a little more spaces.

Categories: C#

Code snippet

A cool feature in Visual Studio is Code Snippet. It automate redundant code that you need to write again and again.

For example, how many times do you write a for loop like this:

for(int i = 0 ; i < list.Count; ++i) { } [/sourcecode] You can insert a snippet with the keyboard shortcut Ctrl-K + X. This bring a context menu that you need to narrow the search and find the for snippet.

A quicker way is to type ‘for’ and press tab twice, which result in:
for_snippet

You can type your variable name to iter on, default is i. Press tab to go to the end condition and tape what you want.

Categories: VisualStudio

Transparent drawing

In GDI+, you can easilly draw transparent shapes.

One of the Color class constructor take a Alpha value. 0 is completly transparent and 255 is opaque.

            //Fill the background
            Brush brush = new HatchBrush(HatchStyle.DiagonalCross, Color.Black, Color.White);
            e.Graphics.FillRectangle(brush, 10,10, this.ClientRectangle.Width-20, this.ClientRectangle.Height-20);

            //Transparent
            Color transparentBlue = Color.FromArgb(0, Color.LightBlue);
            e.Graphics.FillRectangle(new SolidBrush(transparentBlue), 20, 20, 50, 50);

            //Opaque
            e.Graphics.FillRectangle(Brushes.LightBlue, 120, 120, 50, 50);

Here’s the screenshot of the example:
Transparent

Predefined brushes and pens

When using GDI+, to save the creation of standard pens and brushes, you can use the predefined in the classes Pens and Brushes.

For example, the black brush is Brushes.Black, and the white pen is Pens.White

Here is a example:

e.Graphics.DrawLine(Pens.Black, x, 0, x, this.Size.Height);