Archive for the ‘.NET’ Category

I hate the Control.Visible property. It tries to do two things instead of one and so broken.

Imagine you have a Panel somewhere in your application and you add a Button onto that Panel. Set the Button.Visible to be False and then back again to True. It works exactly as you would expect by hiding the control and then displaying it again.

Now move up a level and try setting the Panel.Visible to be False so the whole Panel, including the contained Button, is hidden away. Whilst hidden try asking for the Button.Visible value and you will see it comes back with False! This is bad because it’s clearly returning a value indicating if the Button is currently visible rather than a value indicating if the Button would like to be visible. There is a big difference between those two semantics.

This problem becomes evident when you want to create your own custom layout panel. Your custom control will need to scan the set of child controls and decide how to arrange them. To make your control behave itself you want to honor the visible setting of child controls. But your stuck because asking a child control for its visible property will not always give the correct answer. If your control performs a layout when it happens to be hidden then all the children will automatically say they want to be hidden as well. Not because they really want to be invisible but purely because at the time the child control was asked the parent chain had a control that was not currently visible.

What we really need is two properties. The Control.Visible should act as an indication of the visible state the control would like to have. So the ‘get’ should return the same value as the last ‘set’. Then a new property called Control.CurrentlyVisible that returns if the control is currently visible based on the state of all the parent controls up the chain of controls up to and including the owning Form.

This is something I have had to get around for the KryptonNavigator and KryptonWorkspace controls because they suffer from exactly this issue. They both have child collections of controls and need to honor the visible state of those controls. Luckily I can get around it because they can only have child controls of a type I have defined. So I can easily add the extra properties needed to those defined types. But this would fail if my collection could take any arbitrary control and not just my own types.

Third time’s a charm?
A common belief is it takes three versions before Microsoft really gets a new technology right. Now that Silverlight 3 has been announced at MIX09 it would seem a good time to take a look at this platform.

WPF, the big brother of Silverlight, has been around since Vista was released and although a critical success has only slowly gained traction in desktop development. I think this is because WinForms, despite many shortcomings, is good enough for most business focused applications. With few ‘must have’ reasons to immediately make the jump, adoption is rising but only slowing.

Silverlight adoption has the potential to happen much faster for two reasons. First is the continuing trend towards richer internet applications. Although you can create nice websites using Ajax, jQuery and HTML it takes great developers pushing the limits to achieve it. Silverlight makes it possible for average developers to do the same but in less time.

The second force is just shear weight of numbers. With about a million .NET developers in the world you are instantly opening up rich internet development for them all. Asking a C#/WinForms developer to learn all about Ajax, jQuery, CSS and so forth is a steep learning curve with no leverage of existing skills. But ask the same developer to keep using C# and much of the same base class library and it becomes much more appealing. Plus once you have mastered Silverlight you can transition that knowledge over to your desktop apps with WPF.

This entire preamble merely explains why I find Silverlight interesting as an area I should start thinking about. I would like to hear bad about others opinions and get a discussion going about this topic.

1, Should Component Factory get involved in Silverlight?

2, What type of controls/components etc would be useful?

I have started a forum thread for this discussion. This will make it easier to track the full set of responses on an ongoing basis. Thanks.

Silverlight 3 Beta Site
Silverlight Development Links

You can see a first glimpse of the new user interface for Visual Studio 2010 on the following Microsoft employee blog. Looks quite nice and makes use of WPF for the editing window.

Visual Studio 2010

When you learn a new language you tend to settle into using it in a particular way. So if you missed a little known feature it is unlikely you will suddenly find it unless someone points it out. Here are 5 of my favorite C# features that you might not already be using.

1 – ?? Operator
The ?? operator is called the null-coalescing operator and is used to define a default value for a nullable value types as well as reference types. It returns the left-hand operand if it is not null; otherwise it returns the right operand. So instead of using code like this…

if (reference == null)
   return _defaultInstance;
else
   return reference;

You can just write the following statement instead…

return reference ?? _defaultInstance;

Much cleaner and it even works for nullable value types.

 

2 - DebuggerStepThrough
This attribute can be added to a method or property accessor and used to speed up debugging. When debugging and using step-into the debugger will step past the method with this attribute. This is really useful in preventing you stepping in and out of the many one liners in the code. To apply do this…

public string Name 
{
   [DebuggerStepThrough]
   get { return _name; }
    
   [DebuggerStepThrough]
   set { _name = value; }
}
 

3 – Automatic Properties
Sick and tired of adding get/set code for simple properties? This is no longer a problem with the automatic properties added in C# 3.0. Instead of this…

private string _name;
public string Name
{
   get { return _name; }
   set { _name = value; }
}

…just do this…

public string Name { get; set; }
 

4 - Object Initializers

Also introduced in C# 3.0 are object initializers. This allows you to set property values inside the new statement. Without this you either create additional overloaded constructor with the possible set of initial values or you write the long hand like this…

Employee emp = new Employee();
emp.Name = "John Smith";
emp.Age = 10;

…but this is clearer…

Employee emp = new Employee {Name="John Smith", Age=10}

5 - Constant flags

Want to specify a float instead of a double for a constant? Most people know the ‘f’ modifier after a constant number informs the compiler to make it a single float value. But did you know the full list of others flags?

100m  (decimal)
100f  (float)
100d  (double)
100u  (uint)
100l  (long)
100ul (ulong)

Let me know if I have missed one you really like.

If you’re building an application using the Krypton controls then you need to add a final polish by using a set of professional icons. Otherwise all that hard work creating a nice looking app is going to waste.

To help make this process as easy as possible you can now buy a pack of 265 icons. As an introductory offer the price is just $79 during this month. Each icon has…

Three different sizes

Three different states

 

6 different file formats

- PNG, GIF
- ICO, PSD
- BMP (24bit & 32bit).

You can see a preview of all the icons here.