Bingo

Strange thing just happened. I got same one-time password two times. First one I got in November last year and second one I received just now. Some would call it an error but I call it a warning.

Run away! Laws of physics do not apply here. Escape while you can.

P.S. I already heard best joke about this from friend in office: “Only Chuck Norris and you can get same one-time password twice.”

ToolStrip as Main Menu

Let’s take small project as an example. You need two or three options and you cram everything as root menu item. Since you like your users and your users like pictures, you additionally create toolbar with same options. Final toolbar has one-to-one mapping with menu. Now question is why do we need menu in first place?

Personally I see only one reason - keyboard accessibility. Toolbar might be nice but it will not allow us to use pure keyboard interface in order to select items. However, solution is simple. First we set KeyPreview=true on form itself and then we just handle Alt key:

private void MainForm_KeyDown(object sender, KeyEventArgs e) {
    switch (e.KeyData) {
        case Keys.Alt | Keys.Menu: {
            menu.Select();
            menuNew.Select();
            e.SuppressKeyPress = true;
            e.Handled = true;
        } break;
    }
}

This code acts upon Alt key press. First we need to select ToolStrip control and then we select first item (New, in this case) within it. If we just use Select on item it will look same, but keyboard arrows will not move selection between items.

Downside to this is handling shortcut keys. While MenuItem will allow us to set Ctrl+N as shortcut key same is not true for ToolStripButton. All shortcuts should be handled explicitly now (within MainForm_KeyDown event):

···
case Keys.Control | Keys.N:
    menuNew_Click(null, null);
    e.SuppressKeyPress = true;
    e.Handled = true;
    break;
···

Full code example is available here.

Everything Bothers IE

Illustration

Internet Explorer is not my primary browser anymore but I do use it occasionally. Of course I installed IE 9. And it got me angry.

First it complained about open programs that are preventing installation progress. I think that it basically just listed all processes running at my computer. What Visual Studio has to do with IE installation is everybody’s guess. And, of course, installation required restart. Why?

P.S. Once install is over, Internet Explorer 9 becomes pretty decent browser. It is quite similar to Chrome in philosophy and that is good thing.

Dirty Docs

Illustration

I usually like Google Docs. They are not full-blown editing tool and they will not replace Microsoft Office for me (at least not soon) but they offer quite a lot when it comes to editing documents from various locations. Your document goes where your browser is.

For reasons I will not get into, I had to do some automatic processing on one of my Google Docs textual documents. I created it with care, all headings were properly defined and most of text was of “Normal” style. It was made with clean export into HTML in mind.

Unfortunately export result was quite far from clean code. First thing is that all is in one line. Yes, this saves few bytes but it is pain in the ass if you need any manual editing of this document. Fortunately PSPad knows how to expand such code.

Biggest issue I have here is that everything is set via style-sheets. While I usually agree with that, Google overdid it this time. They added bunch of span tags all over place. Even when you have just “Normal” text, you can be sure that it will not stand without around it. Event bolds and italics will not get just and but they will have full-blown CSS definition.

I agree that this is not an issue if you just want to view it. However this makes any automatic processing of text real pain-in-the-ass. It definitely brings back memory of Microsoft FrontPage and it’s html mess.

P.S. No, ODT export is not solution - it is even bigger and dirtier.

Not All Files Can Be Embedded

Quite a lot of helper files needed for average applications are best stored as resource. This way separation of data and code is kept on logical level but everything gets stored in one executable so nothing can be lost.

In order to make some file resource only thing needed is selecting “Properties” from context menu and setting “Build Action” to “Embedded Resource”. Reading this from code is equally easy (assuming program is called MyProgram and resource file is called Test.txt):

Stream myStream = Assembly.GetExecutingAssembly().GetManifestResourceStream("MyProgram.Test.txt");

It is easy as it can be and I was very surprised when I could not get this to work. I added file “File.hr.xml” to project and I embedded it correctly but GetManifestResourceStream method returned null for it. Documentation says that this can only happen when resource is not there and this was impossible in my case.

In order to confirm this I looped through all resources I had embedded:

foreach (string name in Assembly.GetExecutingAssembly().GetManifestResourceNames()) {
    Debug.WriteLine(name);
}

To my surprise documentation was correct, my file simply wasn’t there. However, in my bin folder there was subfolder named “hr” with “MyProgram.resources” file inside.

Then it hit me. Problem was in multi-dotted extension with valid language code as first part. These files are understood by Visual Studio as being language specific and reading them from another culture is not possible (ok, it is possible but not easy).

Since I really wanted this resource to be available regardless of localization, solution was simple. I just renamed file to “File-HR.xml” and it magically appeared in resource list.