Skip to main content

Code Snippet Manager in C# and WPF

Hello all, today I will post a simple code snippet manager created using C# and WPF. The idea of the app is to be used when making presentations and you need to paste all kind of code snippets (html, xaml, C#, java ). The similar tool is used by Karen Corby in MIX09 presentation about creating silverlight controls.
This is my first .net application so you will see all kind of C# strangeness like using "Object" instead of "object" and String instead of "string".
Because this was my first application I choose to use WPF not WinForms because WinForms is a lot like Swing and I've wanted to see "how declarative XML for the UI is better" and I can say: "hmm ok there is some big pluses". Anyway I have little experience with Flex's MXML too and if you ask me the MXML is a lot than XAML.
The code snipper manager application lokks like this :
The view is very simple and I can say that XAML is perfect for creating a simple views and there is tones of snippets in internet for all kinds of examples like popup for example.
The application XAML looks like this :


<Window x:Class="CodeSnippetManager.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Snipper Manager" Height="355" Width="177" Topmost="True" ResizeMode="NoResize" >
<Grid Height="309">
<ListBox Margin="0,-8,0,38" Name="listSnippets" HorizontalContentAlignment="Left"
MouseRightButtonDown="listSnippets_MouseRightButtonDown"
SelectionMode="Single" SelectionChanged="listSnippets_SelectionChanged"
MouseDoubleClick="listSnippets_MouseDoubleClick">

</ListBox>
<Popup Placement="Mouse" Name="popupHint" HorizontalAlignment="Left"
VerticalAlignment="Top" IsOpen="False" AllowsTransparency="True">
<Border BorderBrush="Black" BorderThickness="1">
<Grid Background="White">
<TextBlock Background="White"
x:Name="PopupTxt"
TextAlignment="Center"
VerticalAlignment="Center">

</TextBlock>
</Grid>
</Border>
</Popup>
<WrapPanel Height="40" Name="wrapPanel1" VerticalAlignment="Bottom" HorizontalAlignment="Center">
<Button Height="26" Name="menu" Width="65" Margin="5" Click="menu_Click">Menu</Button>
<Button Height="26" Name="clear" Width="65" Margin="5" Click="new_Click">Clear</Button>
</WrapPanel>
</Grid>
</Window>


Maybe you can notice that code is short and very simple, maybe simpler than MXML in Flex and a lot simpler than Swing/SWF and JavaFX. There is no code fragments on the view and etc. But what i did not understand is why all kinds of Name x:Name and other attributes are needed. The XML schemas are strangely organized if you ask me.
Anyway the project is uploaded here http://snippetmanager.codeplex.com/Wiki/View.aspx?title=Home. You can download the source and edit it if you want or you can download the release version and try to use it.
The application have the fallowing features:
- creating and adding code snippets from the clipboard
- saving sets of snippets as XML files for usage in the future
- loading sets of snippets
- delete snippet if it is inserted by mistake
Maybe you will notice strange fonts rendering ( there is no clear type ). This is because WPF 3.5 doesnt support clear type at this moment. But clear type will be supported in the incoming .net 4.0 release. If you ask me the current fonts looks even bad than the Java Swing font rendering before 1.6 update 10 which is funny.
The Expression Blend application is written in WPF too and the fonts looks also this bad but if you use dark theme (black by default) it is harder to notice the bad looking fonts. So if you want change the application color schema to darker. I will add dark theme when I have time.
The reason why I've created this app is because I often need to write code in front of the students when I make presentations and I think that using a tool like this decrease the possibility to make mistakes when writing.
The idea of this app came after I watch the Karen Corby presentation in MIX09 called Building Microsoft Silverlight Controls btw you can watch it here http://videos.visitmix.com/MIX09/T16F . But because I was unable to find this app I've created my own.

Comments

Anonymous said…
that is a silly comparison. Use javaFx instead if you want to compare it with Java technology.
jNayden said…
Heh :) Ok maybe I will but the target is different.


