Archive for the ‘ WinForms ’ Category

Xml Editor Control


If you have a Windows Forms application that involves Xml editing or viewing, you can use this control to save yourself the effort of formatting the Xml content. For now, only syntax highlighting is implemented. I expect to add more features in the future like spacing, grouping, intellisense, etc…

Usage

Simply add the files (XmlToken.cs, XmlTokenizer.cs, XmlEditor.cs, XmlEditor.designer.cs) to your project then drag and drop the XmlEditor control from the Toolbox into your Windows Form.

The XmlEditor control currently has three public properties. Use AllowXmlFormatting to enable or disable formatting on the Xml content in the editor. The ReadOnly property tells whether or not to allow the user to change the text. The Text property sets or gets the text of the Xml editor.

Here is how the control looks like when AllowXmlFormatting = true and ReadOnly = false (default values):

Implementation

To color the Xml string, we have to split it into multiple tokens, then color each token based on its type. I have identified the following token types (based on syntax highlighting behavior in Visual Studio 2008):

  • A “Value” is anything between double quotes
  • A “Comment” is anything that starts with <!– and ends with –> (or starts with <!– and is never closed with –>)
  • An “Element” is any letter or digit that falls between < and a space or >
  • An “Attribute” is any letter or digit that falls after a < followed by space and not closed by >
  • An “Escape” is anything that starts with & and ends with ; (For example &quote;)
  • A “SpecialChar” is any character that is not a letter or a digit
  • A “None” is anything else

The Tokenize() public static method of the XmlTokenizer class does the job of splitting a string into Xml tokens.

An XmlToken object is a representation of an Xml token with details about the exact text of that token, its location in the string and its type.

Here is the code in the XmlEditor control that does the syntax highlighting:

List<XmlToken> tokens = XmlTokenizer.Tokenize(xmlEditor.Text);

foreach (XmlToken token in tokens)
{
    xmlEditor.Select(token.Index, token.Text.Length);

    switch (token.Type)
    {
        case XmlTokenType.Attribute:
            xmlEditor.SelectionColor = Color.Red;
            break;
        case XmlTokenType.Comment:
            xmlEditor.SelectionColor = Color.DarkGreen;
            break;

        //  and so on for the other token types
    }
}

Download source and exe.

SPList Image and SPFile Icon


If you ever wanted to create a WinForms app for SharePoint that displays or interacts with lists, document libraries and files, then taking advantage of the SPList.ImageUrl and SPFile.IconUrl would help improve the user experience for your app.

Below is an app that displays all the webs, lists, document libraries and files in a selected site collection.

We can improve this a little bit by adding icons (taken from TEMPLATE\IMAGES frolder on a SharePiont installation) for some of the nodes, as follows:

However, this is not good enough since we’ll have all lists with the same icon, all doc libs with the same icon and so on… Here is the same app taking advantage of the list image url and file icon url.

For the list or document library, this is done by using the SPList.ImageUrl attribute. Here is howthe tree node was added:

TreeNode listNode = parentNode.Nodes.Add(list.Title);
listNode.SelectedImageIndex = listNode.ImageIndex = GetImage(GetImageFullPath(list.ImageUrl));

For the file, this is done by using the SPFile.IconUrl attribute. Here is howthe tree node was added:

TreeNode fileNode = parentNode.Nodes.Add(file.Name);
fileNode.SelectedImageIndex = fileNode.ImageIndex = GetImage(GetImageFullPath(file.IconUrl));

The GetImageFullPath method simply gets the full path of the image:

/// <summary>
/// Gets the SharePoint full path from the relative path
/// </summary>
/// <param name="relativePath"></param>
/// <returns></returns>
private static string GetImageFullPath(string relativePath)
{
    //  Get image name
    string imageName = Path.GetFileName(relativePath);

    //  Get SharePoint IMAGES folder path
    string fullPath = SPUtility.GetGenericSetupPath(@"TEMPLATE\IMAGES");

    return Path.Combine(fullPath, imageName);
}

Given that now we have the full file system path to the image, all we have to do now is create an image from the file path then add it to the ImageList of the TreeView. The GetImage() method below does the job.

/// <summary>
/// Gets the image of the sp element from specified url
/// </summary>
/// <param name="url"></param>
/// <returns></returns>
private int GetImage(string url)
{
    //  Get image index from the Image List
    int imageIndex = spImageList.Images.IndexOfKey(url);

    //  Image is not in the list, so add it
    if (imageIndex == -1)
    {
        //  Get the image from specified path
        Image image = Image.FromFile(url);

        //  Add the image to the image list
        spImageList.Images.Add(url, image);

        //  Get its index
        imageIndex = spImageList.Images.Count - 1;
    }

    return imageIndex;
}

