Syntax Highlighter In WPF

9. May 2011 16:55

C# Code Snippets Projects Visual Studio WPF 

If you are writing code for a while now then by now you must have a lot of code snippets which you will be using in your application development, and you use them because they save a lot of development time. At this moment I have now a huge collection of code snippets which includes functions, classes, extension methods and functions that I have extracted from different open source applications.
As I was progressing towards building an application in WPF which helps me managing all my code, a thought ran into my mind that it would be good if I could use syntax highlighting in the code. As usual I began my search to find a control in WPF which supports syntax highlighting and what I found, I was and I am at present satisfied. The control called AvalonEdit is a part of the free IDE called SharpDevelop for C# and VB.NET and Boo projects.
Languages support by the control:
  • HTML
  • JavaScript
  • XML
  • XMLDoc
  • VB.NET
  • C#
  • C++
  • Coco
  • Java
  • PHP
  • Patch
  • Boo
  • TeX
  • Custom Highlighting
I am a .NET guy so I will be using it for highlighting C# code. Fire Visual Studio and create a new WPF project. Add ICSharpCode.AvalonEdit.dll in the reference of your project.

After adding the reference in your project, add below XAML code on the window where you have your code window.
<Window x:Class="SyntaxHighlighter.MainWindow"
        Title="MainWindow" Height="401" Width="617" Loaded="Window_Loaded">
<avalonEdit:TextEditor  Name="txtCode"
At line 4, we will use custom mapping (because it is a third-party control) so we can use the control in our project. At line 7, I have used TextEditor class of the AvalonEdit namespace. The font name and size is the same as of the source code text editor in Visual Studio 2010.
To get the control working with least configuration set some namespaces on the top, and two lines of code on window load for syntax highlighting and to show line numbers respectively.
using ICSharpCode.AvalonEdit.CodeCompletion;
using ICSharpCode.AvalonEdit.Folding;
using ICSharpCode.AvalonEdit.Highlighting;
txtCode.SyntaxHighlighting = HighlightingManager.Instance.GetDefinition("C#");
txtCode.ShowLineNumbers = true;
If you wish to change the language, then just change the name of the language which is passed as a parameter in the GetDefinition method. The code in the Window_Loaded method will allow you to set syntax highlighting specifically for C#, pretty simple but not very useful. Check out the other way where the text editor will load the file and by reading the file extension, it will set the syntax highlighting. Above method will be useful if the user wants to set syntax highlighting of his choice. But if you want to detect the language and get the syntax highlighting automatically, then use the below code.
txtCode.SyntaxHighlighting = HighlightingManager.Instance.GetDefinitionByExtension(System.IO.Path.GetExtension("D:\\Data.cs"));
txtCode.ShowLineNumbers = true;
The first line of code will Load the file and the second file will first get the extension of the file loaded, set the instance of the HighlightingManager class and in the end set the syntax highlighting. This is what I got in the end (I am using the second way to load the file).

Note: To make sure that the second method work, you need to make sure that the file should have a language extension like .cs for C#, cpp for C++, xml for XML files etc. AvalonEdit is an open source code, so you can play around with it and can have your own customizations. There are lots of in-built configurations that you can do to set up your syntax highlighting control. I strongly recommend you to download the below files and take a look at the sample application.
Related Links:
Currently rated 2.5 by 4 people

Windows 7 Logon Screen Changer

29. May 2010 22:42

C# Projects Utils Windows 

Recently I was reading an article on some Windows 7 forums on how to change Windows Logon Background. There are manual steps which helps me to change the longon background, but its tedious if I have to do it manually everytime. So I decided to make a tool which can do it for me. 

I start up a new project in Visual Studio 2010 and create a new windows application. I am not writing about how I did it, I am just giving away this tool for use. Just Browse and click Change Logon Screen and you are done.

Press WIN+L to lock your computer and check your new logon screen. There is only one limitation, you cannot select an image which is larger than 256KB.

I will be uploading the complete source code at codeplex shortly. So keep an eye on the update.

Download: Win7 (49.21 kb)

Source Code: (139.44 kb)

Currently rated 1.0 by 2 people

MP3 Informer - Get Lyrics, Album Art, Artist Image, Album Name and Release Date

15. May 2010 01:13

C# Projects 

I am a huge music fan, so huge that I need headphone while I work on my machine and if I don't have them in my head I can't work.Cool It's not the case with me but most of the people do the same thing. But sometimes the things get worsts when people with their headphones on start singing.Laughing Ok they sing only when they know the lyrics and if they won't they search for them and then sing along. It's not the case with me but for the folks who wants to search lyrics for their songs collection, I have created this small tool which will help them to get lyrics, get album name, album art, release date and artist image. Just enter artist name and song name and let this tool get the details for you. I have consumed lyricWIKI, Last.FM and LyricFly web service to get the details.

This tool is build on .NET Framework 3.5. So in order to use this tool you must have .NET 3.5 Framework installed on your machine.

Download: MP3 Informer.msi (655.00 kb)


 No Rating