Tag Archives: C#

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

Delete files or folders to recycle bin in C#

Sadly C# doesn’t have the required libraries in place to aid the users or the developers to send files or folders directly to the recycle bin. System.IO.File.Delete() and System.IO.Directory.Delete() simply delete the path without allowing for the action to be undone.

Luckily there are two ways this can be resolved. Either by using the Windows API SHFileOperation function or by referencing Microsoft.VisualBasic and using the Microsoft.VisualBasic.FileIO.FileSystem.DeleteFile() and Microsoft.VisualBasic.FileIO.FileSystem.DeleteDirectory() methods.

Personally I prefer the Microsoft.VisualBasic approach as the code looks much cleaner and is more readable to me but I will demonstrate how both methods can be implemented either way.

The Microsoft.VisualBasic approach:
Reference Microsoft.VisualBasic in your project and simply call

or

depending if you want to delete a file or a folder.

Serialize an object to string and from string back to object

Serialization enables us to store or transmit data structures or objects. This can be done by converting the data to a textual representation when storing or transmitting them which also allows us to recreate the original object using the stored information.

Lets create a dummy class called Person, that will hold some basic information.

Keep in mind that your class needs to have a parameterless constructor in order to be serialized or else you will get an InvalidOperationException exception (cannot be serialized because it does not have a parameterless constructor) if you try to serialize a class without one. Luckily you can just set it to private or internal so it won’t be accessible from other classes.

Now it is time to serialize our Person object.