Tag Archives: winforms

Set placeholder text for textbox (cue text)

Setting placeholder text for your textboxes helps the users to identify better what kind of information it’s needed from them.

Instead of handling the focus enter and focus leave events in order to set and remove the placeholder text it is possible to use the Windows SendMessage function to send a EM_SETCUEBANNER message to our textbox to do the work for us.

This can be done with two easy steps. First we need to expose the Windows SendMessage function.

Then simply call the method with the handle of our textbox, EM_SETCUEBANNER’s value and the text we want to set.

The result will be a textbox with a placeholder text of our choice. The placeholder text will be automatically be removed when the textbox gains focus and will only reappear if the textbox loses focus and has no characters typed in it.

Cue text example

Create and extract .zip files in C#

Sadly there aren’t many flexible or efficient ways to create .zip files in .NET prior to .NET 4.5. Thankfully some people took the initiative and created some very easy to use libraries for creating/extracting and updating .zip files. My two all time favourite are DotNetZip and SharpZipLib.

For this example I will be using the DotNetZip library.

First you will need to download the library (.dll) either from http://dotnetzip.codeplex.com/ or from http://www.fluxbytes.com/?dl_name=DotNetZipLib_v1.9.1.8.rar. The file should contain quite a few libraries, so choose the one that suits your needs the most and add it as a reference in your project.

How to register a global hotkey for your application in C#

If you are looking for a way to set a global hotkey for your C# application that can be used without your form having focus I have created a decently commentated example for you below that you can use.

How to know if a process has stopped or started using events in C#

There are numerous of ways to detect if a new process has started or stopped, sadly the majority of them are extremely inefficient as it requires you to keep looping through the active process constantly to see if a new one appeared in the array or if one is not there any more.

Luckily the windows Win32_ProcessStartTrace and Win32_ProcessStopTrace classes are here to help out.

The first thing we need to do is reference System.Management.dll in our project. Then we need to define the scope in your class which we will be using.

After that we need to initialise the class which will contain the process start and process stopped events and add the handlers and their methods.

Add the two following variables in your Class.

In your constructor the event handlers need to be added.

and then their event methods that will be trigged when a process either starts or stops.

And finally we need to start the events by using

Remove digital signature from a file using C#

There are two reasons why you might want to consider removing the digital signature from some of the files you are using, especially third party libraries.

The first reason is because in many cases it will greatly speed up your programs start-up time. The reason for that is because if a file is signed with Microsoft digital certificate as an example on runtime it will attempt to verify the signature which in most cases it requires an internet connection. In cases where the user might not have an active internet connection or if the verification attempt is blocked by a firewall or any other reason, it will greatly increase the time your application will take to start.

Another important reason for removing a digital signature from a library you want to distribute alongside with your software is that a lot of users feel alarmed when an application tries to establish a connection at start-up, especially if the type of the application you are making doesn’t sound like the type it requires to connect somewhere.

Thankfully it is fairly easy to remove a signature from a file using the ImageRemoveCertificate API function. Below you can find a snippet that illustrates how it can be implemented.

If you would like to use the above code but have no knowladge on how to compile it then simply download the already compiled version from FileUnsigner v1.0.