❔ how to detect when you close the window
how can i check when you close your windows form and so i can run some code off it when it is closed
39 Replies
there's a close event, no doubt
Form.FormClosing
does that only work in net 6?
no
well at least it is not working for me in net 7 rn it doesnt show it exists
are you really using win forms?
or wpf?
does that mean that this hasnt work since 4.8.1 or is that something different
it means it works up to netfx 4.8.1, and in 'net'/'net-core' from 3.0 to current
well that means it will not work in .net 6 or 7
right?
no...
it means as long as youre not earlier than net core 3, then it works on current .net platform (+ windows desktop)
'5,6,7,8' from your image = .net 5 thru .net 8
how do i check what net core i am on?
Think its in the .csproj file what youre actually building for
You can check whats installed with
dotnet --list-sdks
i still dont know why it is not working
is the wrong template?
theres a thing for this. What is it 🤔
$newproject
When creating a new project, prefer using .NET over .NET Framework, unless you have a very specific reason to be using .NET Framework.
.NET Framework is now legacy code and only get security fix updates, it no longer gets new features and is not recommended.
https://cdn.discordapp.com/attachments/569261465463160900/899381236617855016/unknown.png
so yeah, you should be fine - you picked 'new' win forms app
yea expect it does show or work
only shows ActiveForm with i search for form
doesnt mean much to me
or what 'search' means
i meant type
events arent* static
What you're doing there doesn't make sens
you need to use the instance you created, and hook up the events from that
do you have any exmaple you know i could look at?
whatever i dont really understand how this works so i will figure it out some itime i just dont know how to do this
well do you think you could further explain what you mean by this?
Here you're creating a new instance of Form1 and passing it to Application.Run
If you want to subscribe to an event, you need to an instance of Form1
So something like
var form = new Form1()
form.On +=
thank you
also one more question is there a way i can open another form like the way above
or is it not possible to open it again when you close it
You mean opening a new one after the main form passed to Application.Run has been closed?
yea
iirc you can call Application.Run again
that doesnt work
Can you show the code for how you tested it?
I mean
Let's step back a little bit
Do you know how variables work in C#?
yea
Okay, in the snippet you have sent, can you explain this part
you just want me to explain how it works?
Yes, I want you to explain the call to Application.Run
var form = new Form1();
I know this might seem a bit dumb on my part, but I think it's better to fully grasp these basics first 😅eh whater i dont feel like doing this i will deal with this some other time
i mean i want to figure it out i am just too lazy to sorry
Well, You'll have to learn to figure it out 😅
Was this issue resolved? If so, run
/close
- otherwise I will mark this as stale and this post will be archived until there is new activity.