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. :D

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 . :D

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 :P

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