WinForms FAQ - Tips

I want to UUENCODE and decode data? Does the .NET framework have support for this?

No, but there is some code available at the link below that you can use for this purpose:

You can also download the source files from here.

Permalink
What is the function of TypeConverters?

Type converters let you convert one type to another type. Each type that you declare can optionally have a TypeConverter associated with it using the TypeConverterAttribute. If you do not specify one the class will inherit a TypeConverter from its base class.

The following methods deal with the type conversion work that is performed by TypeConverters.

// for conversion from another type to the type that this type converter is associated with
public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType);
public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value);

// for conversion of this type to some other type
public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType);
public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType);

The above functions provide the core of TypeConverters. One reason for the confusion surrounding TypeConverters is the fact that they have other functions that are unrelated to the primary type conversion function. Methods such as public PropertyDescriptorCollection GetProperties(ITypeDescriptorContext context, object value) do not perform type conversion. They are present for another purpose.

Typically when we require access to the properties, events and methods we use a TypeDescriptor. However there are .NET framework elements such as the PropertyGrid that first access the TypeConverter and then query it for a list of Properties (it returns true when public bool GetPropertiesSupported(); is called). If the TypeConverter returns false from public bool GetPropertiesSupported(), then the TypeDescriptor is queried for such information. Deriving a type converter is an easy way to present properties, events etc in any manner that you want especially with the property grid. In your own code also when you access Property information it is good practice to query the TypeConverter first. So, presentation of object properties, methods and events is also a function of the TypeConverter. Unfortunately, the name ’TypeConverter’ does not hint at this function.

Enclosed is a small sample that illustrates the use of a custom Type Converter for conversion. Download TypeConverter.zip. Try the sample and it will be clear what Type Converters primarily do.

For more information on TypeDescriptors please refer to https://www.syncfusion.com/content/en-us/faq/windowsforms/search/705.aspx

Permalink
How can I persist a collection of items into code

The CollectionEditor allows adding and removing items from a collection at design time. If the items in this collection implement IComponent or if they are derived from Component, the items in your collection can be persisted in code.

Download collectioneditorsample.zip for a complete sample project.

Here are some steps you should follow:

