"Close window" In Taskbar

Nedy

Member
Joined
Dec 26, 2021
Messages
10
Programming Experience
Beginner
I have an app with 2 forms (main and help.) And when I click on a menu item in the main form a (modeless) help form is visible.

When I close the help form I want the main form to have the focus and enable the help menu item. And this works when I use the help form's close (such as clicking on the X button.) But it doesn't work when I right click on the help form in the taskbar and choose "Close window".

I've tried various things to fix this (form activate(), lostfocus, putting the code in form closed.) But the only way I can get the focus to go back onto the first form (after doing the right click) is if I put a msgbox in the closing sub.

VB.NET:
Private sub Form2_closing (sender As Object, e As FormClosingEventArgs) Handles Me.Closing

msgbox ("Closing.")
mainform.mnuhelp.Enabled = True

End Sub

But I don't want a msgbox in my sub. So can someone please tell me how to solve this?

Thank you.
 
When I use this code and do the right click/"Close window" thing, Form1 does not get the focus.
All you do in Help_FormClosed is to enable a menu, so why should Form1 also activate? Call the Activate method too.
 
When I use this code and do the right click/"Close window" thing, Form1 does not get the focus.
There's no code there doing the focusing. If you want the current form to take focus when the other form closes, the logical thing to do is to execute code to focus the current form when the other form closes. You know where that is because you have code there to enable the menu item. Add code to focus the form too. Because no one ever reads the documentation and tends to just call Focus, I will state that you call Activate to focus a form.

I'm assuming that the fact that you have clicked on the task bar means that it is now the window with focus, so hopefully explicitly activating the form occurs at the right time to address that.

PS. Wrote this yesterday but neglected to actually submit it.
 
Here's what I did. I tried to find out what a method is. Apparently a sub procedure and a function are methods. But then other websites talked about other methods that are only one line of code. So I found out what the Activate method line of code was and put it in my code....

VB.NET:
Private Sub Help_FormClosed(sender As Object, e As FormClosedEventArgs) Handles Help.FormClosed

Me.Activate()

HelpToolStripMenuItem1.Enabled = True

End Sub

Which I think I tried before and it didn't work. But I didn't really know what I was doing so let's just forget that I said that. Anyway, it's still not working. The first time I right click + "Close window" sometimes Form1 is activated and sometimes Form1 is not activated.

I also tried googling "Close window" to try and find out why this way of closing a window is different to "Close" but I came up with nothing. Maybe I'm missing something..... Again.

Most importantly, thanks guys for sticking with me. I can understand that working with me can be difficult, especially when my programming knowledge is worlds apart from yours. So thank you for your persistence and also your patience. It certainly is appreciated.
 
It may be that the event is not raised at the right time and focus shifts after that code is executed. I'm back home now so I'm in a position to write some code and do some testing of my own.
 
I have been unable to reproduce your issue so far. I'm on Windows 11, so I'm not sure whether that is a factor. I also targeted .NET Framework 4.8. What's your target?

On another note, I would suggest that you don't disable the menu item at all. Just leave it active and, in the Click event handler, call Show and Activate on the second form. That will ensure that it is displayed if it's not already and focused if it is.
 
It may be that the event is not raised at the right time and focus shifts after that code is executed.

There's another time the event can be raised?

I have been unable to reproduce your issue...

I created my app using...

Windows Forms App
A project template for creating a .NET Windows Forms (WinForms) App.
Target Framework = .NET 5.0 (Current)

I also created a new app on another Win10 computer, and I think the framework was 4.7.2 (or something like that) but it definitely wasn't 5.0 and the same problem appeared. So maybe it's a Win10 problem.

I would suggest that you don't disable the menu item at all...

You mean like this.....?

VB.NET:
Private Sub HelpToolStripMenuItem1_Click(sender As Object, e As EventArgs) Handles HelpToolStripMenuItem1.Click

Help = Form2
Form2.Show()
Me.Activate

End Sub

It's a good suggestion, but... Well, by now I think you know how this sentence ends. And in the new app that I created, I used a button instead of a menu item but I still got the same problem.

So my question is..... Can we identify when the right click + "Close window" happens?

Yesterday I discovered a thing in Visual Studio that said something like "reason for closing" and I tried that but it didn't work. It kept giving me the same response, something like "User Closed Form".

But then I found this...

WM_SYSCOMMAND and SC_CLOSE

And then I found this...

SC_CLOSE = 0xF060

And...

SendMessage (iHandle, WM_SYSCOMMAND, SC_CLOSE, 0);

And these, I think, can help me work out if a right click happens. But I need to find out more later on. Because I needed a break. So that's where I'm at right now. Thanks jmcilhinney.
 
There's another time the event can be raised?
It's not about time but about order. Sometimes a particular action will cause multiple events to be raised in an order such that something you do on one is effectively cancelled out by something that is done on a later one. I can't think of a specific example off the top of my head but I have seen it happen. That may or may not be the case here.
You mean like this.....?

VB.NET:
Private Sub HelpToolStripMenuItem1_Click(sender As Object, e As EventArgs) Handles HelpToolStripMenuItem1.Click

Help = Form2
Form2.Show()
Me.Activate

End Sub
No, I mean like what I actually said:
call Show and Activate on the second form. That will ensure that it is displayed if it's not already and focused if it is.
What would be the point of activating, i.e. focusing, the current form immediately after showing the second form? The words I actually wrote were showing and activating the second form. If the form is not already shown, it will be shown. If it is already shown, it will be activated, i.e. it will receive focus. This is how many applications work with secondary windows. Basically, you're are ensuring that the desired form is visible and focused, no matter its current state.
 
What I have seen in testing is that, if you focus a window outside the application, then focus the second window, then close the second window, it is the external window that takes focus rather than the first window in the application. I was able to override that behaviour by explicitly activating the first window when the second window closed. This was true when targeting both .NET Framework 4.8 and .NET 5.0. You can test for yourself like I did by following the instructions below:
  • Create a new Windows Forms Application project.
  • Add a second form to the project.
  • Add a MenuStrip to the first form and add a menu item with the text "Help".
  • Add the following code to the first form:
VB.NET:
Public Class Form1

    Private WithEvents f2 As Form2

    Private Sub HelpToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles HelpToolStripMenuItem.Click
        f2 = Form2
        Form2.Show()
        Form2.Activate()
    End Sub

    Private Sub f2_FormClosed(sender As Object, e As FormClosedEventArgs) Handles f2.FormClosed
        Activate()
    End Sub

End Class
  • Run the project.
  • Click the Help menu item.
  • Activate an external window, e.g. a Notepad window.
  • Activate the second form in your application.
  • Close the second form in your application.
You should see the first form in your application receives focus. If you comment out the Activate call and do the same thing again, you should see the external window receive focus. This should be the same regardless of how you close the form. If you right-click a window in the Task Bar, that will bring up the same context menu as would be displayed if you clicked the icon on the window's title bar. The Close menu item there invokes exactly the same code as the Close button on the title bar.
 
We need to get rid of that Win11 computer and replace it with a Win10 computer because that code didn't work. But thanks for trying. And I think I understood some of the other stuff you said too. So it's not all bad.
 
Back
Top