Here is how the final app looks like. Click on the image below to download the src and exe.

How To Get All Site Urls Without Creating an SPSite object?


Let’s say you are trying to create a SharePoint admin tool that shows all site collections in the farm, then allows the user to interact with only the site collections of interest to him.

To get all the site collection Urls in the farm, you’d usually go with the following approach:

foreach (SPWebApplication webApp in SPWebService.ContentService.WebApplications)
{
    foreach (SPSite site in webApp.Sites)
    {
        string fullSiteUrl = site.Url;
        Console.WriteLine(fullSiteUrl);
        site.Close();
    }
}

The disadvantage in using such an approach is that we have to create an SPSite object for every site collection in the farm, then close it. That’ll mean a lot of work allocating and deallocating memory. Luckily, there is a better approach to do this, shown below:

foreach (SPWebApplication webApp in SPWebService.ContentService.WebApplications)
{
    string webAppUrl = webApp.GetResponseUri(SPUrlZone.Default).AbsoluteUri; 

    foreach (string siteUrl in webApp.Sites.Names)
    {
        string fullSiteUrl = webAppUrl + siteUrl;
        Console.Writeline(fullSiteUrl);
    }
}

This way, we don’t have to create any of the expensive SPSite objects then close them. On a small farm, that’ll help your application load faster, but on a large farm with hundreds of site collections, this approach is necessary.

You can find the full source for the SPSite ListView here. This was created as a Visual Studio 2008 project on top of SharePoint 2010 Beta, but you can easily remove the reference and add a reference to the 2007 Microsoft.SharePoint.dll.

C# Windows Form is Busy


There are two very common ways of telling the user that your application is busy. One is to show a progress bar that gets updated based on the progress getting done, and another is to show the “Waiting” cursor while the application is doing work.

Waiting Cursor

To show the user the Waiting cursor while your program is busy, all you have to do is to set the current cursor to the Waiting cursor before your code runs, then set it back to an arrow after your code completes.

Cursor.Current = Cursors.WaitCursor;

//  Your Code

Cursor.Current = Cursors.Default;

Progress Bar

The progress bar is a more user-friendly solution, but in most cases showing the waiting cursor does the job. Here is the simplest way to use a progress bar:

int totalSteps = 10;
for (int i = 1; i <= totalSteps; i++)
{
    //  One chunk of your code

    int progress = i * 100 / totalSteps;
    blocksProgressBar.Value = progress;
}
blocksProgressBar.Value = 0;

Yes, it’s that easy to implement a progress bar that gets updated based on the work done by your app. However, while progress is shown, the user can’t interact with the UI or do any other operation (the UI thread is the single thread doing the work here). To get the multi-threaded behavior, the easiest way is to use a background worker process, as shown below:

So instead of putting your code in the event handler method, you will replace it with a call to start the worker process then move the code to the worker process events.

private void doButton_Click(object sender, EventArgs e)
{
    backgroundWorker.RunWorkerAsync();
}

The worker process will do its work in the DoWork event. To show progress, the code needs to be split into segments and the background worker ReportProgress method needs to be called whenever a segment of code is executed.

private void backgroundWorker_DoWork(object sender, DoWorkEventArgs e)
{
    int totalSteps = 10;

    for (int i = 1; i <= totalSteps; i++)
    {
        //  One chunk of your code

        int progress = i * 100 / totalSteps;
        backgroundWorker.ReportProgress(progress);
    }
}

Whenever progress changes, we need to update the value of the progress bar.

private void backgroundWorker_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
    blocksProgressBar.Value = e.ProgressPercentage;
}

When the worker process is done (progress = 100%), we reset the progress bar.

private void backgroundWorker_Completed(object sender, RunWorkerCompletedEventArgs e)
{
    blocksProgressBar.Value = 0;
}

Below is a Windows Form application that lets you try the concepts explained above, and also shows you how the Marquee progress bar works, which is shockingly harder than the more realistic single-threaded progress bar we’ve discussed above.

Download source and exe from here.

Sortable ListView


