Windows Forms Tips

18.1 How do I write a screen saver?

Check out this sample on gotnetdot.com.

18.2 How do I determine the screen resolution?

Use the System.Windows.Forms.Screen.PrimaryScreen.Bounds property.

18.3 How do I determine the working area of a screen without the systemtray area?

The Screen.GetWorkingArea(control) will provide you the working area of a screen without the system tray area.

18.4 How do I position my form to the bottom right of the screen when it opens up the first time above the system tray?

Do as follows in your Form's constructor after setting the StartPosition to Manual:

[C#]

this.SetBounds(Screen.GetWorkingArea(this).Width-this.Width

,Screen.GetWorkingArea(this).Height-this.Height , this.Width, this.Height);

[VB.Net]

Me.SetBounds(Screen.GetWorkingArea(Me).Width-Me.Width

,Screen.GetWorkingArea(Me).Height-Me.Height , Me.Width, Me.Height)

18.5 How do I change my screen's resolution programatically?

You can do so by calling the native ChangeDisplaySetting api.

This website at www.omniscium.com provides you an article on how to do it: http://www.omniscium.com/index.asp?page=DotNetScreenResolution

18.6 How do I determine the current date/time?

Use the static properties in the DateTime class to get the current date (Today property) and also in terms of ticks (Ticks property).

18.7 How do I determine the time taken for a long operation that I perform?

You could use the DateTime.Ticks property to record the time taken for a long operation, as follows:

[C#]

private void MyLongOp()

{

long startTicks = DateTime.Now.Ticks;

// Perform a long op:

long endTicks = DateTime.Now.Ticks;

long delta = endTicks - startTicks;

MessageBox.Show("Time taken in Ticks: " + delta.ToString());

delta = delta/(long)10000000; // A tick is 100 nanoseconds

MesaageBox.Show("Time taken in seconds: " + delta.ToString());

}

[VB.Net]

Private Sub MyLongOp()

Dim startTicks As Long = DateTime.Now.Ticks

' Perform a long op:

Dim endTicks As Long = DateTime.Now.Ticks

Dim delta As Long = endTicks - startTicks

MessageBox.Show("Time taken in Ticks: " + delta.ToString())

delta = delta/(Long)10000000 ' A tick is 100 nanoseconds

MesaageBox.Show("Time taken in seconds: " + delta.ToString())

End Sub

18.8 What are the implications of using the Timer class in System.Timers as opposed to the Timer class in System.Windows.Forms?

Be careful when useing the Timer from System.Timers from within a Windows.Form application. Most methods in Windows Forms are not thread safe and can produce indeterminate results when called from another thread. In this case, System.Timers.Timer is much higher resolution and less affected by how busy your application is (since it doesn't use the message pump) but it's Tick event is fired from another thread. In order to be sure this works correctly, you'll have to write the proper Control.Invoke code to marshal over to the thread the control was created on (the main thread) before you call methods on the control. So, in cases where you don't need super-high-resolution and regular events to be fired, you're much better off using System.Windows.Forms.Timer. It's messsage pump is based (WM_TIMER) and will fire on the UI thread.

(from sburke_online@microsoft..nospam..com on microsoft.public.dotnet.framework.windowsforms)

18.9 Is there an easy way to Create/Delete/Move files in the Windows File System?

Use File.Create, Delete and Move static methods.

18.10 How can I use XP Themes with Windows Forms using the .NET FrameWork 1.1?

The .manifest file is not required if you are using .NET FrameWork 1.1. You can now use the application's EnableVisualStyles() method which should called before creating any controls. You also need to ensure that the FlatStyle property of the control is changed to System/b> instead of the default value of Standard.

[C#]

static void Main()

{

Application.EnableVisualStyles();

Application.Run(new Form1());

}

[VB.Net]

Public Shared Sub Main()

System.Windows.Forms.Application.EnableVisualStyles()

System.Windows.Forms.Application.Run(New Form1)

End Sub

18.11 How can I use XP Themes with Windows Forms using the .NET FrameWork 1.0?

Follow these steps to introduce Windows XP visual styles into your Windows application.

1. Create a new Windows Application and add some controls to the default form.
2. For every control you place on the form that has a FlatStyle property, set the property to System.
3. Compile your application.
4. Build a manifest file in the application directory. NOTE: This manifest file must be located in the same directory as the executable. Open Notepad and place the code shown below in the file.

.NET control deployment tool

type="win32"

name="Microsoft.Windows.Common-Controls"

version="6.0.0.0"

processorArchitecture="X86"

publicKeyToken="6595b64144ccf1df"

language="*"

/>

Save the file in the application directory with the name of the application and an extension of exe.manifest. For instance, if your application name is "MyApp", you will name the manifest file as MyApp.exe.manifest.
5. When your application runs, Windows XP looks at the application manifest which tells the operating system to use common controls library version 6.0.

This process is discussed and a sample given in an article by the David C. Brown on the Windows Forms team at gotnetdot.com.

18.12 How do I use Windows Forms Controls in Internet Explorer?

Erick Ellis steps you through creating a Windows Form and then adding an object tag to an HTML page in his article Using Windows Forms Controls in IE on gotnetdot.com.

18.13 Are event handlers required to have this signature void MyEventHandler(object sender, EventArgs e)?

C#

18.14 How do I change a directory name?

Use the static move member of the Directory class.

Directory.Move(oldPathString, newPathString);

18.15 How do I beep the computer's speaker in a Windows Form application?

There is no Windows Form function to beep your computer's speaker. But you can just invoke the Win32 API MessageBeep.

using System.Runtime.InteropServices;

...

[DllImport("user32.dll")]

public static extern int MessageBeep(uint n);

private void button2_Click(object sender, System.EventArgs e)

{

MessageBeep(0x0);

}

Another method (suggested by msauper@sauper.com on microsoft.public.dotnet.framework.windowsforms)

Reference the VB.NET runtime support and just use the Beep() method.

The method is in:

Microsoft.Visual Basic.NET Runtime

The method is:

Microsoft.VisualBasic.Interaction.Beep();

18.16 How do I use the system clipboard?

The SetDataObject and GetDataObject methods in the Clipboard class found in the System.Windows.Forms namespace allows you to access the clipboard. Here is some code.

string text = "Some text for the clipboard";

Clipboard.SetDataObject(text); //clipboard now has "Some text for the clipboard"

text = ""; //zap text so it can be reset...

IDataObject data = Clipboard.GetDataObject();

if(data.GetDataPresent(DataFormats.Text))

{

text = (String)data.GetData(DataFormats.Text);

//text is now back to "Some text for the clipboard"

}

18.17 How do I add an application to the Window's Tray?

In design mode, you drop a NotifyIcon object on your form. You can then drop a ContextMenu on your form and add this menu to the NotifyIcon's ContextMenu property. This menu will be seen when the user rightclicks the try icon. You can add a handler for the NotifyIcon's Click event to catch the action of a user clicking the icon.

From code, you create an instance of NotifyIcon, set properties, hook any handlers you want, and then make it visible. Here are some VB snippets.

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

Dim trayItem As NotifyIcon = New NotifyIcon()

trayItem.Icon = SystemIcons.Question

trayItem.Visible = True

AddHandler trayItem.Click, New EventHandler(AddressOf HandleTrayClick)

End Sub

Private Sub HandleTrayClick(ByVal sender As Object, ByVal e As EventArgs)

MessageBox.Show("Tray item clicked")

End Sub

18.18 Why would I be getting a NullReferenceException in Windows Forms with no application code in the call stack?

The CLR is catching an Access Violation that's being thrown from unmanaged code, and that is propogating up as a NullReferenceException. I've seen this happen with certain common control library windows types if an application such as spy++ is running, and I see this is the TreeView control that is having troubles with a mouse down. Have you done any modification to the control through P/Invoke methods?

(from sburke_online@microsoft..nospam..com on microsoft.public.dotnet.framework.windowsforms)

18.19 When I try to catch the Resize event to handle sizing of MDI children, the event is called even before my form's constructor can create the children throwing an exception. How do I avoid this?

The Resize event is getting raised before the constructor completes because the form is being resized in the constructor. If you are using VS.NET to create your project, then in your constructor there is a call to InitializeComponent. In this method there is code to resize the form. You could instantiate your child form in your constructor *before* InitializeComponent is called, then when the form gets resized for the first time you already have an instance of your child form.

(broderonline@microsoft.com_(Brian_Roder))

18.20 How can I tell if the user has changed some system preference such as the locale or display settings?

Use the static events in the SystemEvents class found in the Microsoft.Win32 namespace. There are many events in this class. Here are a couple:

SystemEvents.DisplaySettingsChanged += new System.EventHandler(displaySettingsChanged);

SystemEvents.UserPreferenceChanged +=

new UserPreferenceChangedEventHandler(userPreferencesChanged);

.............

.............

private void displaySettingsChanged(object sender, EventArgs e)

{

MessageBox.Show(e.ToString());

}

private void userPreferencesChanged(object sender, UserPreferenceChangedEventArgs e)

{

switch(e.Category)

{

case UserPreferenceCategory.Locale:

MessageBox.Show("Changed locale");

break;

default:

MessageBox.Show(e.Category.ToString());

break;

}

}

18.21 What mechanisms does .Net provide to convert one type to another?

Type conversion is usually possible in one of the following ways:

1) Using explicit methods in source type like:

public class SizeF

{

...

public Size ToSize();

}

There is usually a corresponding explicit or implicit operator that does the same conversion. The operators are however not available in VB.Net.

2) Using FromXXX static methods exposed in the destination type:

class Image

{

...

public static Image FromStream(Stream);

}

3) Using implicit or explicit operators defined in the source or destination type. This will allow you to perform implicit or explicit casts from the source type to the destination type.

SizeF sizeF = size; // Possible because type Size has an implicit type conversion operator that converts a Size to SizeF

PointF pointF = (PointF)sizeF; // Possible because type SizeF has an explict type conversion operator that converts a SizeF to PointF.

There is usually a corresponding ToXXX method that does the same conversion. You can use either this or the ToXXX methods in C#.

4) Using TypeConverters. Some types come with TypeConverters that allow you to convert to or convert from another type. This conversion is usually not documented and can be discovered only by writing some test code.

