Category Archives: .Net

.Net programming stuff/tips etc.

using UITypeEditor on Winform with a Generic Type Editor

If you are a developer, than I guess Property Grid isn’t a stranger to you. Property grid  has been used in win form designer to manipulate the properties of the selected object in the designer. Property Grid can also be used as control to use on your own form , to give user a compact way to manipulate properties( It is not available in the tool box , you will need to add it by using choose item option in Visual Studio). Property Grid has an interesting feature , it can show Type specific editor if one is available . e.g editing a color value will result in opening a color box, similarly for font and other types. If you want to have an editor for your custom data type , then you can implement a UITypeEditor and bind it to the target type using attirbute.

Some times you don’t use the whole Property Grid , but only want to display a certain type editor on your form, e.g you want to give user , option to edit a font , and want to utilize the font editor . How this can be achieved? Well, its simple , all you need to do is implement the IWindowFormsEditorService interface and provide a form or control to host the uitype editor. 😀

I came across a very insightful article on DevX about implementing these requirements. Emmanuel Tissandier has not only explained the underlying architecture of UITypeEditor  but also impelement a control which can be used to host any UITypeEditor, and so you can have a generice type editor.

Article can be accessed here:  Build A Property Editor That Can Edit Any .Net Type

regards

Regular Expression : Escaping Meta Characters within character classes

Many of us have written applications where we have used regular expression for different tasks, like validation , parsing and other related task. Regular Experssion is quite a powerful tool, and has been available in most of the programming languages that are used today (either natively supported or by using libraries).

Usually in a regular expression ‘\’ is used as a the escape sequence (it may be different for different languages , I am using C# convention), to escape meta characters. Regular Expression also support a construct called ‘character classes’ ,which can be roughly taken as a set of characters. There are some pre-defined character classes like \d \w \s etc. and if you need a more customized version you can define your own using Square brackets notation ‘[]’.

The world inside the square brackets is much different than the one outside.  Inside the brackets , there are only two meta characters, ‘^’ and ‘-‘; even an opening bracket ‘[‘, asterisk ‘*’ , plus sign ‘+’ are not considered as meta inside []. Furthermore , [] has no escape sequence within them. Now what you will do if you want to have ‘^’ , ‘-‘  and ‘]’ inside a character class.

Well these characters are escaped using particular placement within brackets.

One key syntactic difference is that the backslash is NOT a metacharacter in a POSIX bracket expression. So in POSIX, the regular expression [\d] matches a \ or a d. To match a ], put it as the first character after the opening [ or the negating ^. To match a -, put it right before the closing ]. To match a ^, put it before the final literal – or the closing ]. Put together, []\d^-] matches ], \, d, ^ or -.

source :http://www.regular-expressions.info/posixbrackets.html

Problem Solved . 😀

By the way , this web site gives a good introduction about Regular Expressions.

regards

Faraz

Some Articles

well.. 🙂

