Monthly Archives: April 2011 - Page 2

Something cool about computers

The following is not really a technical post but some general comment and even amazement about a tool that is not exactly new – but I recently discovered the power of it.

I’ve knew about Google’s Picasa software for some time and also made use of it occasionally – the red-eye reduction tool is really good compare to some other free tools. Today I launched the tool again to quickly use a little feature or two but was prompted a new version is available. So I thought, awh well let it update itself while I do something else. After the update it started furiously to scan new picture folders that was added since I last ran the application – which could have been months ago for all I know. Anyway, I noticed something I did not see or was not aware of before. The ‘People’ tab on the left started showing faces of ‘unknown’ people. So I started playing with it adding a few names of me and my family. Next thing this thing starts added dozens/hundreds of pictures to the list where my and my family’s happen to be on the pictures.

Cool I thought, lets try to see how accurate it really is! Added some more names and checked the existing names for the photos it added automatically. Wow, it works really well. Sure it made mistakes – people that might look alike or the quality of the picture was bad to begin with – but overall it is working very well. Of course, the more pictures of a person you have the easier it is for the software to recognize someone. It did not take too long to get lost in the process and have hundreds or thousands of photos ‘tagged’ – and I have many thousands of photos I have taken over the last couple of years.

The point of this all? Even though I’m working each day with technology sometimes something simple like this can still amaze me. Sometimes we are so busy working with computers that we almost forget what they really can/should do for us. Often we (especially in IT) work ‘for’ the computer instead of it doing some work for us! This tool is one of those rare cases where a computer can do something amazing for me/us! Don’t forget how hard it is sometimes for us people to recognize faces – and we’re suppose to be good at it.

Now, it someone can just figure out how to get computers to properly respond to voice input – like KITT use to do, we can get rid of all these stupid keyboard and mice thingy’s 😉 Awh well, may by next decade…

Time

Time is actually more like an hourglass than you think…

As it passes there’s less of it than before and it seems to go faster

A wish…

If I could get money for having headaches I would be rich! Only trouble is then my headache would disappear!

Thoughts on health

Being a little bit sick lately – flu and so on, I cannot help thinking about the ‘health’ issue. It is one of those things that one only thinks about after you have lost it already.

A few days ago while driving back home after a day’s work I listened to a phone in show about the issue of anti-biotics. People were moaning about how bad they are and how doctors prescribed it too easily. It seems like more and more people are realizing that it is more an evil than any good. We are ‘breading’ super bugs because anti-biotics are only killing the weak ones leaving only the real bad ones behind – at least that is what I’m thinking these days. Have you noticed how flu’s are getting stronger and tougher lately to cure? It is only going to be worse – not better.

So what can we do to make sure we and our children stand a chance against these super bugs of tomorrow? That is the million Rand question (or is it billion Rand these days… with inflation the way it is these days… ).

Perhaps I should stop thinking… makes my head spin.

RTFBuilder

Every so often you come across a requirement that can be solved by an ‘old’ technology. Yes, us old ‘bru’s’ still can make a difference here and there… I needed a way to display formatted data that can also be easily edited, saved, printed, exported. Sure, html can be used as well but it is sometimes a shlep manipulating the data inside the application and it is sorta bloated. Years ago – even before html existed (or was widely used) there was a standard document format called Rich Text Format. It was compact, can be read/written by most word processors, and do lots of funky stuff. Even today Visual Studio has a control that specifically make use of this format.

Unlike html the rtf raw format isn’t exactly easy to read or understand but you don’t need to be an expert to use it. In VS you can simply use the the RTF control to programatically add and format text (on a side-note the format also allow embedding images and other stuff). This works fine for smallish documents. If however you need to create a big or long document you need to go ‘raw’ or use this library I discovered on Codeproject.  This library actually implements a way so you can build up a document similar to a StringBuilder – this makes it very fast. Using it is also fairly easy as it implements an Method based formatting concept. This is best explained with an example:

RTFBuilder rtfBuilder = new RTFBuilder();
//the following line adds a line of text using the font Courier New size 10
rtfBuilder.Font(RTFFont.CourierNew).FontSize(10).AppendLine("Some text");
//the following line adds a bold line of text
rtfBuilder.FontStyle(FontStyle.Bold).AppendLine("Some bold text");
//the following line write blue text on a black background
rtfBuilder.ForeColor(Color.Blue).BackColor(Color.Black).AppendLine("Blue on black");

//then if you want to see what the raw rtf text looks like
string rtfData = rtfBuilder.ToString();
//then finally you can set the rtftext of an RTFControl like this:
rtfControl.Rtf = rtfData;

Really simple hey?

So what is this good for? Well since the content is now inside an rtf control it can now be edited by a user – yes that is right, the rtf control allows for editing the content just like a plain old text box except you can also change the formatting – in fact, using the rtf control you can build a fully working word processor since that is what it was intended to do! Once you’re done editing or formatting the control allows for saving or easily exporting the content. Printing is still a bit tricky but even for that there are examples on the web. Remember that you can copy & paste formatted text from and to the rtf control as well. Now, try to do all that with html and a web browser control! hehe

Thoughts on history

  • We learn from history – that we don’t learn from history.
  • A man that ignores history is bound to make the same mistakes again.
  • Our children try so hard to be ‘not like us’ that they make all the same mistakes again…
  • Everything that has been will be again (except they haven’t found a fossilized Coke bottle yet…)
  • History is a story as told be the victors (oppose to those that lost or died)
  • There is a reason ‘history’ is spelled with 7 and ‘future’ with only 6 letters…

Another thought for the day

When things keep on going ‘right’ it usually means you’ve missed a critical part somewhere…

If things keep on going wrong it means you must stop or try something else…

Thought for the day – 8 Apr 2011

After listening to the radio on the way to work this morning about some policeman denying that a charge was filed against him this thought came up:

If you play with mud – expect to get dirty

Just a thought though…

ListViewColumnSorter

On my previous blog(the one on ‘that’ site that just went AWOL…) I had an article and download link to a class that I often used for making sorting easier in a ListView control. It drew some attention even with some people at Microsoft themselves.

Of course times have passed and even MS these days seems to be moving away from Winforms but I have yet to make any real effort to start moving to WPF – there are reasons for this… Anyway,  I’m sure there are people that still use Winform development and a class like this one could be very useful.

Functionality

Since the original I have made some changes (hopefully improvements). For those that don’t remember or missed it originally, here is a list of (the original) functionality that it provides:

  • It implements the standard IComparer interface that is required for making a custom class for sorting a ListView control (in detail view).
  • It defines some standard types of sorting (plain string, number, date, byte sizes, inverted strings and number/string mix)
  • It can automatically ‘bind’ to the column clicking event meaning you don’t have to write a single line of code to do the sorting stuff yourself (provided you use one of the provided standard sorting types)
  • It adds the little triangle icons to the clicked column header indicating sort direction.
  • It allows for disabling/enabling of sorting. This helps while you are loading the control and do not want the sorting/comparing routines firing for each insert!
  • Using it is very easy. All it requires to get started is a declaration to it and instantiation. That is 2 lines of code you have to write to use and bind it!

Since the original version I now have added the following functionality:

  • Added 2 new column ‘types’ that are not actually types but a way to add behavior. These are ‘Override’ and Deferred. More on the details on what they do later.
  • The ListView control has the ability to use ‘Groups’ so new functionality has been added to make use of it.

Usage

Like mentioned above to use it is really easy. All you need are ‘2’ lines of code like this:

private ListViewColumnSorter lvwSorter;

private void MainForm_Load(object sender, EventArgs e)
{

//The following lines are actually all one line wrapped for easier reading

lvwSorter = new ListViewColumnSorter(lvwMyListView, true,
SortColumnTypes.StringType,
SortColumnTypes.NumberType,
SortColumnTypes.DateType,
SortColumnTypes.ByteSize,
SortColumnTypes.Override,
SortColumnTypes.Deferred);

}

That is all that is required to get it up and running.

New functionality

Except for the ListView grouping functionality (which I’m not going to cover for now) there are two new related things that have been added to to the control – Override and Deferred sorting.

Deferred sorting

Sometimes you need to sort the ListView based on some other criteria than the displayed column that the user has clicked on. It could be that the displayed values are incomplete or that values in other columns might have to be taken in consideration as well. You may even want to do some external (to the ListView control) look-up in the sorting process. Deferred sorting is the solution to this problem.

The way this functionality work is by using delegates. This way the ListViewColumnSorter control effectively allows you to provide your own custom function that it will call during the sorting process. Keep in mind it will do this for every row in the ListView in the sorting process!

The delegate requires your method to accept 3 paramerters – column number, list view item 1 and list view item 2. As you can guess the column number is the column clicked on and the 2 list view items the items (rows) that must/can be compared. I often attach a custom class to the tag property of the list view item that contains more properties that what is displayed in the ListView. This way you can use the custom class as part of the comparison process. Yes, the ‘unboxing’ of the classes from type object like stored in the tag property does add some overhead so you’re not going to be using this on huge lists (not if you are doing some kind of complex sorting anyway!)

Example code

This example sample simply shows how the deferred method works. It must return the same -1/0/1 return value that the CompareTo method expect.

Somewhere (e.g. in the Form_Load event you can set:

lvwSorter = new ListViewColumnSorter(lvwListView, true, SortColumnTypes.Deferred, …);

lvwSorter.DeferredCompare = DeferredCompare;

Then somewhere else you can define the following method

private int DeferredCompare(int columnToSort, ListViewItem x, ListViewItem y)
{

if (columnToSort == 0)

{

CustomClass customClassX = (CustomClass)x.Tag;

CustomClass customClassY = (CustomClass)y.Tag;

return customClassX.SomeProperty.CompareTo(customClassY.SomeProperty);

}

else

return 0; //or whatever

}

Override

This is actually not really a way to sort the control at all but really to capture the column click event and call a custom method you provide and do anything else (including sorting or reloading the ListView control). This could be useful if you need to do some kind functionality that is too complex for sorting or got nothing to do with sorting at all.

As with deferred sorting this is done by delegates (actually an event in this case). You provide a method that the control will call when that column is clicked.

Example code

Somewhere in the Form_Load event you can set:

lvwSorter = new ListViewColumnSorter(lvwListView, true, SortColumnTypes.Override, …);

lvwSorter.OverrideColumnClicked += new OverrideColumnClickedDelegate(lvwSorter_OverrideColumnClicked);

Then you can define a method like this:

private void lvwSorter_OverrideColumnClicked(int column, SortOrder sortOrder)
{

if (column == 0)

{

DoSomethingElse();

}

}

Download

You can find a copy of the source code here.

 

My Codeplex projects

This post serves as both an introduction to my new blog and also to list some of my projects that I host on Codeplex.

The general projects are

Then I have some BizTalk related projects