Thursday, January 19, 2006

MessageBox.Show(“Hello World!”);

Yes!!! God bless us with a baby!

Below is the 10 week ultrasound which was done last Tuesday. Doctor wrote “This photo was taken by my mom” while my wife operated the ultrasound equipment. It was very beautiful to see our baby moving itself like a cycle :-)) We’re very happy!

Monday, January 09, 2006

When maximum is not a maximum (Windows Form’s Scrollbar control)

Problem
  • You want to allow to your users to set a range value in a visual manner.

Possible solutions in Windows Forms
  • Use a TrackBar control or a ScrollBar (HScrollBar or VScrollBar) control.

We wanted to mimic the UI of MS Word when you set the Fill transparency value for an AutoShape. In the Fill Effects dialog box you’ll find that most transparency controls are HScrollBar that allows you to set it in a range from 0 to 100.
So, to mimic that UI, we dragged & dropped an HScrollBar control onto a Form. We set their properties Minimum to 0 (zero) and Maximum to 100. All seems to be easy and straightforward until you run your form.
At runtime, if you scroll to the minimum you’ll get the minimum value i.e. 0 (zero). But if you scroll to the maximum you’ll get 91. How is it possible?
Well the answer is in the Maximum property help on MSDN that states:

Note The value of a scroll bar cannot reach its maximum value through user interaction at run time. The maximum value that can be reached is equal to the Maximum property value minus the LargeChange property value plus one. The maximum value can only be reached programmatically.

Ah… that makes sense :-S

Maximum (100) – LargeChange (10) + 1 = 91

Ok. If that is the logic behind ScrollBar controls, then in order you can reach a Maximum value at runtime scrolling it we’ll need to set the Maximum property to:

Maximum desired (100) + LargeChange (10) – 1 = 109

That’s it. No comments.

Wednesday, January 04, 2006

UITypeEditor for Font Name properties

We’re making big changes and enhancements (I guess) in our current development of the next release of our ImageDraw control for ASP.NET
One of the big changes we made is that now, ImageDraw will support layers and a lot of imaging actions out of the box. We’ve included an action that will allow you to create a Polaroid version of a specified image. Polaroid will support text that you can include onto it a.k.a. captions.
So, we designed a Polaroid class that has a Font property in order you can specified the font style you want to use for Polaroid’s captions.

In a first place, we though in using the FontInfo class of ASP.NET, the same used by WebControl class for its Font property. But we discard it when we did realize that it has not an appropriated TypeConverter needed for State Management and because it is a Sealed Class i.e. we can’t inherit from it and provide a custom TypeConverter.

In second place, we went for System.Drawing.Font class. It seemed to be the correct class because it has all properties we need to draw text using GDI+ and it has a built-in TypeConverter! Wrong choice I’d say. Its TypeConverter seems not to be prepared to ASP.NET serialization, I mean, when a Font object is serialized on a WebForm, some read-only properties of Font class, such as Bold, are serialized on the WebForm making the Designer begins throwing exception about you can’t set read-only properties.

Without success on built-in Font classes, we decided to design one and we called it Font, of course. We made it very simple with properties like Name, Size, Bold, Italic, Underline, and Strikeout.
If you try our products, you’ll see that we always make great efforts on design-time experience. When we can imitate a design-time feature of built-in ASP.NET controls, we’ll do it. And Font Name property is one of those cases.

When you drag & drop an ASP.NET control like Label, you’ll find that it has an “expandable” Font property. This Font property allows you to set a Font Name by selecting one of its installed Fonts list.
As I stated before, we went for that beautiful Font List to include it into our Font Name property. You can easily found it using tools like Reflector going to System.Web.UI.WebControls.FontInfo class and selecting Name property. You’ll realize that the font names list is UITypeEditor class in the System.Drawing.Design.dll assembly called System.Drawing.Design.FontNameEditor.
So, add the following attribute to our Name property and... that’s it?

[
Editor(typeof(FontNameEditor), typeof(UITypeEditor))
]

public string Name
{
get{}
set{}
}


Well, that’s not it! Compile your project and you’ll get the following i.e. a Name property with no button picker to drop down the font list.

To solve the issue, we must add another attribute that is involved with Font list editor. It’s a built-in TypeConverter for Font Names called System.Drawing.FontConverter.FontNameConverter. Basically, this class gets all installed Fonts and together the FontNameEditor class, both make the trick.
Change your Name property to look like the following and you’ll get your Font Names list finally.

[
TypeConverter(typeof(System.Drawing.FontConverter.FontNameConverter)),
Editor(typeof(FontNameEditor), typeof(UITypeEditor))
]

public string Name
{
get{}
set{}
}