9 Replies - 1076 Views - Last Post: 11 March 2015 - 09:13 AM Rate Topic: -----

#1 jlis   User is offline

  • D.I.C Head

Reputation: 13
  • View blog
  • Posts: 230
  • Joined: 02-February 15

Windows Form (FormClosingEvent) Question

Posted 05 March 2015 - 05:29 PM

Hello,

I've created a windows Form with an Exit button, the exit button when pressed by the user displays a MessageBox which has YesNoCancel. From there I've used an if statement to check is the user clicks 'yes', which is they do closes the application else if the No or cancel buttons are pressed then the application stays running.

What I'd like to do is achieve the exact same result if the user clicks the 'X' (cross in the top right hand corner).

At the moment what happens is, if the user clicks either the 'Exit' or Cross the messagebox appears however if they click any of the YesNoCancel buttons the application closes.

Code below, cheers in advance for any help.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace FunderlandApplicationPPW5
{
    public partial class WelcomeForm : Form
    {
        public WelcomeForm()
        {
            InitializeComponent();

            this.FormClosing += WelcomeForm_FormClosing;
        }



        private void WelcomeForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (e.CloseReason == CloseReason.UserClosing)
            {
                //Create a DialogResult variable for quitting the program
                DialogResult drExit;

                //Assign drExit to the following MessageBox when quitting so we can use the YesNoCancel buttons in an if statement.
                drExit = MessageBox.Show("Are you sure you want to quit ?", "Exit", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question);

                if (drExit == DialogResult.Yes)
                {
                    MessageBox.Show("Goodbye");
                    Application.Exit();
                }
                else
                {
                    
                }
            }
        }

        private void btnExit_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void WelcomeForm_Load(object sender, EventArgs e)
        {

        }
    }
}




Is This A Good Question/Topic? 0
  • +

Replies To: Windows Form (FormClosingEvent) Question

#2 tlhIn`toq   User is offline

  • Xamarin Cert. Dev.
  • member icon

Reputation: 6535
  • View blog
  • Posts: 14,450
  • Joined: 02-June 10

Re: Windows Form (FormClosingEvent) Question

Posted 05 March 2015 - 05:52 PM

One: WinForms is dead. Employers want coders that can do WPF. Spend your time there if you expect to a get a job in this industry.

Two: Of course it closes. Line 48 tells it to. You're not doing anything to stop it. Its been years since I've done WinForms (like most of us), but if I remember right the formclosingargs e has a cancel property. I think you can do something to the effect of e.cancel = true; to stop the closing from happening. Read up on the argument on MSDN to confirm that.

But unless you have no choice but to be doing this in WinForms such as this is a class and you have to... Redirect your efforts to THIS decade's technology of WPF.
Was This Post Helpful? 1
  • +
  • -

#3 jlis   User is offline

  • D.I.C Head

Reputation: 13
  • View blog
  • Posts: 230
  • Joined: 02-February 15

Re: Windows Form (FormClosingEvent) Question

Posted 05 March 2015 - 05:56 PM

Thank you so much for the quick reply, ill look into that now.

I completely agree, I put this argument to my lecturers with the response that they aren't trained on WPF so windows forms is what Im stuck with for my assignments.

e.cancel = true; worked in the else section of the if statement.

Thankyou so much!

This post has been edited by andrewsw: 05 March 2015 - 05:59 PM
Reason for edit:: Removed previous quote, just press REPLY

Was This Post Helpful? 0
  • +
  • -

#4 jlis   User is offline

  • D.I.C Head

Reputation: 13
  • View blog
  • Posts: 230
  • Joined: 02-February 15

Re: Windows Form (FormClosingEvent) Question

Posted 05 March 2015 - 06:01 PM

The working code just in case anybody ever needs it in future.

private void WelcomeForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (e.CloseReason == CloseReason.UserClosing)
            {
                //Create a DialogResult variable for quitting the program
                DialogResult drExit;

                //Assign drExit to the following MessageBox when quitting so we can use the YesNoCancel buttons in an if statement.
                drExit = MessageBox.Show("Are you sure you want to quit ?", "Exit", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question);

                if (drExit == DialogResult.Yes)
                {
                    MessageBox.Show("Goodbye");
                    Application.Exit();
                }
                else
                {
                    e.Cancel = true;
                }
            }
        }



With the above you can click the cross or create a 'Close()' method and it'll always carry out the above.
Was This Post Helpful? 2
  • +
  • -

#5 click_here   User is offline

  • D.I.C Regular

Reputation: 47
  • View blog
  • Posts: 303
  • Joined: 25-November 13

Re: Windows Form (FormClosingEvent) Question

Posted 05 March 2015 - 06:38 PM

If no and cancel are doing the same thing, why not use the YesNo or OkCancel buttons intead?

