главная продукты downloads форум тех. поддержка

 

Главная страница

Windows Forms Tips

FAQ Home

1. How do I write a screen saver?
2. How do I determine the screen resolution?
3. How do I determine the working area of a screen without the systemtray area?
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?
5. How do I change my screen's resolution programatically?
6. Is there an easy way to Create/Delete/Move files in the Windows File System?
7. Can working on WinForms and .Net in general get exciting?
8. I have a long loop. How can I make my application continue to process events while it is executing this loop?
9. How can I display HTML in a Form?
10. The MessageBox always appears in the center of the screen. How can I change it's location?
11. If I have a button with &Next as the text, the N-accelerator key does not appear until I press Alt. Why?
12. How can I add a custom verb to the file and folder context menus in the explorer shell?
13. How do I create windows shortcuts on my desktop programatically in .Net?
14. What mechanisms does .Net provide to convert one type to another?
15. 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?
16. In a Hashtable, why doesn't setting the value for an existing key to be null remove the key from the Hashtable?
17. Why would I be getting a NullReferenceException in Windows Forms with no application code in the call stack?
18. What are the implications of using the Timer class in System.Timers as opposed to the Timer class in System.Windows.Forms?
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?
20. How can I tell if the user has changed some system preference such as the locale or display settings?
21. How do I change a directory name?
22. How do I beep the computer's speaker in a Windows Form application?
23. How do I use the system clipboard?
24. How do I add an application to the Window's Tray?
25. How can I use XP Themes with Windows Forms?
26. How do I use Windows Forms Controls in Internet Explorer?
27. Are event handlers required to have this signature void MyEventHandler(object sender, EventArgs e)?
28. What is the purpose of the [STA Thread] attribute for the Main method of a C# program?
29. I get a message 'DragDrop registration' failed. Why?
 



1 How do I write a screen saver?

Check out this sample on gotnetdot.com.
 



2 How do I determine the screen resolution?

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



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.
 



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)
 



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
 



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

Use File.Create, Delete and Move static methods.
 



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

Yes, only if you work at Syncfusion, Inc.
 



8 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.
 



9 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
     //
     }
 



10 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.
 



11 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".
 



12 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
 



13 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
 



14 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.
 



15 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.
          }
     }
 



16 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.
 



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



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



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



21 How do I change a directory name?

Use the static move member of the Directory class.

     Directory.Move(oldPathString, newPathString);
 



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



23 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"
     }
 



24 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
 



25 How can I use XP Themes with Windows Forms?

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.
 



26 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.
 



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

No. The .NET architecture certainly allows you to define your delegate signatures any way you like. Microsoft chose to used this fixed signature for its handlers because:

1) Ease of calling and defining (simplicity). The more parameters we had, the more a user has to type to create a handler for them. Think about when you need to override a function with a whole bunch of arguments. It's a pain to type out all those parameters when you may not be interested in handling them.
2) Reusability. By keeping the signatures consistent, we increased the likelyhood that we wouldn't need yet another class of handlers for a given type of event -- reuse is good since it keeps namespaces smaller, has need for less documentation, etc.
3) Modifiable values. Many of our *EventArg classes have data in them that can be specified or modified by the event handlers. For example. the KeyEventArgs class has a read/write Handled property that can be set to "true" by handler function if the key should not be passed to the default handler and processing stopped. You can't do this with normal params (unless they're passed byref which just adds a little more complexity.
4) Extensibility. Having a base class allows us to add functionality (we don't have plans for this, but if push came to shove it's an option) without breaking people. Hypothetically, say users were screaming for the new size in the Resize event, which is currently just a plain EventHandler. Well, we could derive a ResizeEventArgs from EventArgs and pass that into the existing handler without breaking anybody's code. Folks that cared about it could check the type, do a cast, and get the extended info.

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



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



29 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());
}

 

e-mail:

 

bousoft@mail.ru