:: Home

  login:         
  passwords:  

Winforms Interview Questions

Windows Forms Deployment
Windows Forms Controls
Windows Forms Data Binding
Windows Forms Datagrid
Windows Forms Docking
Windows Forms Keyboard Handling
Windows Forms Layout
Windows Forms Licensing
Windows Forms Menus
Windows Forms Mouse Handling
Windows Forms from MFC
Windows Forms from VB6
Windows Forms Patterns
Windows Forms Printing
Windows Forms Resources
Windows Form Scrolling
Windows Forms Tips
Windows Forms Common Dialogs
Windows Forms Listbox
Windows Forms ComboBox
Windows Forms Rich TextBox
Windows Forms ListView
Windows Forms TreeView
Windows Forms Button
Windows Forms TabControl
Windows Forms TextBox
Windows Forms MDI
Windows Forms Cursors
Windows Forms WebBrowser
Windows Forms PictureBox
Windows Forms Form
Windows Forms MDI
Windows Forms In IE
GDI+Bitmaps&Images
GDI+Font
GDI+Color
GDI+Brushes
GDI Drawing Tips
GDI+ from GDI
GDI Paths Regions
GDI+Pens
Interioerability WIn32
Tools Metadata Viewers
Design Time Serialization
Design Time Custom Designers
Design Time Tips
Design Time Type Editors
Vs.Net Tips
Vs.Net Debugging
Vs.Net Macros
Framework Tips General
Framework Tips Events
Framework Tips General IO
Framework Tips Strings
Framework Tips Threading
Tool Resource Editor
Design Time UI
Framework Tips CGI
Framework Tips XML

WPF Interview Qs

SilverLight Interview Qs

SAP Interview Questions

Oracle Interview Questions

PHP Interview Questions

Ajax Interview Questions

IIS 7.0

OOP Interview Questions

Ruby Interview Questions

Sql Server Interview Questions

SharePoint 2007 Questions

Microsoft Crm Questions

Design Time Type Editors

The General Winforms Interview Questions consists


 the most frequently asked questions in Winforms. This list of 100+ questions guage your familiarity with the Winforms platform. The q&a have been collected over a period of time from various blogs, forums and other similar Winforms sites

46. Design Time Type Editors

    46.1 In my UITypeEditor for a collection, how do I change the "(collection)" string to a string of my own?
    46.2 How can I allow my user to add more than 1 type of object to my collection, during design-time?
    46.3 How can I let the user edit my Multi-Dimensional collection?
    46.4 What should I do to make my StringCollection property editable during design-time?
    46.5 From within a PropertyGrid, is there any way to display property names that differ from the actual class property names?
    46.6 How do I make a class expandable within the property browser?
    46.7 How can I paint a representation of enum values using a UITypeEditor?

46.1 In my UITypeEditor for a collection, how do I change the "(collection)" string to a string of my own?

The string "(Collection)" is coming from the TypeConverter on that property, which is CollectionConverter. To modify this value, do the following...