https://msdn.microso...(v=vs.110).aspx
Was This Post Helpful? 1
  • +
  • -

#6 tlhIn`toq   User is offline

  • Xamarin Cert. Dev.
  • member icon

Reputation: 6535
  • View blog
  • Posts: 14,450
  • Joined: 02-June 10

Re: Windows Form (FormClosingEvent) Question

Posted 06 March 2015 - 05:59 AM

Quote

I put this argument to my lecturers with the response that they aren't trained on WPF so windows forms is what Im stuck with


Sounds like 100 of you that are in the course need to go to the administration and ask why they are paying this guy to teach skills that are so out of date. Isn't he supposed to be getting you hire-able for the year 2019 not 1989?

You could learn everything in his 4 year WinForms course for free on line in 1 year - or through a couple older textbooks in a year.

Under both circumstances you won't be competitive against existing staff that have a decade or more of WinForms experience who can maintain the legacy WinForms programs employers have. As a new hire you need a skillset for FUTURE projects, a skill set the old-timers haven't bothered to pick up yet just like your instructor.

You're going to be paying off those school loans for decades. Its your money. Have some say in where it gets spent and how it works for you - or in this case isn't doing squat for you.
Was This Post Helpful? 1
  • +
  • -

#7 jlis   User is offline

  • D.I.C Head

Reputation: 13
  • View blog
  • Posts: 230
  • Joined: 02-February 15

Re: Windows Form (FormClosingEvent) Question

Posted 06 March 2015 - 07:28 AM

I couldn't agree more.

I spoke to the head of dept here and explained the problem, funny thing is all the lecturers recommended THIS book at the start of my studies. I bought the book and got about half way through (I'm a slow learner as I like to make sure I have a good understanding of EVERY aspect before moving on) and then asked why they recommended a book that codes in WPF if they refuse to let me submit my work in WPF and only Win Forms.

Her response simply was, I can do my dissertation in final year in WPF. She later then asked me to write up the module booklets they provide us with basic win form tutorials like for like in WPF. I obviously refused lol.

I quit full time work for come to university so really for me it's not a major major issue as Im learning WPF in my spare time anyway and I really only do the windows form stuff when assignments are due... It's the other students I feel sorry for as pretty much everybody does the base minimum.

I suppose this is the price you pay when you go to a university ranked 30+ in the country :/
Was This Post Helpful? 0
  • +
  • -

#8 tlhIn`toq   User is offline

  • Xamarin Cert. Dev.
  • member icon

Reputation: 6535
  • View blog
  • Posts: 14,450
  • Joined: 02-June 10

Re: Windows Form (FormClosingEvent) Question

Posted 06 March 2015 - 07:50 AM

Sounds like the teachers are complacent in their position. Tenure is nice: You don't have to work, you don't have to keep your skills up, and you stay employed.
And then they want you to teach them WPF. WOW. They can't even understand WPF enough for a motivated student to be able to turn in the assignment using it. What a waste.
Was This Post Helpful? 0
  • +
  • -

#9 Curtis Rutland   User is offline

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 5106
  • View blog
  • Posts: 9,283
  • Joined: 08-June 10

Re: Windows Form (FormClosingEvent) Question

Posted 11 March 2015 - 08:40 AM

To be fair, most of these degree programs aren't focused on teaching people to be competent business programmers. It's a Science degree (or possibly an Engineering degree, depending on the school). It's about the science of programming. They're supposed to learn concepts broadly applicable to all languages (learning just enough of the language to be used in class to demonstrate these concepts); academic concepts. Like the difference between a calculus class and a practical accounting class; one is about the theory of math, the other the practical application. Most CS classes are like the former. So in that respect, it makes sense that they're not teaching what's "current"; they're using something that hasn't changed in years to continue to make the same points as they have in years prior.

The problem is, a ton of people graduate, then go spend the next 30 years doing business programming. Most of what they learned is wasted, since they're not doing anything academically advanced; the complexity comes from integrating into a ton of external systems and maintaining ancient large codebases, not to mention dealing with data in various states of good and crap. Most of what people learned in school isn't all that useful in the business world.

Apparently the first few years out of school is where most programmers are supposed to learn their actual job skills.
Was This Post Helpful? 0
  • +
  • -

#10 tlhIn`toq   User is offline

  • Xamarin Cert. Dev.
  • member icon

Reputation: 6535
  • View blog
  • Posts: 14,450
  • Joined: 02-June 10

Re: Windows Form (FormClosingEvent) Question

Posted 11 March 2015 - 09:13 AM

Exactly. Which is why I find it both hysterical and frustrating that so many employers insist on a degree to be hired as a developer. The piece of paper means NOTHING with respect to their ability to actually develop software.

Worse yet are the graduates that feel "I've graduated: Who wants the privilege of giving me a job for $150k/year so I can bring my college education and fix all your woes."
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1