History of bools and p/invoke

Raymond has an interesting post on the history of boolean across different APIs.  Because each different kind of boolean has a different size/interpretation for true/false, when writing a p/invoke definition it’s important to use the MarshalAs attribute to specify which kind of boolean you mean.


Creating a custom control in winforms – Part 6: Testing!

Continuing on from last time… Now that we’ve got Painting, Mouse handling up to speed, it’s time to build a test application. After adding a Square to your control, add these few lines in OnLoad (or after initialize component) PropertyGrid pg = new PropertyGrid();this.Controls.Add(pg);pg.Dock = DockStyle.Left;pg.SelectedObject = square1; Now when you run the application, you’ve…


Creating a custom control in Winforms – Part 5 Default Values for interesting datatypes

 Specifying interesting datatypes in the DefaultValue attribute   Ok so last time we got off easy with a boolean property – what about something more complex – for example a Color?  How can that be specified?   The DefaultValueAttribute has another overload that we can use for this – it allows us to pass in a Type…


Creating Custom Controls in Winforms – Part 4 – Adding a property: DefaultValue attribute explained

Debunking a common misconception   There is a lot of confusion around the DefaultValue attribute – it feels like it really should setup the initial value of the property.  Unfortunately, this is not the case.  It’s used to determine whether the property should be written out to InitializeComponent, and what to reset the property to when…


Andrew has joined the blogging world

Andrew – yet another windows forms groupie – has just joined the world of the blogging.  Can’t wait to see what he’s got to say.


Creating Custom Controls in Winforms – Part 3 Painting OnMouseOver

Continuing on building our custom control…   Handling Mouse Movement   Lets add some pizzazz to the square – it seems too stale.  When we enter the square, let’s draw a dark rectangle around the outside, and when we leave let’s erase that border.     In order to achieve this, let’s add a private…


Creating a Custom Control in Winforms – Part 2 Adding Paint

Continuing on from last time….   A grey square on a grey form is not very interesting…. Lets add some paint.  I’ve talked a bit about painting before in Painting Best Practices, but we’re going to do it for real this time.   We’re going to paint red, yellow, green and blue squares, making use…


Creating a Custom Control in Winforms – Part 1

Creating the Square control Creating a custom control in windows forms is very simple – there are a few gotchas along the way – so I thought I’d make a sample control called “Square” to walk through it.  The square will obviously have sizing restrictions, but will also do some simple custom drawing.   Making…


Picking your base class wisely

One of the harder things to decide when you’re starting out building a custom control is what to derive from.  There are a couple different layers to the control hierarchy, and it’s not always clear what to derive from. Base class What it adds When to inherit from it Component The IDisposable pattern on top of System.Object…