Tag Archives: winforms

Benchmarking your code

Benchmarking your code is very important since it allows you to pinpoint bottlenecks in your application and allows you to improve the overall performance of your software as well as learning which techniques are more taxing than others.

For this example we are going to be using the Stopwatch class. This class will enable us to see how many milliseconds it required for our code to be executed so we can compare different methods or techniques.

As an example, the code below checks the performance difference of a try/catch block in a loop vs the performance when not using one. Keep in mind that try/catch blocks are not that performance heavy if they don’t actually catch something but the performance gets hit a lot in cases a lot of exceptions are being thrown.

For even better results I would advise running each test 3 to 5 times and simply calculating the average time of execution.

Send email using C#

Sending emails using C# is fairly easy. You require the host or the IP of an SMTP server that the email will be send through and a username/password if that server requires credentials.

For this example I will be providing a way to send emails using the Gmail SMTP server.

Move borderless form using mouse

Forms that have their FormBorderStyle set to None cannot be moved using the mouse by default. In order to achieve this functionally we can use the ReleaseCapture and SendMessage Windows functions.

Simply place the following code in your borderless form class.

Don’t forget to assign your form’s MouseDown event to the form1_MouseDown method !

Keep in mind that you could handle another control’s MouseDown event (a good example would be a MenuStrip control) instead of the form’s one, making your form moveable only when the user clicks and drags that specific control.

Minimize a form without border using the taskbar

By default borderless forms are not designed to be minimized, which means when the form’s FormBorderStyle property is set to None you will notice that clicking the application box in taskbar does not minimize the form.

This can be fixed by overriding CreateParams and adding the WS_MINIMIZEBOX style to the Window and CS_DBLCLKS to the Window class styles.

Simply place the following code inside your Form’s class which you want to enable the minimize functionality using the taskbar.

Enable Double Buffering for Controls to reduce flickering

Some controls do not have their DoubleBuffered property listed in the properties window. For this reason it requires an additional step to enable double buffering for specific controls such as a ListView control.

If you wish to enable the DoubleBuffed property of a control simply use the method provided below.

Example