For example, the System.Drawing.Icon type's TypeConverter converts the Icon into a byte[]. You can use this functionality in your code as follows:

TypeConverter tc = TypeDescriptor.GetConverter(typeof(System.Drawing.Icon));

byte[] blob = tc.ConvertTo(myIcon, typeof(byte[]));

It's usually time consuming to figure out whether a TypeConverter's ConvertTo or ConvertFrom method can perform the necessary conversion.

The attached TypeConverterLookup tool lets you figure that out easily on any type declared in any assembly available in the GAC or available in the same directory as the tool's exe. If you have types in custom assemblies, then just copy over that assembly to the same directory as the tool, you can then specify the type in the tool.

18.22 Its possible that some of my class members will not be initialized when one of my virtual members gets called even before the constructor. How can I avoid this?

If possible, initialize them in-line:

public class MyClass

{

public ArrayList myArrayList = new ArrayList();

public MyClass(){}

public override void MyOverride()

{

// When this gets called by the base class construcotr, myArrayList will be initialized.

}

}

18.23 In a Hashtable, why doesn't setting the value for an existing key to be null remove the key from the Hashtable?

This is by design. You have to use Remove to actually remove the Key/Value pair from the Hashtable.

18.24 How do I create windows shortcuts on my desktop programatically in .Net?

