Tag Archives: C#

Host a process window inside your applications window

The following snippet will allow you to host the window of any application inside your own application. This isn’t a recommended practice but it’s a fun method that might spawn some interesting ideas.

In order to get this working we will need to pinvoke two Win32 functions, SetParent and SetWindowPos.

Place the following lines in your class

Shortening a URL using bitly’s API in C#

Using the snippet below you can convert links in your application from this

to this

This is especially important in cases where you have a fairly long URL which you want to post somewhere and you either don’t want to use such a long URL, or simply you are limited by characters. An example of that situation would be a URL such as

which can be shortened down to

The first thing you are going to need it an account from bitly.com. After you have created an account, log in and navigate to https://bitly.com/a/your_api_key. The page will contain your username and your API key which are both needed.

Convert string to binary and binary to string in C#

The following two snippets allow you to convert a string to binary text and also to convert binary back to string.

String to binary method:

Binary to string method:

Usage:

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.