[TypeConverter(typeof(MyCollectionConverter)]
public class MyCollection : SomeBaseCollectoin
{
// ...
}
internal class MyCollectoinConverter : System.ComponentModel.CollectionConverter
{
public override object ConvertTo(ITypeDescriptorContext context,
CultureInfo culture, object value, Type destinationType)
{
if (destinationType == typeof(string))
{
if (value is ICollection)


{
return "You can return what ever string value you want
here";
}
}
return base.ConvertTo(context, culture, value, destinationType);
}
}


46.2 How can I allow my user to add more than 1 type of object to my collection, during design-time?

The default CollectionEditor will allow your user to add objects of type returned by your collection's indexer method.
You can make this CollectionEditor allow your user to pick the type of object to add by deriving from CollectionEditor and making the change as shown below. This will introduce a drop-down in the editor's "Add" button to allow the user to pick the type he wants to add.
public class CustomCollectionEditor : System.ComponentModel.Design.CollectionEditor
{
private Type[] types;
public CustomCollectionEditor(Type type)
: base(type)
{
types = new Type[]{typeof(ItemType1), typeof(ItemType2), typeof(ItemType3),
typeof(ItemType4)};
}
// Return the types that you want to allow the user to add into your collection.
protected override Type[] CreateNewItemTypes()
{
return types;
}
}
[Editor(typeof(CustomCollectionEditor), typeof(UITypeEditor))]
public class CustomCollection : IList
{
...
}

46.3 How can I let the user edit my Multi-Dimensional collection?

This is usually a problem if you have a custom collection type MyCollection which itself can take items of type MyCollection.
The problem is because a single instance of a UITypeEditor is used by the framework irrespective of how many types and instances it serves to edit. Which means you cannot start editing your MyCollection from within a MyCollection editor, since the editor is already open.
To work around this problem, you can provide a custom editor as follows:

public class CustomCollectionEditor : CollectionEditor
{
// The base class has its own version of this property
// cached CollectionForm
private CollectionForm collectionForm;

public CustomCollectionEditor(Type type)
: base(type)
{
}
public override object EditValue(
ITypeDescriptorContext context, IServiceProvider provider,
object value)
{
if(this.collectionForm != null && this.collectionForm.Visible)
{
// If the CollectionForm is already visible, then create a new instance
// of the editor and delegate this call to it.
BarItemsCollectionEditor editor = new BarItemsCollectionEditor(this.CollectionType);
return editor.EditValue(context, provider, value);
}
else return base.EditValue(context, provider, value);
}

protected override CollectionForm CreateCollectionForm()
{
// Cache the CollectionForm being used.
this.collectionForm = base.CreateCollectionForm();
return this.collectionForm;
}
}

46.4 What should I do to make my StringCollection property editable during design-time?

Use this custom editor attribute for the property that is of the StringCollection type (this can be used for other Collection types too if you want to allow entry of strings into the collection during design-time).

Editor("System.Windows.Forms.Design.StringCollectionEditor, System.Design", "System.Drawing.Design.UITypeEditor, System.Drawing"),
public YourCollection YourProp{get{...}set{...}}
StringCollectionEditor is not public so you have to use the above constructor override and specify the typename. This editor should allow you to edit your collection without any problems.

46.5 From within a PropertyGrid, is there any way to display property names that differ from the actual class property names?

You can do this but it's not as simple as using an attribute. If you want to modify the property names for your component, you have to interact with the reflection mechanism that the grid is using by implementing ICustomTypeDescriptor. The grid operates on objects called PropertyDescriptors with are the Framework's generic wrapper for a property, where reflection is a specific implementation. So what ICustomTypeDescriptor allows you to do is to have *your object* asked for it's properties rather than than the default system, which is reflection.
You then write your own derived version of PropertyDescriptor (an abstract class in System.ComponentModel) and return your property descriptors instead of the ones that come back from the TypeDescriptor... so some of the impl would look something like this.

public class MyFriendlyNamePropertyDescriptor : PropertyDescriptor
{
private PropertyDescriptor baseProp;
private string friendlyName;
public MyFriendlyNamePropertyDescriptor(PropertyDescriptor baseProp, Attribute[] filter)
: base(baseProp)
{
this.baseProp = baseProp;
}
public override string Name
{
get{return this.baseProp.Name;}
}
public override string DisplayName
{
get
{
return GetFriendlyname(baseProp.Name); //replace with code to return a friendly name
}
}
public override bool IsReadOnly
{
get {return baseProp.IsReadOnly;}
}
public override bool CanResetValue(object component)
{
return this.baseProp.CanResetValue(component);
}
public override Type ComponentType
{
get{return baseProp.ComponentType;}
}
public override object GetValue(object component)
{
return this.baseProp.GetValue(component);
}
public override Type PropertyType
{
get{return this.baseProp.PropertyType;}
}
public override void ResetValue(object component)
{
baseProp.ResetValue(component);
}
public override void SetValue(object component, object Value)
{
this.baseProp.SetValue(component, Value);
}
public override bool ShouldSerializeValue(object component)
{
return this.baseProp.ShouldSerializeValue(component);
}
}
public class MyClass : ICustomTypeDescriptor
{
PropertyDescriptorCollection ICustomTypeDescriptor.GetProperties(Attribute[] filter)
{
PropertyDescriptorCollection baseProps = TypeDescriptor.GetProperties(GetType(), filter);
// notice we use the type here so we don't recurse
PropertyDescriptor[] newProps = new PropertyDescriptor[baseProps.Count];
for (int i = 0; i < baseProps.Count; i++)
{
newProps[i] = new MyFriendlyNamePropertyDescriptor(baseProps[i], filter);
string oldname = ((PropertyDescriptor)baseProps[i]).DisplayName ;
string newname = ((MyFriendlyNamePropertyDescriptor)newProps[i]).DisplayName;
}
// probably wanna cache this...
return new PropertyDescriptorCollection(newProps);
}
AttributeCollection ICustomTypeDescriptor.GetAttributes()
{
return TypeDescriptor.GetAttributes(this, true);
}
string ICustomTypeDescriptor.GetClassName()
{
return TypeDescriptor.GetClassName(this, true);
}
string ICustomTypeDescriptor.GetComponentName()
{
return TypeDescriptor.GetComponentName(this, true);
}
TypeConverter ICustomTypeDescriptor.GetConverter()

{
return TypeDescriptor.GetConverter(this, true);
}
EventDescriptor ICustomTypeDescriptor.GetDefaultEvent()
{
return TypeDescriptor.GetDefaultEvent(this, true);
}
EventDescriptorCollection ICustomTypeDescriptor.GetEvents(System.Attribute[] attributes)
{
return TypeDescriptor.GetEvents(this, attributes, true);
}
EventDescriptorCollection ICustomTypeDescriptor.GetEvents()
{
return TypeDescriptor.GetEvents(this, true);
}
PropertyDescriptor ICustomTypeDescriptor.GetDefaultProperty()
{
return TypeDescriptor.GetDefaultProperty(this, true);
}
PropertyDescriptorCollection ICustomTypeDescriptor.GetProperties()
{
return TypeDescriptor.GetProperties(this, true);
}
object ICustomTypeDescriptor.GetEditor(System.Type editorBaseType)
{
return TypeDescriptor.GetEditor(this, editorBaseType, true);
}
object ICustomTypeDescriptor.GetPropertyOwner(System.ComponentModel.PropertyDescriptor pd)
{
return this;
}
// the code for MyClass.... including the properties to show in the propertygrid
}

46.6 How do I make a class expandable within the property browser?

Add a TypeConverter to your class type to tell the Property Browser it should be expandable.

[TypeConverter(typeof(ExpandableObjectConverter))]
public class MyClass
{
}

46.7 How can I paint a representation of enum values using a UITypeEditor?

The following HatchStyleEditor class draws a graphical representation for values in the HatchStyle enumeration. HatchStyleEditor is derived from UITypeEditor and overrides the GetPaintValueSupported method and returns true. GetPaintValueSupported indicates that this editor supports the painting of a representation of an object's value. PaintValue paints the representative value to the canvas.
public class HatchStyleEditor : UITypeEditor
{
public override bool GetPaintValueSupported(ITypeDescriptorContext context)
{
return true;
}
public override void PaintValue(PaintValueEventArgs e)
{
if (e.Value is HatchStyle)
{
HatchStyle hatch = (HatchStyle) e.Value;
Brush br = new HatchBrush(hatch, SystemColors.WindowText, SystemColors.Window);
e.Graphics.FillRectangle(br, e.Bounds);
br.Dispose();
}
}
}

Copyright 2007, Megasolutions Ltd