"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
Thanks for replying jmcilhinney but where exactly do I, and how can I, get the first form to handle the second form's formclosed event?
 

jmcilhinney

VB.NET Forum Moderator
Staff member
Joined
Aug 17, 2004
Messages
14,717
Location
Sydney, Australia
Programming Experience
10+
The same way you handle any event of any object. You can either assign the object raising the event to a field declared WithEvents and include it in the Handles clause of a method or you can use an AddHandler operator where you create the object.
 

Nedy

Member
Joined
Dec 26, 2021
Messages
10
Programming Experience
Beginner
Thank you again jmcilhinney for trying to help.
However I'm a beginner and programming is very difficult for me.
So could you help me by providing some code to get me started?
 

jmcilhinney

VB.NET Forum Moderator
Staff member
Joined
Aug 17, 2004
Messages
14,717
Location
Sydney, Australia
Programming Experience
10+
I'm using my phone while on holidays so writing code is not really practical. I've given you some pointers and some keywords so you can search the web from that and find the relevant information. That should be your first step whenever someone gives you new information.
 

Nedy

Member
Joined
Dec 26, 2021
Messages
10
Programming Experience
Beginner
Yes I've already spent hours searching the Internet (before and after I started this thread) and I still couldn't work it out.

jmcilhinney, let me be frank, you're talking to an idiot when it comes to programming. To be honest, I'm even more confused now than before as a result of your posts. So if we're going to arrive at an answer, you're going to have to speak to me like a child who is taking their first steps. Programming doesn't come naturally to me and yet despite this I am determined to get an answer. I just need a smart person like you to help me. So, if you're okay with that, here's my first question for you....

You say that the first form should be handling the FormClosed event of the second form. So my question is.... Are you expecting me to create a new sub in form1's code?
 

JohnH

VB.NET Forum Moderator
Staff member
Joined
Dec 17, 2005
Messages
15,603
Location
Norway
Programming Experience
10+
Here you'll find examples of both: Events - Visual Basic
Simplest when you have just one object is to declare a WithEvents field and assign that to it. Then you set up the event handler similar to your code in post 1.
Are you expecting me to create a new sub in form1's code?
Yes. After you declared the WithEvents field you can select it in top center list in code window then select the event in top right list.

1640740661258.png


1640740641520.png
 

Nedy

Member
Joined
Dec 26, 2021
Messages
10
Programming Experience
Beginner
Thanks for the help John, I'm pretty sure I understand what I have to do now, and here's what I tried....

VB.NET:
Public Class frmaaa
WithEvents Help As Form = Form2

VB.NET:
Private Sub Help_FormClosed(sender As Object, e As FormClosedEventArgs) Handles Help.FormClosed
msgbox ("Form2 is closed")
End Sub

But unfortunately this doesn't fix the problem.

The first time I open/close Form2 (using X close or right click on the taskbar + "Close window") I see a message. But, the second time I open/close Form2 I don't see a message.

So what am I doing wrong now?
 
Last edited:

JohnH

VB.NET Forum Moderator
Staff member
Joined
Dec 17, 2005
Messages
15,603
Location
Norway
Programming Experience
10+
When you close the form it is disposed so next time you're showing a new form, while the Withevents variable is still refering to the old one. Just assign the new form instance to the Withevents variable before showing the form.
 

Nedy

Member
Joined
Dec 26, 2021
Messages
10
Programming Experience
Beginner
Just assign the new form instance to the Withevents variable before showing the form.

Thanks for explaining that John. Now, I'm sorry to say this, but the above quote doesn't make sense to me.

But I do know that I don't use a "new form instance" in my code. So can you explain this part a bit more please?
 

jmcilhinney

VB.NET Forum Moderator
Staff member
Joined
Aug 17, 2004
Messages
14,717
Location
Sydney, Australia
Programming Experience
10+
But I do know that I don't use a "new form instance" in my code. So can you explain this part a bit more please?
No, you don't know that, because it's not possible. Maybe if you showed us the code that displays the form, we could tell you how to modify that code to make it work. ALWAYS show us the relevant code. Most likely you are using the default instance, so that's what you need to assign to the field. We shouldn't have to guess though.
 

JohnH

VB.NET Forum Moderator
Staff member
Joined
Dec 17, 2005
Messages
15,603
Location
Norway
Programming Experience
10+
I do know that I don't use a "new form instance" in my code
You are using the default form instance, which takes care of creating a new instance whenever you need one, and returns the same instance until it is disposed. Before you call the Show method just assign it to the WithEvents field Help = Form2
 

Nedy

Member
Joined
Dec 26, 2021
Messages
10
Programming Experience
Beginner
I made a new solution/project and added the relevant code....

VB.NET:
Public Class Form1

WithEvents Help As Form = Form2

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

Help = Form2

Form2.Show()

HelpToolStripMenuItem1.Enabled = False

End Sub

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

HelpToolStripMenuItem1.Enabled = True

End Sub

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load

Me.Show()

End Sub

End Class

Public Class Form2

Inherits System.Windows.Forms.Form

Private Sub Form2_Load(sender As Object, e As EventArgs) Handles MyBase.Load

Me.Top = 400

End Sub

End Class

When I use this code and do the right click/"Close window" thing, Form1 does not get the focus.

PS... jmcilhinney, I do not appreciate being yelled at, if you want some code all you have to do is ask for it in a respectful and civil way.
 

jmcilhinney

VB.NET Forum Moderator
Staff member
Joined
Aug 17, 2004
Messages
14,717
Location
Sydney, Australia
Programming Experience
10+
PS... jmcilhinney, I do not appreciate being yelled at, if you want some code all you have to do is ask for it in a respectful and civil way.
I fail to see the relevance, given that I didn't yell. As for respect, it works both ways. Why would you think that asking for help with code while not providing that code would be a good idea? I can't see how anyone who takes the time to think about it would come to that conclusion, which means that a great many people just don't think about it. They come here once in a while and only think about what they want, without considering how they might help us to help them.
 

JohnH

VB.NET Forum Moderator
Staff member
Joined
Dec 17, 2005
Messages
15,603
Location
Norway
Programming Experience
10+
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.
 

jmcilhinney

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

Nedy

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

jmcilhinney

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

jmcilhinney

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