Posted by: Cirilo Meggiolaro | 04/4/2009

Tip of the day #172 – Saving memory in Windows Forms

It is pretty usual to add labels to Windows Forms that don’t have any function other than describe the information that an user must input in a control such as TextBox, ComboBox and so on.

By default, all controls that you drag and drop onto the form surface have a member variable created on the designer that allows you to access the control programmatically. The labels are a good example of “waste” memory. If you don’t have to set any property programmatically for this control, why do we need a variable in memory for each one?

Microsoft didn’t forget to give us an option to choose whether we want to generate this member or not.

How to…

Easily defined, just change the GenerateMember property of your control to false. You don’t have access in compile time to this control but you are saving nice 4 bytes for each label in runtime.

Important: There is no problem when you work with localized versions of your form if you are not setting the text programmatically but binding directly to a resource file.

Enjoy it!

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s


%d bloggers like this: