Trimming Text After Label Edit

ListView is beautiful control. Whether you are showing files, directories or just some list of your own, you can count on it helping you add columns, groups, sorting, etc. It even allows for rename.

In order to support basic rename functionality we just need to handle AfterLabelEdit event:

private void listView_AfterLabelEdit(object sender, LabelEditEventArgs e) {
    if (e.Label == null) { return; }
    //do something with e.Label (e.g. rename file)
}

But what if we want to ignore whitespaces after the text?

For example, every time somebody tries to rename file to " My new name " we should actually remove all that extra spacing and use “My new name”. Obvious solution would be to do e.Label = e.Label.Trim(). However, that code does not even compile. Time for tricks…

If we detect whitespace we can just cancel everything. That will prevent ListView from making any changes to our item. Then we can be sneaky and change item ourselves:

private void listView_AfterLabelEdit(object sender, LabelEditEventArgs e) {
    if (e.Label == null) { return; }
    var trimmed = e.Label.Trim();
    if (!(string.Equals(trimmed, e.Label))) {
        e.CancelEdit = true;
        listView.SelectedItems[0].Text = trimmed;
    }
    //do something with variable trimmed
}

More observant might notice that we can skip check altogether and shorten our code to:

private void listView_AfterLabelEdit(object sender, LabelEditEventArgs e) {
    if (e.Label == null) { return; }
    var trimmed = e.Label.Trim();
    e.CancelEdit = true;
    listView.SelectedItems[0].Text = trimmed;
    //do something with variable trimmed
}

Full sample is available for download.

PS: For homework find why we need (e.Label == null) { return; } at start of an AfterLabelEdit event handler?

QText 3.40

QText screen

Biggest improvement in QText 3.40 comes in area of printing. Code that traces its origins probably from very first (text-only) version of QText was finally adjusted to properly support Rich Text Format.

With ever increasing number of folders there was need for some sort of navigation helper. Pressing Ctrl+G will enable you to quickly search documents by name. That should make searching for particular document among folders a breeze.

Those using QText for journal will be happy to know that Alt+Shift+D and Alt+Shift+T are finally supported. Upon press you will get current date and time inserted into a document.

In addition to general bug-fixing that was done I think that everyone should have a reason or two for upgrade.

Parsing UTC Time

It all started with simple requirement: there is UTC time string (e.g. 21:00) and you should parse it into UTC DateTime. Simplest code ever:

var time = DateTime.ParseExact(text, "HH:mm",
                               CultureInfo.InvariantCulture,
                               DateTimeStyles.NoCurrentDateDefault);

Only issue is that it does not work. While it looks good on first look, more careful investigation shows that its type is not Utc but Unspecified. While this might not seem as a major pain, it does make a difference.

Let’s assume that you send proper UTC time to some function that does time.ToUniversalTime(). Function will just return exactly same time back. If you do same with Unspecified time, function will adjust it for time zone and you will get back completely different time than what you started with.

Well, fixing this is easy, just add AssumeUniversal flag:

var time = DateTime.ParseExact(text, "HH:mm",
                               CultureInfo.InvariantCulture,
                               DateTimeStyles.NoCurrentDateDefault
                             | DateTimeStyles.AssumeUniversal);

Unfortunately it does not work. While description says “If no time zone is specified in the parsed string, the string is assumed to denote a UTC”, flag actually causes our time to be Local. Weird.

To properly parse time you need one more flag:

var time = DateTime.ParseExact(text, "HH:mm",
                               CultureInfo.InvariantCulture,
                               DateTimeStyles.NoCurrentDateDefault
                             | DateTimeStyles.AssumeUniversal
                             | DateTimeStyles.AdjustToUniversal);

This will finally cause our time to be of Utc kind.

Code sample is available.

More Ohms Please

One of first things that you might learn as electronic hobbyist is that you should have 20 mA running through your indicator LEDs. For example, if you have 5 V power supply and your LED has voltage drop of 2V at 20 mA just give it 150 ohm resistor and you are golden.

And that is wrong. This recipe was valid 20 years ago but it is not something you should follow these days. If you have 20 mA coursing through LED, that is BRIGHT as hell. When you work in dark, every look at such device blinds you for minutes.

Don’t misunderstand me, I like to see some indication that device is alive but 1 mA or less should do the trick. These new LEDs are extremely efficient with their output and, while they still specify their current at 20 mA (or even higher), they will be bright enough at lower currents also.

PS: This rant is only about indicator LEDs; if your device needs to light-up the room, so be it.

Do I Have This Certificate?

For one build script I had to know whether certain certificate is present. It took me a while until I found Certutil. Assuming that you know a hash of desired key (and you should) command is simple:

CERTUTIL -silent -verifystore -user My e2d7b02c55d5fe76540bab384d85833376f94c13

In order to automate things you just need to extend it a bit to check exit code:

CERTUTIL -silent -verifystore -user My e2d7b02c55d5fe76540bab384d85833376f94c13
IF ERRORLEVEL 1 ECHO No certificate found.

All nice-and-dandy except that it does not work. For some reason Certutil always returns exit code 0 regardless of success. But not all is lost, command does set ERRORLEVEL environment variable (not the same thing as exit code):

CERTUTIL -silent -verifystore -user My e2d7b02c55d5fe76540bab384d85833376f94c13
IF NOT %ERRORLEVEL%==0 ECHO No certificate found.