The following article explains how shortcuts can be created programatically: Creating and Modifying Shortcuts

18.25 How can I add a custom verb to the file and folder context menus in the explorer shell?

The article at www.vbaccelerator.com shows you how it is done in .net: Associating Applications with Any File or Folder

18.26 Can working on WinForms and .Net in general get exciting?

Yes, only if you work at Syncfusion, Inc.

18.27 I have a long loop. How can I make my application continue to process events while it is executing this loop?

Call the static Application.DoEvents() method in your loop.

18.28 How can I display HTML in a Form?

You can add a web browser to a form in a straight forward manner.
1) Right-click your toolbox and select Customize Toolbox. Then add the Com Component 'Microsoft Web Browser'.
2) Drag the just added Explorer tool and drop it on your form to position it where you want it.
3) Set the inital display page using code such as:

public Form1()

{

//

// Required for Windows Form Designer support

//

InitializeComponent();

object a = 0;

object b = 0;

object c = 0;

object d = 0;

axWebBrowser1.Navigate("www.syncfusion.com", ref a, ref b, ref c, ref d);

//

// TODO: Add any constructor code after InitializeComponent call

//

}

18.29 The MessageBox always appears in the center of the screen. How can I change it's location?

Its looks like you cannot change this behavior of MessageBox. One solution is to derive your own MessageForm class from Form to display your message. Then call its ShowDialog method to show it after you set its Size, Location and StartPosition properties. (If you don't set the StartPosition , then the Location is ignored.) One of the values for StartPosition is CenterParent which would center your new MessageForm.

18.30 If I have a button with &Next as the text, the N-accelerator key does not appear until I press Alt. Why?

This behavior has nothing to do with .Net and is expected Windows behavior beginning with Win2000. Windows will only show accelerator keys after the alt is pressed. There is a control panel setting that you can use to change this behavior. Look on the Effects tab under Control Panel|Display to see a checkbox labeled "Hide keyboard navigation indicators until I use the Alt key".

18.31 How can I shut down/restart the OS from my Windows Forms Application?

You could do this using the WMI Classes in the .NET Framework or use WindowsController available at http://www.mentalis.org

18.32 I get a message 'DragDrop registration' failed. Why?

Try placing the attribute [STA Thread] on your Main method. OLE D&D requires single threaded apartments.

[STAThread]

static void Main()

{

Application.Run(new Form1());

}

18.33 What is the purpose of the [STA Thread] attribute for the Main method of a C# program?

That marks the thread as being "Single Thread Apartment" which means any multiple threaded calls need to be marshaled over to that thread before they are called. That's there because Windows Forms uses some OLE calls (Clipboard for example), which must be made from the thread that initialized OLE.

(from sburke_online@microsoft..nospam..com on microsoft.public.dotnet.framework.windowsforms)

18.34 I add my application to the Window's Tray, set ShowInTaskBar to false, but the program still appears in the Alt+Tab list.

An easy way to keep the program from appearing in the Alt+Tab list is to set the Form's FormBorderStyle property to be a ToolWindow (fixed or sizable).

One caveat to this is that if you display another Form (that is not a ToolWindow) through a call in your application, the main Form will again appear in the Alt+Tab listing. To get around this, you can make all of the Forms in your Tray application have a FormBorderStyle of ToolWindow.

No comments: