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

Latest posts

Back
Top