"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.
 

Nedy

Member
Joined
Dec 26, 2021
Messages
10
Programming Experience
Beginner
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.
 

jmcilhinney

VB.NET Forum Moderator
Staff member
Joined
Aug 17, 2004
Messages
14,602
Location
Sydney, Australia
Programming Experience
10+
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.
 

jmcilhinney

VB.NET Forum Moderator
Staff member
Joined
Aug 17, 2004
Messages
14,602
Location
Sydney, Australia
Programming Experience
10+
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.
 

Nedy

Member
Joined
Dec 26, 2021
Messages
10
Programming Experience
Beginner
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.
 
Top Bottom