The Windows Forms ListView control doesn’t provide column sorting functionality. So if you click on a column in a ListView Details view, don’t expect the items to be sorted by the clicked column. To get this functionality, we’ll need to sort the items by the clicked column in the ListView ColumnClick event. I searched online for “Sortable ListView” and I found three MSDN articles talking about this: Sort ListView Column in Visual C#, Sorting ListView Items by Column Using Windows Forms, and How to: Sort ListView Items. None of those implementations takes into consideration the type of the column being sorted. That is, they all do string sorting. If you have dates and numbers in your list, then they’ll not be sorted properly. For example, number 2 will be considered greater than 11. Date time 9/9/1400 will be considered greater than 11/11/2020. Below is an implementation that takes into consideration string, DateTime, int and double types. It can be easily extended to handle more types.

  • Add the SortableListView control to your Windows Form
  • When adding columns to the SortableListView, set the Tag attribute to the type of the column.
sortableListView.Columns.Add("String Field").Tag = typeof(string);
sortableListView.Columns.Add("DateTime Field").Tag = typeof(DateTime);
sortableListView.Columns.Add("Int Field").Tag = typeof(int);
sortableListView.Columns.Add("Double Field").Tag = typeof(double);
  • Now, you can add the items as usual.

For example, the below list is sorted by the DateTime field.

Sortable ListView

Sortable ListView

 Click on the above image to download the SortableListView control.

PropertGrid Collection Events


You are using the PropertyGrid control to allow the user to edit the properties of an object at run-time from the UI. This works great and is in fact very reliable since this is the same control used by Visual Studio to display the properties of controls. Now that you are letting the user update the object directly, you’d like to get a PropertyValueChanged event whenever the user changes a property, so you can respond to the user’s action. You’ll be able to do that by adding a PropertValueChanged event to the PropertyGrid, which will get triggered every time a property is changed, unless the property is a collection. So if you have a collection inside the property grid selected object and you’d like to be notified when a property changes in that collection or when an item is added\removed, there is no way to do so with the OOB Microsoft PropertyGrid control. Clearly, the reason why the PropertyValueChanged event is not triggered when a collection is changed is because the reference to the collection object is still the same.

In order to be notified when a property changes inside a collection, follow instructions on this blog. You’ll find this very helpful. However, this solution can’t help you if you’d like to be notified when an item is removed from the collection or when it is added without any of its properties changed. A more generic solution I found was to get a notification when the collection form is closed. This way, I wouldn’t have to worry too much about the steps the user is taking in the collection editor. When the collection form is closed, I get notified and act on the collection object based on its current state.

Solution

First, add a reference to System.Design dll in your Visual Studio project. Create the following class, which represents a collection editor that triggers a MyFormClosed event when the collection editor form is closed.

using System;
using System.ComponentModel.Design;
using System.Windows.Forms;

public class MyCollectionEditor : CollectionEditor
{
    public delegate void MyFormClosedEventHandler(object sender,
                                        FormClosedEventArgs e);

    public static event MyFormClosedEventHandler MyFormClosed;

    public MyCollectionEditor(Type type) : base(type) { }
    protected override CollectionForm CreateCollectionForm()
    {
        CollectionForm collectionForm = base.CreateCollectionForm();
        collectionForm.FormClosed += new FormClosedEventHandler(collection_FormClosed);
        return collectionForm;
    }

    void collection_FormClosed(object sender, FormClosedEventArgs e)
    {
        if (MyFormClosed != null)
        {
            MyFormClosed(this, e);
        }
    }
}

To use the above collection editor for the property grid, you can simply add the following attribute above the collection Property of the object which you set to propertGrid.SelectedObject.

[Editor(typeof(MyCollectionEditor), typeof(UITypeEditor))]

The final part is to create our custom FormClosed event handler and bind it to our custom collection editor as follows:

public MyForm()
{
    InitializeComponent();

    //  Make the property grid listen to collection properties changes
    MyCollectionEditor.MyFormClosed += new MyCollectionEditor.MyFormClosedEventHandler
                                        (propertyGrid_CollectionFormClosed);
}

private void propertyGrid_CollectionFormClosed(object s, FormClosedEventArgs e)
{
    //  Code to run when collection form is closed
}

C# Regular Expression Helper


One component of an application I’m writing uses a lot of regular expressions. To be sure I was using the right regular expressions, I’ve created this simple tiny tool to help me learn and verify regular expressions against my input data. Nothing fancy, but it might help someone out there.

For example, it took me a little while trying to find the proper regular expression to match all comments (including those that are not closed) in an Xml file.

C# Regular Expression Helper

Download exe or source. I recommend using this tool with this cheat sheet. It’ll definitely speed up the learning curve.

Follow

Get every new post delivered to your Inbox.

%d bloggers like this: