C# Constraints (where)

September 16, 2009 jfblier Leave a comment

Have you ever wanted to use generic definition but with a specific type?

To add a constraint to a generic type definition, use the where.

Let’s say that you want to create a new class MyClassgeneric that use generic, but the types should be of type Item.

public class MyClass where T : Item
{
}

You can also declare a method with the where constraint as follow:

public T Get() where T : Item
{
}

Categories: C#

Measuring the text to draw

August 30, 2009 jfblier Leave a comment

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

August 30, 2009 jfblier Leave a comment

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

August 15, 2009 jfblier Leave a comment

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
Ctral-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 MDI documents opened
Ctrl-Shift-TAB : Move to the previous MDI document

Categories: VisualStudio

Debug/Release condtional compilation switch using Condtional Attribute

August 14, 2009 jfblier Leave a comment

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

August 14, 2009 jfblier Leave a comment

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)
{
}

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

August 5, 2009 jfblier Leave a comment

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

August 5, 2009 jfblier Leave a comment

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);

Rotated text in GDI+, C#

July 12, 2009 jfblier Leave a comment

The code to rotate a text in GDI+ is pretty simple:

private void Form1_Paint(object sender, PaintEventArgs e)
{
   int x = this.ClientRectangle.Width / 2;
   int y = this.ClientRectangle.Height / 2;

   e.Graphics.TranslateTransform(x, y);
   e.Graphics.RotateTransform(270);
   e.Graphics.DrawString("Hello", new Font("Arial", 14), new SolidBrush(Color.Black), 0, 0);
   e.Graphics.ResetTransform();

   e.Graphics.DrawLine(new Pen(Color.Black), x, 0, x, this.Size.Height);
   e.Graphics.DrawLine(new Pen(Color.Black), 0, y, this.Size.Width, y);
}

Here is a screenshot of what it’s gave:

RotatedText

Enumerate object properties at runtime

June 19, 2009 jfblier Leave a comment

This article show how to create a class that can return dynamic properties at runtime, for example, to be consummed by the PropertyGrid of a DataGridView, or any components or controls that use the ICustomTypeDescriptor.

Complete article

Categories: Control, WindowsForm