2

All, I am over half way through a MDI WinForm application and have started testing what we have so far on several different machines and I have found the following error on all machines (running Windows 7/Vista). The error is to do with the TaskBar and the visably active state of the application within it. This will be easier with images:

A. I hover over the Task Bar ready to launch.

Hovering over the application icon (mouse not displayed)

B. I click on the application and it launches no problem and shows in the Task Bar - all good so far.

Launches with no problem

C. Wait for perhaps three seconds and the icon fades away to not being active, so that when I click on it, it launches another instance of my application instead of restoring the one I have just opened!? Once I click on the form and minimise and then restore the form this problem goes away!?

Active icon on the Task Bar dies a death

This is strange and I have no idea how to go about fixing it. This also manifests itself when using the .exe file to launch the application, where the task bar does not show at all until you click on the applications main form. I am truly stumped. If anyone has any idea about what could be happening here, it would be most appreciated? Thanks for your time.

Note. I have narrowed the problem down to the form being launched behind other open forms. I have used the relevent options this.TopLevel = true; in the forms Shown event etc. but nothing seems to work...

MoonKnight
  • 23,214
  • 40
  • 145
  • 277

2 Answers2

2

All,

This was solved by adding

this.Activate();

in the finally block of the main form's (the MDI parent form's) Shown event.

Thanks for your time and help.

MoonKnight
  • 23,214
  • 40
  • 145
  • 277
  • 2
    Hmm, that's a workaround, not a solution. You need to fret about this, there are other things that can go wrong when you have to write code like this. Start by heavily testing the Windows + L key (lock workstation) and make sure your UI doesn't deadlock. Use the built-in support for splash screens in .NET, if that's the cause. – Hans Passant Apr 16 '12 at 19:13
  • @HansPassant thanks for the advice. I am unsure what the issue could be at this stage. I have now developed many MDI applications and not once come across this behaviour. I will pay heed to the above advice. If 'Windows + L' does not cause any issues is it safe to assume thay I am out-of-the-woods? I am using my own Splash-Screen but I have used the method before with no side-effects. Again, thanks for the comment it is appreciated... – MoonKnight Apr 16 '12 at 20:12
  • "I'm using my own splashscreen" is what I guessed. It is a very strong hint that you are papering over a real problem. – Hans Passant Apr 16 '12 at 21:22
  • I have developed a splash screen that works fine with other applications so there is no suggestion that this is what is causing this problem. I will remove it and see what happens... – MoonKnight Apr 16 '12 at 21:32
  • @HansPassant just to let you know (if your interested). I removed the `Activte()` keyword from the above expecting the problem to return and it didn't!? I have checked this on other machines (that had the problem before I put the `Activate()` keyword in) and these now also work!? I have removed the splash screen and it makes no difference (this test is now clearly redundent). It seems to me that the code behind (graphics stuff) could have contained a bug that was fixed (for good) via the inclusion of the `Activate()` keyword. – MoonKnight Apr 18 '12 at 09:55
  • As a side note this behavior is similar to that of an application that takes forever to load. When you click the icon on the taskbar is 'lights up' for 5 seconds or so before fading away until that application finally responds and loads, is your application doing anything with the respondence or focus of applications? – edhurtig Apr 23 '12 at 03:07
  • It is loading about five MDI children, but this should not be the problem. However, I am doing something different with the MDI forms. Istead of having them all in the same class and using a Tab Control I am using the tab control Click event to show the relevent form to allow me to make the code more modular (as we are sharing the code quie a bit and the first approach is messy). I am not sure why (see the comment above) but this behaviour is now gone, even with out the Focus() statement - although this is what seemed to have fixed it. Very odd... – MoonKnight Apr 23 '12 at 07:25
2

I saw a similar issue where an application would not take focus when it was first launched and would not show in the windows 7 taskbar until you clicked on it to activate the main form.

I was never able to track down the real cause but moving the setting of the form's Text property from an OnLoad override to the constructor made the issue go away. Very strange and it still bugs me that I don't know why it was happening.

Not sure if this is applicable to you, but I would be very interested to know if you are also setting Text outside of the constructor.

roken
  • 3,946
  • 1
  • 19
  • 32
  • Thanks for the post. I am still developing and was hopefull that someone would have a set solution and that this would be a known issue. This might not be the case! As I slow down in the development I will start some detailed testing which will incorporate your suggestion above. Thanks for your time, I will keep you posted.... – MoonKnight Mar 30 '12 at 13:24
  • One this I do in the LOAd event is load MDI children, I will test the significance of this and let you know... – MoonKnight Mar 30 '12 at 13:25