I am making normal regular not animated application so I dont need JavaFX for it. But ok Maybe I will make demo in JavaFX
Sara Reid said…
Code snippets in Visual Studio are small parts of written code that can be inserted using a specific shortcut again and again in applications. In this post I will demonstrate how to use them through an example.

Suppose you find yourself writing the code below again and again in your application:
if (SomeObject!=null)
Do this
else
Do the other

Then it is nice if you could create a snippet in visual studio and whenever you need this code, insert it automatically through a sequence of keyboard/mouse shortcuts.

zubehör
jNayden said…
Yes thats true but I am not a windows programmer so I have no idea how to add a OS hook from C# which responds to some key/mouse combination. Anyway the tool is free opensource the source code is very small so if you want you can add it. Maybe I will add feature like this in Swing version of the app (http://gochev.blogspot.com/2009/09/code-snippet-manager-in-java-and-swing.html ) because I have idea how to do this in Java but in C# ( where is easier I dont know how :) )
Rahul said…
hi,

First of all. Thanks very much for your useful post.

I just came across your blog and wanted to drop you a note telling you how impressed I

was with the information you have posted here.

Please let me introduce you some info related to this post and I hope that it is useful

for .Net community.

There is a good C# resource site, Have alook

http://www.csharptalk.com

simi

Popular posts from this blog

Convert PFX certificate to JKS, P12, CRT

I recently had to use a PFX certificate for client authentication (maybe another post will be coming) and for that reason I had to convert it to a Java keystore (JKS).  We will create BOTH a truststore and a keystore, because based on your needs you might need one or the other.  The difference between truststore and keystore if you are not aware is(quote from the JSSE ref guide : TrustManager: Determines whether the remote authentication credentials (and thus the connection) should be trusted. KeyManager: Determines which authentication credentials to send to the remote host. Ok that's enough what you will need is openssl and Java 7+ ;) ! First let's generate a key from the pfx file, this key is later used for p12 keystore. openssl pkcs12 -in example.pfx -nocerts -out  example .key   Enter Import Password: MAC verified OK Enter PEM pass phrase: Verifying - Enter PEM pass phrase: As shown here you will be asked for the password of the pfx file, l

Hibernate Generic DAO.

When you use Hibernate and DAO pattern it is a good idea to use a Generic Base Dao. The fallowing code snippet contains GenericDAO that is a base class for all my DAO classes. This GenericDAO uses HibernateDaoSupport from Spring for its implementation if you want you can use JpaDaoSupport or JdbcDaoSupport in your projects. My Generic DAO interface looks like this : package org.joke.myproject.dao.base; import java.io.Serializable; import java.util.List; /** * @author Naiden Gochev * @param <E> * @param <PK> */ public interface GenericDao<E,PK  extends Serializable> {     PK save(E newInstance);     void update(E transientObject);     void saveOrUpdate(E transientObject);     void delete(E persistentObject);     E findById(PK id);     List<E> findAll();     List<E> findAllByProperty(String propertyName,Object value); } All method names are very common so I don't

Patching a Maven library with your custom class.

Sometimes you use a library that has a bug. Or maybe it doesn’t has a bug but you want to change something. Of course if it is an open source you can get the sources… build them … with your change and so on. However this first takes a lot of time and second you need the sources. What you usually want .. is to just replace one class.. or few classes with something custom… maybe add a line .. or remove a line and so on. Yesterday… I had an issue with jboss-logging. The version I was using was 3.2.0Beta1 and it turns out that using this version and log4j2 2.0 final basically meant that no log is send to log4j2. The reason was a null pointer exception that was catched in jboss logging class called Log4j2Logger. The bug I submitted is here https://issues.jboss.org/browse/JBLOGGING-107 and it was fixed at the same day. However I will use it as an example since I didn’t knew when this will be fixed.. and I didn’t want to wait till it is fixed. So I was thinking what I want.. to take the j