Wednesday, August 19, 2009

 

Form.Load() override never called

I came across an issue today that boggled my mind for a few minutes. I had an apparently normal WinForm's Form, but the OnLoad() override was never getting called. Therefore, the Load event was never rasied.

As it turns out, there was nothing wrong with the form itself, but it was being used in an unconventional way. For some special-case reasons, the code that caused the form to be displayed was using PInvoke calls to SetWindowsPos() and ShowWindow() instead of calling the Form.Show() or Form.ShowDialog() method. Apparently, because neither Show() nor ShowDialog() was used, the OnLoad() method was never called.

My solution to this problem was to override the OnVisibleChanged() method, use a flag field to track whether the initialization code had been executed, and call the initialization code from whithin OnVisibleChanged if it had not yet been executed and Visble just changed to true. The flag, of course was then changed to true so the initialization code would not be called again if the form went invisible and then became visible again.

Thursday, August 13, 2009

 

Use Shift+Enter to insert '\n' characters using the Visual Studio resource editor

This is by no means something new. It's just something I always forget to do, and forget how to do, so I figured I would post it here so I know where to find it when I need it again.

I often use hard-coded strings for message boxes, UI strings, etc. when I initially write the code, and then go back later to add all of those hard-coded strings into resource files so that they can get localized. When adding such strings to resource files, one cannot simply cut and paste it from the source code to the resource file because of the embedded control characters.

The C# Compiler knows that when it sees "\n" in a string [ that does not start with '@' ] it should replace that two-character substring with the '\n' control character. However, the resource files are not so smart; the substring "\n" in a resource string is not converted to the '\n' control character automatically. But, you can insert a '\n' newline control character into the resource string by using Shift+Enter in the Visual Studio resource editor. (For some reason, I always want to do a Ctrl+Enter but that does not work).

This page is powered by Blogger. Isn't yours?

Subscribe to Posts [Atom]