Live Chat Icon For mobile
Live Chat Icon

WinForms FAQ - Tips

Find answers for the most frequently asked questions
Expand All Collapse All

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

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);
      }
            
      /// 
      /// 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.
      /// 
      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();
      }
            
      /// 
      /// This implementation for the Item property for the IList interface. It’s
      /// property type is object.
      /// 
      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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

Example:


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

/// 
/// Resets the  property to its default value.
/// 
private void ResetFont()
{
	this.localFont = null;
}
Permalink

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

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

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

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

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

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

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

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

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

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

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

Share with

Couldn't find the FAQs you're looking for?

Please submit your question and answer.