Where is PaddingF? And Other Padding Oddities

1 Comment »

There is No PaddingF

There is Point and PointF, Size and SizeF, Rectangle and RectangleF, Padding and… wait, there is no PaddingF!

System.Drawing vs. System.Windows.Forms

The 2-D drawing functions in the System.Drawing namespace accept both integer and floating point measurements.  That’s why the main 2-D drawing structures have both int and float versions such as Point and PointF, respectively.

However, the Padding structure is defined separately in the System.Windows.Forms namespace where most 2-D measurements are in integers.  Hence, there is an integer Padding structure but no floating point PaddingF.

Read the rest of this entry »

Display Icons in a ListBox

No Comments »

The standard Windows Forms ListBox control is not designed to display an icon with each item.  You can modify the ListBox to handle the DrawItem event and manually draw the items and their associated icons, as shown here and here

However, if you were hoping to use a standard off-the-shelf control with full icon support, your best bet is to use the ListView control.

C# TextBox Scroll to Cursor

No Comments »

To scroll a C# TextBox to the cursor/caret, it’s important that the TextBox is both visible and focused, then call the ScrollToCaret method:

textBox.Focus();
textBox.ScrollToCaret();

To scroll to the bottom/end of a TextBox, set the SelectionLength to 0 to remove any selection, then set SelectionStart to the end of the text in the TextBox:

textBox.SelectionLength = 0;
textBox.SelectionStart = textBox.Text.Length;
textBox.Focus();
textBox.ScrollToCaret();

Type Name “UITypeEditor” Not Found

1 Comment »

This is one of those “D’oh!” moments.  You’re creating your own UITypeEditor.  You know the UITypeEditor class is located in the System.Drawing.Design namespace.  So naturally you want to add to your Visual Studio project a reference to the System.Drawing.Design.dll, right?  Wrong!  When you compile your project, the following error may appear:

The type or namespace name ‘UITypeEditor’ could not be found (are you missing a using directive or an assembly reference?)

It turns out that UITypeEditor is actually defined in System.Drawing.dll, even though it’s located in the System.Drawing.Design namespace.  See the disconnect?  But you can easily solve this problem by adding to your Visual Studio project a reference to System.Drawing.dll

Add Drop Shadow to Borderless Form

8 Comments »

When you create a Form with a border, Windows automatically draws a drop shadow around the form, as shown here:

Border form with shadow

However, if you set the form’s FormBorderStyle property to None, Windows draws neither the form border nor the drop shadow, as shown here:

Borderless form with no shadow

Read the rest of this entry »

Show ToolTip on TabPage in TabControl

3 Comments »

So you’ve set the ToolTipText property of a TabPage in a TabControl.  When the user moves the mouse pointer over the tab, the text you specified is supposed to show in a tooltip. 

TabPage ToolTip

But what if the tooltip is not showing?  Fortunately, this problem has an easy solution:

Set the ShowToolTips property in the TabControl to true.

C# Focus TextBox on Form Load

3 Comments »

When showing a form that contains a TextBox, it’s common courtesy to focus the TextBox so that the user can begin typing immediately.

To focus a TextBox when a Windows Form first loads, simply set the TabIndex for the TextBox to zero (or the lowest TabIndex for any Control on the Form).

When a Form is displayed, it automatically focuses the Control with the lowest TabIndex.  Note that if your TextBox is pre-initialized with some text, then the entire Text will be selected, as shown below:

Entire text is selected

Read the rest of this entry »

Add Shaded Rows to ListView Details View

9 Comments »

Sometimes it can be challenging to read the Details view in a ListView, especially if the rows are long.  This article shows how to add shading to every second row to make a ListView easier to read.

ListView Shaded Rows

Read the rest of this entry »

Convert Handle to Form, and Vice Versa

3 Comments »

This is one of those things that’s obvious once you know it.  When dealing with COM, there is often a need to convert a WinForms Form object to an IntPtr handle, and vice versa.

Read the rest of this entry »