C
C#2y ago
Zickles

❔ 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
phaseshift
phaseshift2y ago
there's a close event, no doubt Form.FormClosing
Zickles
ZicklesOP2y ago
does that only work in net 6?
phaseshift
phaseshift2y ago
no
Zickles
ZicklesOP2y ago
well at least it is not working for me in net 7 rn it doesnt show it exists
phaseshift
phaseshift2y ago
are you really using win forms? or wpf?
Zickles
ZicklesOP2y ago
Zickles
ZicklesOP2y ago
Zickles
ZicklesOP2y ago
does that mean that this hasnt work since 4.8.1 or is that something different
phaseshift
phaseshift2y ago
it means it works up to netfx 4.8.1, and in 'net'/'net-core' from 3.0 to current
Zickles
ZicklesOP2y ago
well that means it will not work in .net 6 or 7 right?
phaseshift
phaseshift2y ago
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
Zickles
ZicklesOP2y ago
how do i check what net core i am on?
phaseshift
phaseshift2y ago
Think its in the .csproj file what youre actually building for You can check whats installed with dotnet --list-sdks
Zickles
ZicklesOP2y ago
i still dont know why it is not working is the wrong template?
phaseshift
phaseshift2y ago
theres a thing for this. What is it 🤔 $newproject
MODiX
MODiX2y ago
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
phaseshift
phaseshift2y ago
so yeah, you should be fine - you picked 'new' win forms app
Zickles
ZicklesOP2y ago
yea expect it does show or work only shows ActiveForm with i search for form
phaseshift
phaseshift2y ago
doesnt mean much to me or what 'search' means
Zickles
ZicklesOP2y ago
i meant type
Zickles
ZicklesOP2y ago
phaseshift
phaseshift2y ago
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
Zickles
ZicklesOP2y ago
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?
Kouhai
Kouhai2y ago
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 +=
Zickles
ZicklesOP2y ago
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
Kouhai
Kouhai2y ago
You mean opening a new one after the main form passed to Application.Run has been closed?
Zickles
ZicklesOP2y ago
yea
Kouhai
Kouhai2y ago
iirc you can call Application.Run again
Zickles
ZicklesOP2y ago
that doesnt work
Kouhai
Kouhai2y ago
Can you show the code for how you tested it?
Zickles
ZicklesOP2y ago
static void Main()
{
ApplicationConfiguration.Initialize();
Application.Run(new Form1());

var form = new Form1();
form.FormClosing += Form_FormClosing;
}

private static void Form_FormClosing(object? sender, FormClosingEventArgs e)
{
Application.Run(new Form1());
}
static void Main()
{
ApplicationConfiguration.Initialize();
Application.Run(new Form1());

var form = new Form1();
form.FormClosing += Form_FormClosing;
}

private static void Form_FormClosing(object? sender, FormClosingEventArgs e)
{
Application.Run(new Form1());
}
Kouhai
Kouhai2y ago
I mean Let's step back a little bit Do you know how variables work in C#?
Zickles
ZicklesOP2y ago
yea
Kouhai
Kouhai2y ago
Okay, in the snippet you have sent, can you explain this part
Application.Run(new Form1());

var form = new Form1();
form.FormClosing += Form_FormClosing;
Application.Run(new Form1());

var form = new Form1();
form.FormClosing += Form_FormClosing;
Zickles
ZicklesOP2y ago
you just want me to explain how it works?
Kouhai
Kouhai2y ago
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 😅
Zickles
ZicklesOP2y ago
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
Kouhai
Kouhai2y ago
Well, You'll have to learn to figure it out 😅
Accord
Accord2y ago
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.
Want results from more Discord servers?
Add your server