I am back after a loooonnnng vacation. :D. I have been really busy during that period (if activity of wasting time is cosidered as a task 😛

Just to start with something, here are some of articles that I have read during my so called vacations.

  • Natural Number Comparer:

Have you ever noticed , how Windows XP Explorer sorts files in a directory by file name.  It does an intelligent sorting by considering the file names and also take into account if there is digit in the file name. e.g file1.txt , file2.txt and file10.txt . Ordinary string sorting places  these 2 strings as file1.txt ,file10.txt, file2.txt, which isn’t the right order. XP sees these strings differently and sort them in almost correct orderi.i.e file1.txt, file2.txt and file10.txt.

This type of sorting might be required in other applications as well. Pascal Ganaye has written an execellent article on Natural Number Comprarer .

  • Colors:

Nice article on Manipulating Colors in .Net by Guillaume Leparmentier in which he discussed the different color models and their inter-conversion. Very informtive!!!

  • Cab File Manipulation:

Often you need good compression and archiving APIs, so why not use the Microsoft’s own cab file implementation. Thats exactly what Elmue has done and explained in his article Cabinet File : Compression and Extraction.

Windows client.net

 

gotdotnet, is now being phased out. Although Microsoft has extended the timeline, so the users can coupe up with the changes but eventually it has to end someday. There are several others community platform which Microsoft has recently launched. Two of them I would like to mention, Codeplex (sort of sourceforge like initiative). and WindowsClient.net (for supporting windows application on .net). I don’t like Codeplex thing, but its better to have something instead of nothing. Windows Client is a better site with forums, and articles to help .net developers on windows desktop applications. It targets winform as well as windows presentation foundation (WPF). These two sites may provide a better replacement for gotdotnet. Lets see.

regards

faraz

Default ThreadPool size in .net 2.0 SP1

ThreadPool in .net framework , as the name applies ,  a pool of thread, that an application can use to execute diiferent code section concurrently, instead of creating its own thread. Application borrows thread from thread pool to exectue via ThreadPoolQueueUserWorkItem , which queues the request , the request is furnished when a thread is available in the thread pool. After execution , the thread is returned to the pool so other requests in the queue can use it. Thread pool are mostly utilized in Asynchronous Programming model.

In the release of .net framework 2.0 , the default max no. of available threads in the pool was 25 per processor. You can set it to a different value, but it is usually discouraged to set to a value too higher, and you may need to reconsider your application (profiling etc) , since it may lead to starvation of the threads. With .net 2.0 SP1 , this max no of thread has been elevated to 250 threads per cpu . Joe Duffy has discussed about necessity of this higher limit in his post.

well I think it is not a good decision, setting a default 250 limit, will result in larger startup time to intialize the thread pool. Though you can set your limit to a lower level,(do it as early as you can). But I think this feauter should be an opt-in feature , instead of an opt-out (as .net framework 2.0 already presents it).

regards

faraz

Blog Publisher source code uploaded

I have uploaded the blog publisher (messenger add-in)  source code, and added it to my projects section. Check the projects page. 

I will be adding few more fun apps soon , so do check back later.

regards

faraz

My Fist Live Messenger Add-in : BlogPublisher

Well Its my first experience with live messenger addin. I wrote a blog publishing addin , that reads RSS feed from the blogs or any other site and updates my Personal Status Message on Live Messenger at regular interval (at the moment it updates after 30 secs) .

Introduction:

Windows Live Messenger exposes the Add-In functionality, though its still in beta stages and not fully exposed to the users. However for the developers, it can be enabled by setting up a key in the registry \HKCU\Software\Microsoft\MSNMessenger

Add a  DWORD “AddInFeatureEnabled” and set its value to “1”.

Now Sign in with your windows live messenger, goto options , you will see  a new add-in tab in the left pane.

 

Now comes hard part  or should I say the fun part. You can develop a plug-in for yourself too and its easy. Live Messenger exposes its functionality via MessengerClient.dll, which has been exposed as .net 2.0 assembly. As of now , the Live Messenger supports plug-ins targeted for .net 2.0.

To start developing the plugin, just add the reference of MessengerClient.dll , (you can find it in the msn messenger installation folder, default location is <ProgramFilesDir>\MSN Messenger\MessengerClient.dll).

Create a new project in visual studio 2005, or other IDE you are using, add reference to the dll by browsing to the directory. Messenger exposes its add-in functionality through  IMessengerAddIn Interface. This Interface has one method ,Initialize(MessengerClient). This method is invoked when your add-in is being loaded. Here you will receive a messenger client object. The MessengerClient has following members:

  1. Properties
    • AddInProperties : Information about the AddIn, DisplayName , Private Message , Creator Info.
    • LocalUser : Information about the signed in user.
    • SavedState: Storage for pluging specific data (like settings etc.)
  2. Methods
    • SendActionMessage
    • SendTextMessage
    • SendNudgeMessage
  3. Events
    • IncomingTextMessage
    • OutgoingTextMessage
    • ShowOptionsDialog
    • Shutdown
    • StatusChanged

I think all these members are quite self explanatory. ShowOptionsDialog occurs when the user clicks on the Settings button in the Add-In Screen in Option Dialog.

So Where to start, well create a class , implement the IMessengerAddIn interface and the world is all yours. But watch for the tricky part, Messenger expects the assembly name as per the NamespaceQualified Name of the Addin Type. So if you are implementing the interface in MyTestAddIn, which is in the namespace of MyMessengerAddIns, then your assembly name should be MyMessengerAddIns.MyTestAddIn.dll . yeah yeah I know that’s insane , but its how the add-in works in messenger, might be this behavior change in future as we have the beta right now.

Now invoke your imagination power and do something creative. I worked out a blog publishing plug-in, which will fetch the rss feed from the blogs (which you can specify in the settings box) and then updates your personal messages setting it to the entries in the feed at regular intervals.

 

I wrote a simple RSS reader, which reads the feed from the URL , then parses it into a model, (it only reads selected properties.)  Reading RSS is quite simple , its simply an XML document. You can get the full specification on the Internet, or just view the source of any rss feed in your browser , you will understand the structure easily. I have two timers in my AddIn one for updating the personal message at regular interval and the other for checking the updates on the feed.

Important Notes:

The Messenger AddIn is executed in Internet Zone (so you have very limited permission to do anything, no file access , no internet access 🙂 ) unless you strong name your assembly and install it in the gac. 

To Debug the add-in via Visual Studio Debugger, goto the Project Properties and set the Start Action to start an external program i.e “msnmsgr.exe” it is in the same directory as the MessengerClient.dll. When you run the project in VS,it will invoke the messenger, which in turn load your dll. Install your assembly into gac , after the messenger is invoked and while it is loading your add-in. that way it will not be obstructed by Code Access Security obstacles.

Each Add-In is executed in it own Application Domain , and it is sandboxed , so that it will not harm other  add-ins.

Resources :

Special Thanks to Bart De Smet for his post “Your first Windows Live Messenger add-in” , a very nice and well written tutorial to the add-in functionality. Another excellent article on CodeProjectCreating Windows Live Messenger Addins” by Filip van der Meeren. For RSS reading , “You Own .Net RSS Feed in C#” by James H. Byrd  and RSS 2.0 Framework by Chris Richner are very helpful.

Where to go from here:

well the options are uncountable. I am thinking about writing a secure chat plug-in so that it will encrypt you conversation using some public key algorithm, so that no more msn sniffing can haunt.

Source Code :

I will upload the source code soon , I am looking for some file hosting site, as the wordpress only uploads images and pdf files. Please check back later, or if you need it immediately then just drop me a comment.