C
C#16mo 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
phaseshift16mo ago
there's a close event, no doubt Form.FormClosing
Zickles
Zickles16mo ago
does that only work in net 6?
phaseshift
phaseshift16mo ago
no
Zickles
Zickles16mo ago
well at least it is not working for me in net 7 rn it doesnt show it exists
phaseshift
phaseshift16mo ago
are you really using win forms? or wpf?
Zickles
Zickles16mo ago
Zickles
Zickles16mo ago
Zickles
Zickles16mo ago
does that mean that this hasnt work since 4.8.1 or is that something different
phaseshift
phaseshift16mo ago
it means it works up to netfx 4.8.1, and in 'net'/'net-core' from 3.0 to current
Zickles
Zickles16mo ago
well that means it will not work in .net 6 or 7 right?
phaseshift
phaseshift16mo 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
Zickles16mo ago
how do i check what net core i am on?
phaseshift
phaseshift16mo ago
Think its in the .csproj file what youre actually building for You can check whats installed with dotnet --list-sdks
Zickles
Zickles16mo ago
i still dont know why it is not working is the wrong template?
phaseshift
phaseshift16mo ago
theres a thing for this. What is it 🤔 $newproject
MODiX
MODiX16mo 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
phaseshift16mo ago
so yeah, you should be fine - you picked 'new' win forms app
Zickles
Zickles16mo ago
yea expect it does show or work only shows ActiveForm with i search for form
phaseshift
phaseshift16mo ago
doesnt mean much to me or what 'search' means
Zickles
Zickles16mo ago
i meant type
Zickles
Zickles16mo ago
phaseshift
phaseshift16mo 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
Zickles16mo 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
Kouhai16mo 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
Zickles16mo 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
Kouhai16mo ago
You mean opening a new one after the main form passed to Application.Run has been closed?
Zickles
Zickles16mo ago
yea
Kouhai
Kouhai16mo ago
iirc you can call Application.Run again
Zickles
Zickles16mo ago
that doesnt work
Kouhai
Kouhai16mo ago
Can you show the code for how you tested it?
Zickles
Zickles16mo 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
Kouhai16mo ago
I mean Let's step back a little bit Do you know how variables work in C#?
Zickles
Zickles16mo ago
yea
Kouhai
Kouhai16mo 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
Zickles16mo ago
you just want me to explain how it works?
Kouhai
Kouhai16mo 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
Zickles16mo 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
Kouhai16mo ago
Well, You'll have to learn to figure it out 😅
Accord
Accord16mo 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.