RSS Feed

Reloading ListBox DataSource

My Little project TimeIsMoney is participating in a contest by Maciej Aniserowicz.

At the moment it’s really simple tool used to help me with collecting and sorting various tasks.

I’m doing this just for fun. This contest will be a great motivation and good way to promote this blog 😛

So what’s interesting in the code right now ?

1. ListBox DataSource Reloading

    public static class Extension
    {
        public static void eReloadDataSource(this ListBox listbox)
        {
            string s = listbox.DisplayMember;
            Object obj = listbox.DataSource;
            listbox.DataSource = null;
            listbox.DataSource = obj;
            listbox.DisplayMember=s;
        }
    }

This Extension method reloads the Data source of a ListBox. I know it looks stupid but it works :D.
Let’s take a look in Reflector …. hmm

public object DataSource
{
    [TargetedPatchingOptOut("Performance critical to inline this type of method across NGen image boundaries")]
    get
    {
        return this.dataSource;
    }
    set
    {
        if (((value != null) && !(value is IList)) && !(value is IListSource))
        {
            throw new ArgumentException(SR.GetString("BadDataSourceForComplexBinding"));
        }
        if (this.dataSource != value)
        {
            try
            {
                this.SetDataConnection(value, this.displayMember, false);
            }
            catch
            {
                this.DisplayMember = "";
            }
            if (value == null)
            {
                this.DisplayMember = "";
            }
        }
    }

So by assigning null value i m calling the SetDataConnection(,,,) which reloads the data. Also you can easily see that we have to reasssign DisplayMember value beacuase it is set to an Empty String at the end.

2. GlobalKeyHook

To catch the key press events i am using slightly modified code from link . I made a little tweak to enable the special keys combinations , so you can catch the alt+ctrl+b sequention.

That’s all for now ;] If you want to check the code it is on the GitHub.

Advertisements


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s