Winforms : How to add Custom Property to a control

When using Winforms controls, you might come to a situation where you need to store some additional information for particular control.

You will have to add a custom property to the control and you can achieve that in two ways:

Use a Tag property

If you only need to add a single custom property, you can just use Tag property. Every control has a Tag property which is an object type, so you can store any custom object.
Only exception is when setting the property in Windows Forms Designer in which case you can only store a string.

Extending the Control

With this approach, you create a new class that extends a Control and then just put additional properties to that class.

using System.Windows.Forms;
class ChessBoardSquarePanel : Panel
{
public string Coordinate { get; set; }
}

If you found this article useful, drop a comment or connect with this blog on social networks.

Share this page

Add a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Back to Top