1) Make sure your item is derived from Component or implements Icomponent.
For example:

  public class SomeItem : Component
  {
    private string label = '''';

    public SomeItem()
    {
    }

    public SomeItem(string label)
    {
      this.label = label;
    }

    public string Label 
    {
      get 
      { 
        return label; 
      }
      set 
      { 
        label = value; 
      }
    }

    public override string ToString()
    {
      return String.Format(''SomeItem: ( Label = ’{0}’ )'', label);
    }
  }

2) Next implement your Collection. You have to implement the Ilist interface. The CollectionEditor will determine the type of instances to be added to your collection using reflection inspecting the return type of the Item property (Indexer).

    [
    Description(''The set of properties to be edited with CollectionEditor.''), 
    DesignerSerializationVisibility(DesignerSerializationVisibility.Content), 
    Editor(typeof(System.ComponentModel.Design.CollectionEditor), 
      typeof(System.Drawing.Design.UITypeEditor))
    ]
    public SomeItemCollection SomeItems 
    {
      get 
      {
        if (someItemCollection == null) 
        {
          someItemCollection = CreateItemCollection();
        }
        return someItemCollection;
      }
    }

    protected SomeItemCollection CreateItemCollection()
    {
      return new SomeItemCollection(this);
    }

    public class SomeItemCollection : IList
    {
      // Fields
      private SomeItemDisplayer owner;
      public event EventHandler Changed;
            
      // Constructors
      public SomeItemCollection(SomeItemDisplayer owner)
      {
        this.owner = owner;
      } 

      internal ArrayList InnerArray 
      { 
        get
        {
          return owner.someItems;
        } 
      }
      
      public void OnChanged()
      {
        if (this.Changed != null)
          this.Changed(this, EventArgs.Empty);
      }
            
      /// <summary>
      /// The CollectionEditor will determine the type of objects to be created by
      /// looking at the property type of the following method. CollectionEditor
      /// internally uses reflection to get the PropertyInfo for the ''Item'' property. 
      /// This method must be public.
      /// </summary>
      public SomeItem this[int index] 
      { 
        set
        {
          if (value == null) 
            throw new ArgumentNullException(''value'');

          if (index < 0 || index >= this.InnerArray.Count) 
            throw new ArgumentOutOfRangeException(String.Format(''Invalid Argument {0}: {1}'', ''index'', index.ToString()));

          this.InnerArray[index] = value;
          OnChanged();
        } 
        get
        {
          if (index < 0 || index >= this.InnerArray.Count)
            throw new ArgumentOutOfRangeException(''Invalid Argument {0}: {1}'', ''index'', index.ToString());
          return (SomeItem) this.InnerArray[index];
        } 
      }

      public void AddRange(object[] items)
      {
        InnerArray.AddRange(items);
        OnChanged();
      }
            
      /// <summary>
      /// This implementation for the Item property for the IList interface. It’s
      /// property type is object.
      /// </summary>
      object IList.this[int index] 
      { 
        set
        {
          // forward call to public indexer 
          this[index] = (SomeItem) value;
        } 
        get
        {
          // forward call to public indexer
          return this[index];
        } 
      }

      public /*IEnumerable*/ IEnumerator GetEnumerator()
      {
        return InnerArray.GetEnumerator();
      } 
            
            
      public /*ICollection*/ int Count 
      { 
        get
        {
          return InnerArray.Count;
        } 
      }
            
      public /*IList*/ void RemoveAt(int index)
      {
        if (index < 0 || index >= this.InnerArray.Count) 
          throw new ArgumentOutOfRangeException(String.Format(''Invalid Argument {0}: {1}'', ''index'', index.ToString()));

        this.InnerArray.RemoveAt(index);
        OnChanged();
      } 
            
            
      public /*IList*/ void Remove(object value)
      {
        int n = this.InnerArray.IndexOf(value,0);
        if (n != -1) 
          this.RemoveAt(n);
      } 
            
            
      public /*IList*/ void Insert(int index, object item)
      {
        if (item == null) 
          throw new ArgumentNullException(''item'');
        if (index < 0 || index > this.InnerArray.Count)
          throw new ArgumentOutOfRangeException(String.Format(''Invalid Argument {0}: {1}'',''index'', index.ToString()));

        this.InnerArray.Insert(index,item);
        OnChanged();
      } 
            
            
      public /*IList*/ int IndexOf(object value)
      {
        if (value == null)
          throw new ArgumentNullException(String.Format(''Invalid Argument {0}: {1}'',''value'', ''null''));
        return this.InnerArray.IndexOf(value,0);
      } 
            
            
      public /*IList*/ bool IsReadOnly 
      { 
        get
        {
          return false;
        } 
      }
            
      public /*IList*/ void Clear()
      {
        InnerArray.Clear();
        this.owner.Invalidate();
      } 
            
            
      public /*IList*/ bool Contains(object value)
      {
        return this.IndexOf(value) != -1;
      } 
            
      void System.Collections.ICollection.CopyTo(Array dest, int index)
      {
        int count = this.InnerArray.Count;
        for (int n1 = 0; n1 < count; n1++)
          dest.SetValue(this.InnerArray[n1], (n1 + index));
      } 
            
            
      int System.Collections.IList.Add(object item)
      {
        int n = this.InnerArray.Add(item);
        OnChanged();
        return n;
      } 
            
            
      bool System.Collections.IList.IsFixedSize
      {
        get 
        {
          return false;
        } 
      } 
            
            
      bool System.Collections.ICollection.IsSynchronized
      {
        get 
        { 
          return false;
        } 
      } 
            
            
      object System.Collections.ICollection.SyncRoot
      {
        get
        {
          return this;
        } 
      } 
            
    }

3) Reference this collection in your control or component that should be designable. You need to supply a DesignerSerializationVisibility and an Editor attribute:

    private SomeItemCollection someItemCollection = null;
    ArrayList someItems = new ArrayList();
    
    [
    Description(''The set of properties to be edited with CollectionEditor.''), 
    DesignerSerializationVisibility(DesignerSerializationVisibility.Content), 
    Editor(typeof(System.ComponentModel.Design.CollectionEditor), 
      typeof(System.Drawing.Design.UITypeEditor))
    ]
    public SomeItemCollection SomeItems 
    {
      get 
      {
        if (someItemCollection == null) 
        {
          someItemCollection = CreateItemCollection();
        }
        return someItemCollection;
      }
    }

    protected SomeItemCollection CreateItemCollection()
    {
      return new SomeItemCollection(this);
    }
Permalink
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 explicit 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.

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

Use File.Create, Delete and Move static methods.

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

<?xml version='1.0' encoding='UTF-8' standalone='yes'?>
   <assembly xmlns='urn:schemas-microsoft-com:asm.v1' manifestVersion='1.0'>
   <description>.NET control deployment tool</description>
   <dependency>
          <dependentAssembly>
                   <assemblyIdentity
                           type='win32'
                           name='Microsoft.Windows.Common-Controls'
                           version='6.0.0.0'
                           processorArchitecture='X86'
                           publicKeyToken='6595b64144ccf1df'
                           language='*'
                   />
           </dependentAssembly>
   </dependency>
   </assembly>

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

Permalink
How do I send email using .NET

There is a nice and free SMTP component available from Quiksoft. You can read more on this here.

Also, check out this article on Codeproject:

SMTP article on Codeproject

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

Permalink
How do I add a tooltip to my Button control

From within the designer:
1) From the ToolBox, drop a ToolTip control to your form.
2) Check the properties of this toolTip1 object to make sure Active is set to true.
3) Click on your button, and in its Property page, set the ToolTip on toolTip1 entry.

From code:

	private System.Windows.Forms.ToolTip toolTip1;
	......
	......
	this.toolTip1 = new System.Windows.Forms.ToolTip(this.components);
	this.toolTip1.Active = true;
	......
	......
	this.toolTip1.SetToolTip(this.button1, 'Press for information...');
Permalink
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 using 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 message pump is based (WM_TIMER) and will fire on the UI thread.

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

Permalink
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 initial 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
	//
	}
Permalink
What is the function of TypeDescriptor?

Here is an MSDN article that discuss about TypeDescriptor and its capabilities,

Type Descriptor Overview in msdn

Permalink
How can I instruct the compiler to use the Main method in Class2 instead of Class1?

In the project’s General Properties, specify Class2 as the ‘Startup Object’. The dropdown contains all classes with a Main method specified.

Permalink
How do I provide Intellisense support to my controls while coding against them and also provide Description support for the properties in the property grid?

You can provide Intellisense support to your type and it’s members by providing xml comments in code as follows:

/// <summary>
/// Summary description for Form3.
/// </summary>
public class Form3 : System.Windows.Forms.Form
{
    /// <summary>
    /// Clean up any resources being used.
    /// </summary>
    protected override void Dispose( bool disposing )
    {
    }
    /// <summary>
    /// Summary of my property
    /// </summary>
    public bool MyProperty
    {
        get{..}
        set{..}
    }       
}

Search for the ‘Tags for Documentation Comments’ topic in MSDN for all the available documentation tags.

Then in your project, go to the Project Properties dialog, to the Configuration Properties/Build tab and specify a file for the XML Documentation File property. This will generate a file by that name when you compile your assembly. Place this xml file beside your dll. This will provide Intellisense support for your types in that assembly.

To provide Description support for your properties in the property grid in the designer, add the DescriptionAttribute attribute to your properties in code.

///
/// Summary of my property
/// 
[Description('description of the property')]
public bool MyProperty
{
    get{..}
    set{..}
}
Permalink
What are the options with using Visual Studio.NET from the command line

Type devenv.exe /? to get a full list of options.

Permalink
Is there any easy way to convert VB.NET code to C#?

If you add a BMP file to your solution using the File|Add Existing Item… Menu Item, then change the Build Action property of this BMP file to Embedded Resource, you can then access this resource with code similar to:

	
	// WindowsApplication6 corresponds to Default Namespace in your project settings.
	// subfolders should be the folder names if any, inside which the bmp is added. If the bmp was added to the top level, you don’t have to specify anything here.
	string bmpName = 'WindowsApplication6.subfolders.sync.bmp';  
	System.IO.Stream strm = null;
	try
	{
		strm = this.GetType().Assembly.GetManifestResourceStream(bmpName);
		pictureBox1.Image = new Bitmap(strm);
		// Do the same for Icons 
		// Icon icon1 = new Icon(strm);
	}
	catch(Exception e)
	{
		MessageBox.Show(e.Message);
	}
	finally
	{
		if(strm != null)
			strm.Close();
	}
Permalink
In Visual Studio, all my docking windows are out of whack. How can I reset them so things will be docked properly

You can use the menu item Tools|Options, and then select Reset Window Layout.

A second alternative is to close Visual Studio, and then delete the suo file in your project folder. (suo=Studio User Options)

Permalink
How to Reinitialize extender provider dependencies (the logic within CanExtend) from within the designer

Parse through all the Controls in the designer and call TypeDescriptor.Refresh() on them.

// From inside your custom IDesigner implementation: 
private void UpdateExtendedProperties()
{
    IDesignerHost idh = this.GetService(typeof(IDesignerHost)) as IDesignerHost;
    foreach (Component comp in idh.Container.Components)
    {
        // Ignoring the Form
        if ((comp is Control) && ((comp is Form) == false))
        {
            Control ctrl = comp as Control;
            TypeDescriptor.Refresh(ctrl);
        }
    }
}
Permalink
How to serialize properties of items that do not implement IComponent in a collection?

You have to set DesignerSerializationVisibility attribute on the property whose type is a strongly-typed collection to Content. You have to ensure the collection is created at startup or on demand. In order to support serialization of items that do not implement IComponent into your code you have to write a TypeConverter for that class that can convert to InstanceDescriptor. See the documentation on InstanceDescriptor for an example.

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

Permalink
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 constructor, myArrayList will be initialized.
		}
	
	}
Permalink
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
			MessageBox.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
			MessageBox.Show('Time taken in seconds: ' + delta.ToString())
		End Sub
Permalink
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.

Permalink
How can I change the Word Wrap Property of VS.NET IDE?

You need to go to
Tools->Options->Text Editor->All Languages->General->Word Wrap and check/uncheck the checkbox or you could
press Ctrl+R Ctrl+R to toggle this property.

Permalink
Can I drag and drop code snippets using the VS.NET IDE?

Yes, in Code View you can select and drag code to aTab (General Tab) in the ToolBox and then you can drag the code from the Tab to the desired location.

Permalink
How can I word-wrap the Tooltip that is displayed?

Rhett Gong posted a work around using Reflection to achieve this in the microsoft.public.dotnet.framework.windowsforms.controls Newsgroup:

[DllImport('user32.dll')]
private extern static int SendMessage(IntPtr hwnd,uint msg, int wParam, int lParam);

.....
object o = typeof(ToolTip).InvokeMember('Handle',BindingFlags.NonPublic|BindingFlags.Instance|BindingFlags.GetProperty,null,myToolTip,null);
IntPtr hwnd = (IntPtr) o;
SendMessage(hwnd,0x0418, 0, 300);
.....
Permalink
In VS.NET, how can I specify the start up project for my application?

In the Solution Explorer right click on the project that should be the start up project for your application and choose the Set As Start Up Project option.

Permalink
In VS.NET, how can I change the build order of projects in my solution?

You can set the build order of the projects by right clicking on the Solution and choosing Project Build Order and adjusting the dependencies on the Dependencies tab in the Project Dependencies window that pops up.

Permalink
How can I get the default namespace of a C# project in VS.NET?

To get the default namespace of a C# project developed using VS.NET, you need to right-click on the project in the Solution Explorer and then choose Properties->Common Properties->General->Default Namespace

Permalink
How does VS.Net find the referenced assemblies when I build a project from the command line?

There are a couple of ways to specify where VS.Net looks for the assemblies that you reference in your project when building from the command line.

One way is to specify the ‘HintPath’ for the reference:

<Reference
	Name = 'MyAssembly.dll'
	AssemblyName = 'MyAssembly.dll'
	HintPath = '..\work\samples\assemblies\MyAssembly.dll'
/>

However, this would require all of the systems that build the application to have the exact same directory structure locally. There are often times when this is not wanted (or needed), such as having a script on a build machine.

In this type of situation, you can use the DEVPATH environment variable, or make use of the Public Assembly folder functionality

To use the DEVPATH environment variable, you would simply add the folder that contains the assembly (e.g. ‘c:\work\samples\assemblies’) to the list of directories. Then, you will need to specify the <developmentMode> element in the machine configuration file:

<configuration>
   <runtime>
      <developmentMode developerInstallation='true'>
   </runtime>
</configuration>

This will instruct the CLR to locate the assemblies based on the DEVPATH environment variable.

A less restrictive way to specify the assembly locations is by making use of the Public Assembly Folder functionality. This is done by adding a path to the PublicAssemblies registry key.

Open up the registry, and locate the ‘HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\VisualStudio\7.0\AssemblyFolders’ key (for VS.Net 2003, you would look under the ‘…\VisualStudio\7.1\AssembliesFolders’ key). You will see a subkey named ‘PublicAssemblies’. This is where you will want to add additional keys to create your own Public folders. To add your own folder, create a key (e.g. MyAssemblies), and set the default value to be the desired path (e.g. ‘c:\work\samples\assemblies’).

The option you choose will simply depend on your needs.

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

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

Permalink
How can I tell my form is being used in design time and not runtime

Check the the property Form.DesignMode.

But, note that when in Visual Inheritance mode (designing a derived form), your Control’s DesignMode property will be true when the base form’s constructor gets executed in the design-time.

To workaround this, you could check if the app in which your control is running is not devenv.exe, as follows:

string exePath = Application.ExecutablePath;
exePath = exePath.ToLower();
if(Application.ExecutablePath.ToLower().IndexOf('devenv.exe') > -1)
{
	// Then you are running in vs.net.
}
Permalink
If I create an assembly, how can I see the assembly/components from with the VS.Net ‘Add…’ dialogs?

There are two ways in which this can be done. The first way is to use the provided Public assembly folder that is installed with VS.Net.

This folder is: ‘C:\Program Files\Microsoft Visual Studio .NET\Common7\IDE\PublicAssemblies’

All assemblies in this folder will be picked up by the ‘Add Reference’ dialog under the ‘.NET’ tab, and the ‘Customize Toolbox’ under the ‘.NET Framework Components’ tab.

Now, you are not limited to using this folder. You can specify your own public assembly folder by adding a key to the registry.

If you look at the following key:
‘HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\VisualStudio\7.0\AssemblyFolders’, you will see a subkey named ‘PublicAssemblies’. This is where the path above is specified as a public assembly folder. To add your own folder, create a key (e.g. MyAssemblies), and set the default value to be the desired path.

Permalink
How do I determine the screen resolution?

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

Permalink
How do I prevent my Component from becoming visible in the VS.Net Toolbox?

Use the ToolboxItem(false) attribute on your Component/Control class. This will prevent it from appearing in the Toolbox.

[ToolboxItem(false)]
public class MyComponent : Component{..}
Permalink
How to debug your design time code (like designers, typeconverters, etc.)

You need to use a second instance of VS.NET to debug the one that’s running the code.

Put your control on a from in VS.NET Start a 2nd Vs.Net Choose the Debug menu >> Processes … Double click ‘devenv.exe’ and choose ‘Common Language Runtime’ as the types of debugging Open your code file, set your breakpoint, and you’re debugging.

Posted by Shawn Burke of MSFT in microsoft.public.dotnet.framework.windowsforms.

Permalink
How do I make my custom dialog droppable in the design surface like a component?

Set the Designer attribute for your custom dialog to be ComponentDesigner (instead of the default ControlDesigner). That should make it droppable in the design surface as a component.

Permalink
How do I move a docked control around (or change the dock order) in my Form?

You cannot move it via drag-and-drop. But you change it’s position or dock order by selecting the ‘Bring To Front’ or ‘Send To Back’ verbs in it’s context menu. ‘Bring To Front’ will move it to the top of the children list and ‘Send To Back’ will move it to the last of the children list. This is possible because the docking logic will be applied on the children in the order in which they appear in the child controls list.

Permalink
I have code snippets that I use often. How can I manage them so I can easily used them

In Visual Studio, you can store commonly used code snippets on a tab (other than the Clipboard ring) in your Toolbox. You use drag and drop techniques to move the snippets to the Toolbox. To later use a snippet in your code, you drag it from the Toolbox and drop it into your code.

Permalink
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
Permalink
How do you provide ‘dynamic’ default values

Provide ShouldSerialize#PropertyName# and Reset#PropertyName# along with your property.

Example:

private bool ShouldSerializeFont()
{
	return this.bFontSet;
}

/// <summary>
/// Resets the <see cref="Font"> property to its default value.
/// </see></summary>
private void ResetFont()
{
	this.localFont = null;
}
Permalink
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.

Permalink
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());
}
Permalink
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’.

Permalink
How can I speed up my work in Visual Studio

Close the Dynamic Help window unless it is something you use. Keeping it open will slow things down.

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

Permalink
How can I graphically set the tab order for controls on a form

While MFC programmer’s have had this feature available in earlier versions of Visual Studio, VB programmers have not. To display tab orders on the active design mode Form, select the View | Tab Order menu item. Then click the numbers on each control to reset their tab order.

Permalink
How can I supply a list of values to be chosen from a dropdown list at runtime for a specific type similar to enums?

You have to implement a TypeConverter for your class and override the GetStandardValues and GetStandardValuesSupported method. In your override of GetStandardValuesSupported you have to return true. In your override of GetStandardValues you should return the list of values.

Optionally you can override GetStandardValuesExclusive and allow the user to specify values that are not in the value list.

Note: The standard values collection can be initialized at runtime depending on the context of the instance object.

	public class GridCellValueTypeConverter: TypeConverter
	{
		public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)  
		{
			if (sourceType == typeof(System.String)) 
				return true;
			return base.CanConvertFrom(context,sourceType);
		}

		public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value)  
		{
			if (value is System.String)
			{
				if (((string) value) != '') 
					return Type.GetType((string) value);
				else
					return null;
			}

			return base.ConvertFrom(context,culture,value);
		}

		// no string conversion
		public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)  
		{
			if (destinationType == typeof(String))
			{
				Type type = (Type) value;
				if (type == null)
					return String.Empty;
				else if (type.Namespace == 'System')
					return type.ToString();
				else
				{
					return String.Concat(type.FullName, ',', type.AssemblyQualifiedName.Split(’,’)[1]);
				}
			}

			return base.ConvertFrom(context,culture,value);

		}

		public override System.ComponentModel.TypeConverter.StandardValuesCollection GetStandardValues(ITypeDescriptorContext context)  
		{
			return svc;
		}

		public override bool GetStandardValuesExclusive(ITypeDescriptorContext context)  
		{
			return false;
		}

		public override bool GetStandardValuesSupported(ITypeDescriptorContext context)  
		{
			return true;
		}

		// Fields
		static GridCellValueTypeConverter()
		{
			values = new string[] 
				{
					'System.String',
					'System.Double',
					'System.Int32',
					'System.Boolean',
					'System.DateTime',
					'System.Int16',
					'System.Int64',
					'System.Single',
					'System.Byte',
					'System.Char',
					'System.Decimal',
					'System.UInt16',
					'System.UInt32',
					'System.UInt64',
			};
			Array.Sort(values);
			Type[] types = new Type[values.Length];
			for (int i = 0; i < values.Length; i++)
				types[i] = Type.GetType(values[i]);
			svc = new TypeConverter.StandardValuesCollection(types);
		}

		private static string[] values;
		private static TypeConverter.StandardValuesCollection svc;
	}
Permalink
Whenever I share files between projects it appears that Visual Studio.NET copies the files over into the new project. I want to just share the file and not have it copied over. Is there any way to do this

When you add an existing item from with the Visual Studio.NET IDE, click the small arrow on the ’Open’ button in the dialog that is presented. You will see a list of options. One of these is to ’Link file’. If you select this option then you will not get a local copy and any changes that you make to the linked file will be in the original file.

Another way to get the same effect is to share the files using Visual Source Safe. You can simply drag and drop the files between projects in VSS and they will also be in sync.

Permalink
How do I use VS to add an override for a virtual member of a baseclass

In the ClassView window, expand the base class under your derived class. Then right-click the desired method, and select Add.

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

Permalink
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;
		}
	}
Permalink
How can I have a multiline tooltip

Include a \n as part of the tiptext.

	this.toolTip1.SetToolTip(this.button1, 'Press for\ninformation...');
Permalink
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))

Permalink
How do I compile unsafe code in VS.NET

Click ‘Project | Properties’ from the menus.
Select ‘Configuration Properties’ folder and the ‘Build’ item under that.
Switch ‘Allow unsafe code blocks’ from ‘False’ to ‘True’.

(from Ryan LaNeve on microsoft.public.dotnet.framework.windowsforms)

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

Permalink
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'
   	}
Permalink
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();
Permalink
How do I create and use a component

Take a look at Mahash Chand’s Creating C# Class Library (Dll) found on C# Corner.

Permalink
How do I change a directory name

Use the static move member of the Directory class.

	Directory.Move(oldPathString, newPathString);
Permalink
How do I change the Zorder of my docked windows at design time

You can use the menu selection Format|Move To Front or Format|Send To Back to change the Zorder and update your docking.

Permalink
How do I start Visual Studio.NET from the command line

Simply type devenv.exe from the command line. If you get a message like this, then you do not have devenv.exe in your path. >>> ’devenv.exe’ is not recognized as an internal or external command, operable program or batch file. >>> To fix this simply run the batch file, vsvars32.bat that comes with Visual Studio.NET from the command line in the working folder. After you run this batch file devenv.exe will be available from the command line in that folder.

Permalink
How to make properties in your custom data type appear in a particular order in the property browser
// Your custom data type
public class MySize
{
    ...
    public int Width{get{...}set{...}}
    public int Height{get{...}set{...}}
}

For example, in the above class (MySize) if you want to your properties ‘Width’ and ‘Height’ to

appear in that order, you should provide this override:

public override bool GetPropertiesSupported(ITypeDescriptorContext context) 
{
	return true;
}
        
public override PropertyDescriptorCollection GetProperties(ITypeDescriptorContext context, object value, Attribute[] attributes)
{
	System.ComponentModel.PropertyDescriptorCollection propertyDescriptorCollection;
	string[] propNames;
	propertyDescriptorCollection = 
TypeDescriptor.GetProperties(typeof(System.Drawing.Size),attributes);
	propNames = (string[])new System.String[2];
	propNames[0] = @'Width';
	propNames[1] = @'Height';
	return propertyDescriptorCollection0.Sort(propNames);
} // end of method GetProperties
Permalink
How do you make your custom data types property browser browsable when contained in a Control/Component (as a property)
// Your custom data type
public class MySize
{
	...
	public int Width{get{...}set{...}}
	public int Height{get{...}set{...}}
}

public class MySizeConverter: 
	ExpandableObjectConverter
{
	public override bool GetCreateInstanceSupported(ITypeDescriptorContext context)
	{
		return true;
	}

	public override object CreateInstance(ITypeDescriptorContext context, IDictionary propertyValues)
	{
		MySize size = new MySize();
		size.Width = (int)propertyValues[(object)'Width'];
		size.Height = (int)propertyValues[(object)'Height'];
		return (object)size;
	}
Permalink
How do you detect the designer host getting loaded for the first time in your designer

Subscribe to IDesignerHost.LoadComplete event. Make any changes to the designerhost (like adding/deleting component, etc.) only after this event, or else the design document will not be made ‘dirty’.
(IdesignerSerializationManager.SerializationComplete will tell you when Code is deserialized into design time components).

Permalink
When a property gets modified, how do you refresh other properties of a Component/Control in the property browser

Set this attribute for the property:

RefreshProperties(RefreshProperties.Repaint)
Permalink
How to convert an application project to class library project, and vice versa

In the Solutions Explorer window, right-click the exe project and then select Properties in the popup. Then change the Output type from Windows Application to Class Library. You may also want to comment out the Main method in your code as it is not needed in the library project.

To convert from a DLL to and exe, reverse the process.

Permalink