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 »

Set InitialDirectory for FolderBrowserDialog

2 Comments »

The .NET FolderBrowserDialog class does not have an InitialDirectory property like the OpenFileDialog class.  But fortunately, it’s quite easy to set an initial folder in the FolderBrowserDialog:

FolderBrowserDialog dialog = new FolderBrowserDialog();
dialog.RootFolder = Environment.SpecialFolder.Desktop;
dialog.SelectedPath = @"C:Program Files";
if (dialog.ShowDialog() == DialogResult.OK)
{
    Console.WriteLine( dialog.SelectedPath );
}

Note that you can choose other RootFolder’s like MyComputer and MyDocuments.  The SelectedPath must be a child of the RootFolder for this to work.

Given the sample code above, the dialog may look like this:

C# WinForms Form Event Order

9 Comments »

Sometimes it’s important to understand the order of events that occur when a WinForms Form is opened, closed, shown or hidden.  There are also a few “gotchas” that are important to know.

Read the rest of this entry »

The Proper Way to Show the Wait Cursor

22 Comments »

It’s common UI courtesy to show the Wait cursor when performing a long operation that requires the user to wait.  Here is how the Wait cursor appears in Windows Vista:

Wait cursor

But developers often go about this the wrong way by setting the Cursor.Current property as follows:

Cursor.Current = Cursors.WaitCursor;

Read the rest of this entry »

Close All Forms in an Application in a Thread-Safe Manner

26 Comments »

Closing all forms in an application seems like it would be a simple task of using a foreach loop in the Application.OpenForms collection, such as:

foreach (Form form in Application.OpenForms)
{
    form.Close();
}

But there are two problems.

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.

DataGridView HideSelection to Hide Selection when Grid Loses Focus

3 Comments »

The DataGridView is a powerful grid control included in the .NET Framework.  One function missing, however, is the ability to hide the current selection when the DataGridView control is not focused.  What the DataGridView class needs is a HideSelection property, similar to the ListView and TextBox.  But the .NET designers have not included this capability in the DataGridView class.

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 »

Show Continuous Progress with .NET ProgressBar and MarqueeAnimationSpeed

19 Comments »

For some operations such as logging on to a web site or downloading a web page, you may not know how long it will take the operation to finish.  So instead of showing a progress bar with a specified percent complete, you can set the .NET ProgressBar to cycle continuously.

ProgressBarMarquee

Read the rest of this entry »

Manipulating Controls Across Threads

11 Comments »

Methods that affect a Windows Forms control can be executed only on the thread that created the control. .NET does not permit directly manipulating controls across threads. The Visual Studio compiler under .NET 2.0 will mark these attempts as errors. .NET 1.1 will allow them, but these will often result in unexpected behavior like incorrectly-painted controls.

Read the rest of this entry »

keep looking »