Wrox Programmer Forums
|
C# 4.0 aka C# 2010 General Discussion Discussions about the C# 4.0, C# 4, Visual C# 2010 language and tool not related to any specific Wrox book
Welcome to the p2p.wrox.com Forums.

You are currently viewing the C# 4.0 aka C# 2010 General Discussion section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old March 23rd, 2012, 06:25 PM
Authorized User
 
Join Date: Dec 2004
Posts: 30
Thanks: 0
Thanked 0 Times in 0 Posts
Default C# Properties and Properties Window

I would like to be able to create C# properties for a control class that will be displayed in the Visual Studio Properties Window in design mode. How do you create properties that are initialized for use in the Properties Window during design time?

For example, I would like to create a 'ComPort' property for a control class that would display as a drop-down list of available COM ports when the control is used during design mode.

The property listbox should be populated with the COM port names returned from the SerialPort

Code:
 
string[] ComPortNames = SerialPort.GetPortNames();
It's easy to create a ListBox property that displays as an empty drop-down list but how do I fill in its initial value from ComPortNames during design mode?
Code:
 
public ListBox ComPort {get; set;}
What is the general principle for initializing / setting initial values especially in regard to more more complex objects that are properties of control. For example, the BorderThickness or BorderBrush properties show more complex property selectors in the Properties Window.

Thanks for your help.
__________________
Philibuster
 
Old March 27th, 2012, 11:40 AM
Authorized User
 
Join Date: Dec 2004
Posts: 30
Thanks: 0
Thanked 0 Times in 0 Posts
Default C# Properties and Properties Window HOWTO

In order to implement a drop-down list of COM ports for a 'ComPorts' property that can be recognized by the Visual Studio Properties Browser TypeConverters can be used. Take a look at the following example of the ComPortConverter TypeConverter in MyUserControl:

Code:
public partial class MyUserControl : UserControl
{
 
 
 
 

public MyUserControl()
{
InitializeComponent();
}
private string comport = "Unknown";
[
TypeConverter(typeof(ComPortConverter)), Category("My Properties")]
public string ComPort
{
get { return comport; }
set { this.comport = value; }
}
} internal class ComPortConverter : StringConverter {
private static StandardValuesCollection defaultComPorts = new StandardValuesCollection
private
static StandardValuesCollection defaultComPorts =
new StandardValuesCollection(SerialPort.GetPortNames() );
public override bool GetStandardValuesSupported(ITypeDescriptorContext context)
{
return true;
}
public override bool GetStandardValuesExclusive(ITypeDescriptorContext context)
{
return true;
}
public override TypeConverter.StandardValuesCollection GetStandardValues(ITypeDescriptorContext context)
{
return defaultComPorts;
}
}
More information on this topic can be found at:

http://msdn.microsoft.com/en-us/library/ms972975.aspx
http://msdn.microsoft.com/en-us/libr...v=vs.100).aspx
http://ondotnet.com/pub/a/dotnet/exc..._6/index3.html


__________________
Philibuster





Similar Threads
Thread Thread Starter Forum Replies Last Post
Extending the Properties Window ? vsny BOOK: Professional Visual Studio 2008 ISBN: 978-0-470-22988-0 0 February 27th, 2010 01:36 PM
Lost Properties window ??? gjelson VB How-To 1 April 1st, 2006 01:49 AM
Properties window disappears. bachakhan VS.NET 2002/2003 1 December 29th, 2004 01:28 PM
Exception while painting properties window... KRYSTAAL BOOK: Beginning VB.NET 2nd Edition/Beginning VB.NET 2003 1 December 12th, 2003 10:29 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.