My New Blog
Visit Me On My New Blog
at WWW.AMRELSEHEMY.NET
This blog is to share ideas and tips about Microsoft Technologies especially programming ones.
Visit Me On My New Blog
at WWW.AMRELSEHEMY.NET
Posted by
Amr Elsehemy
at
7:17 PM
5
comments
Office 2007 offers great new features, one of them is the SuperTooltip which provides much more information about controls than standard old style tooltips. This article shows how to build tooltips in such a way.
read more | digg story
Posted by
Amr Elsehemy
at
6:58 PM
3
comments

Yet another Custom Control (Component).
The Super ToolTip component has rich desgin time support and GDI+ enhanced methods.
you can read the article at http://dotnetslackers.com/articles/net/SuperToolTip.aspx
The download is available there too.
Take a look and welcome for questions.
Posted by
Amr Elsehemy
at
1:50 PM
0
comments
Topic: articles
Posted by
Amr Elsehemy
at
4:11 AM
2
comments
Topic: articles
In this part I’ll talk about the Type Converters one more Service that we can benefit from when making our custom controls.
So what are type converters and what are they used for?
Imagine this scenario, some times we need to convert from one data type to another one, for example before displaying any data type on the screen we need to convert it as a string and in the opposite way too you must cast back the string to Date or int to proceed in logic calculation and so, now for simple types casting works fine but when it is time to cast to and from a complex object the matter wont only be a simple cast Here comes the power of the Type Converters.
Type Converters are classes that define how classes are converted to and from other data types. In Design-Time they are used to convert to and from strings because all property values in the property grid are shown as strings.
Some built in converters are:
Common .NET Type Converters
The .NET framework is already equipped with several useful and reusable type converters. We will examine each of these type converters briefly to help provide knowledge of what is already out there, so that the wheel won’t be reinvented.
System.ComponentModel.BooleanConverter
This class provides the capability to convert Boolean values to and from other representations. Actually, this converter can only convert to and from string representations. A value of true will be converted to and from “True.” A value of false will be converted to and from “False.”
System.ComponentModel.DateTimeConverter
This type converter can convert DateTime objects to and from string representations only. It uses the DateTimeFormatInfo and the Parse method of the DateTime class to perform conversions.
System.ComponentModel.ExpandableObjectConverter
This converter converts objects to expandable representations. It overrides GetProperties and GetPropertiesSupported to return the properties through the GetProperties method of the TypeDescriptor.
System.ComponentModel.TimeSpanConverter
This converter can only convert TimeSpan objects to and from a string. It uses the TimeSpan.Parse method when converting from a string and the ToString method when converting to a string. The string representation is normally in the form, “hh:mm:ss”.
System.Drawing.ColorConverter
This class converts Color objects to and from string representations. All standard colors and system colors that will be returned by GetStandardValues are stored in a Hashtable. The string representation of a color is typically the value of the Color.Name property. However, sometimes, it may be the RGB value if the color doesn’t have a name.
System.Drawing.FontConverter
This converter converts Font objects to and from other representations. The string representation of a Font object is usually a combination of the Font’s Name, Size and Unit. A lot of string manipulation is used when converting fonts.
System.Drawing.PointConverter
This converter converts Point structures to and from string representations. It implements GetCreateInstanceSupported and CreateInstance so that a change to the X or Y property results in a new Point object.
System.Drawing.SizeConverter
This converter converts Size structures to and from string representations. Similar to the RectangleConverter class, it implements GetCreateInstanceSupported and CreateInstance so that a change to the Width or Height properties results in a new Size object.
Enough talking lets see how to implement a complex type converter
The steps
Implementing a TypeConverter
First worth noting is that the same type converter can be used in both web forms and windows
forms. In order to implement a full-fledged type converter, we must follow these steps:
1. Derive a class from System.ComponentModel.TypeConverter.
2. Override the CanConvertFrom method to specify the types that the converter can convert from.
3. Override the ConvertFrom method to perform the type conversion from the given type.
4. Override the CanConvertTo method to specify the types that the converter can convert to.
5. Override the ConvertTo method to perform the type conversion to the given type.
6. Override the IsValid method to perform a validity check.
7. Override GetStandardValuesSupported to indicate whether a set of standard values is supported.
8. Override GetStandardValues to return a collection of the type’s standard values.
9. Override GetCreateInstanceSupported to indicate whether instances of an object
of the type can only be created with CreateInstance.
10. Override CreateInstance to recreate instances of an object.
Ofcourse not all steps are needed
The following code snippet shows how the ExpandableObjectConverter can be used with complex types
Sample class :
public class IntTextInt
{
public IntTextInt()
{ }
public IntTextInt(int n1, string text,int n2)
{
this.intOne = n1;
this.intTwo = n2;
this.theString = text;
}
private int intOne;
public int IntOne
{
get { return intOne; }
set { intOne = value; }
}
private int intTwo;
public int IntTwo
{
get { return intTwo; }
set { intTwo = value; }
}
private string theString;
public string TheString
{
get { return theString; }
set { theString = value; }
}
}
Sample UserControl:
public partial class MyCControl : UserControl
{
public MyCControl()
{
InitializeComponent();
}
private IntTextInt myVar;
public IntTextInt MyProperty
{
get { return myVar; }
set { myVar = value; }
}
}
Once the property is done this is the default provided in this figure
To show inner properties we can decorate the complex property with the
ExpandableObjectConverter Attribute The resulting shape is
Posted by
Amr Elsehemy
at
10:51 AM
1 comments
Topic: Custom Controls, Windows Forms
Hi again,
In part 1 I talked about the components that work together to give a design-time support for your custom controls.
In this part I will crack in some details, I will start with the UI Type Editors,
First for people who don't know what is an UI Type Editor , it is the editor that appears in the property grid window, it is the part where you actually assign the property with a value, the default editor is just a white space to enter your value like this:
just a plain space as you see to put your value in, the visual studio is kind enough to give us some default editors for our properties having some known types for example , bool : your boolean properties automatic will be given the default editor of boolean types which is a small combo having two values true and false, one more complex editor visual studio gives is for properties of type Color: are given a palette of colors(custom,web,system), some other editors are not in place which means they don't appear as if they were from a dropdown window for example when you edit the Items collection of a ListView or TabPages collection of TabControl those properties when edited a popup window appears (a dialog) to give more options handling complex Properties ,. I think that was enough for an introduction and lets see how we make some for our Controls.
And now for some real code, how to make a uitypeEditor for your control.
You have the following tasks to follow:
1- Of course have the Custom Control that has a property that needs a sepcial editor.
2-Implement a class that Derives from System.Drawing.Design.UITypeEditor.
3-You need now to override some methods:
3.1-First one which tells the type of the Editor DropDown, Modal.None
public override UITypeEditorEditStyle GetEditStyle(
System.ComponentModel.ITypeDescriptorContext context)
{
return UITypeEditorEditStyle.DropDown;
}
Very simple you just return a value of type UIEditorEditStyle and thats it.
3.2-Next one is to tell the design time environment interacts with the user interface of our small editor,this example I give here is making the editor of a property of Type int to appear as a Vertical TrackBar and read its value to the property
which can be used later as the editor in my ProgressDisk Control of the SquareSize property.
public override object EditValue(System.ComponentModel.ITypeDescriptorContext context, IServiceProvider provider, object value)
{
IWindowsFormsEditorService iwfes =(IWindowsFormsEditorService)provider.GetService( typeof(IWindowsFormsEditorService));
if (iwfes == null)
{
return null;
}
TrackBar trackBar = new TrackBar();
trackBar.Orientation = Orientation.Vertical;
trackBar.SetRange(30, 90);
trackBar.Size = new Size(50, 100);
iwfes.DropDownControl(trackBar);
Size theSize= new Size(trackBar.Value, trackBar.Value);
return theSize;
}
The trick here is to add a control to the IWindowsFormsEditorService reference, in the example I added a trackbar for more complex scenarios you can make a UserControl to take care of more options as Drawing itself having more controls as this one:
3.3-UITypeEditor has two more methods that can be overriden to support some painting in the editor are itself as this:
These 2 methods are:
3.3.1-GetPaintValueSupported and this is to tell the Designer that the Editor Supports Drawing
public override bool GetPaintValueSupported(ITypeDescriptorContext context)
{
return true;
}
3.3.2-PaintValue which paints the part giving event args e having 4 main properties
Value: the current value of the property.
Graphics: graphics object used to draw with.
Bounds: the rectangle bound to draw on.
Context: gets the current context.
public override void PaintValue(PaintValueEventArgs e)
{
string bmpName = null;
int theSize = (int)pe.Value;
if (theSize > 80)
{
bmpName = "best.bmp";
}
else if (theSize > 60)
{
bmpName = "ok.bmp";
}
else
{
bmpName = "bad.bmp";
}
Bitmap b = new Bitmap(typeof(SizeEditor), bmpName);
pe.Graphics.DrawImage(b, pe.Bounds);
b.Dispose();
}
4-Now the only thing left is to use the appropriate Attribute that attaches the UITypeEditor we just made to the Property as
[EditorAttribute(typeof(SizeEditor),
typeof(System.Drawing.Design.UITypeEditor))]
public int BlockSize
{
...
}
All these methods of course are optional choose which methods you need to override according to your Properties needs and good luck.
For some code samples I am working these days on a collection of commonly used Editors I will post all soon after this series, describing the function of each.
Now this part has come to the end, be welcomed for any questions , discussions or comments, thanks.
Wait for the next part ...
Posted by
Amr Elsehemy
at
11:00 PM
0
comments
Topic: Custom Controls, Windows Forms