Hi all,
I am creating an application for Library Management Systems.
I want to disable the close button initially and enable it once the user clicks on the menu item logoff.

Is there any way I could accomplish this functionality in my application?

I tried using the following code in the formClosing event but it is not woking out.

private void frmLibrary_FormClosing(object sender, FormClosingEventArgs e)
        {
            e.Cancel = false;
            if (checkLogOff == false)
            {
                MessageBox.Show("Please Log Off before closing the Application");
                   e.Cancel = false;
                    this.ControlBox = false;
                    return;
            }
            else
            {
                this.ControlBox = true;
                e.Cancel = true;
            }

        }

The value of checkLogOff Variable is set as follows:

public bool checkLogOff = false;
        private void logOffToolStripMenuItem_Click(object sender, EventArgs e)
        {
           checkLogOff = true;
         /*Code to update the logoff users in the database*/
       }

When executing the application if I dont click on the LogOff menu item I am getting the dialog box but immediately after I press the OK Button in the Message Box the Application closes. But I dont want to allow the user to close the application before clicking on the LogOff MenuItem.

Please help me out in accomplishing this task.

Thanks in advance!

Hello, S2009.

Beforehand little quotation from msdn:

CancelEventArgs..::.Cancel Property
Gets or sets a value indicating whether the event should be canceled.

Property Value
Type: System..::.Boolean
true if the event should be canceled; otherwise, false.

And now let's look at your code:

private void frmLibrary_FormClosing(object sender, FormClosingEventArgs e)
        {
            // there's no need in this line, because it's set to false by default
            e.Cancel = false;
            if (checkLogOff == false)
            {
                MessageBox.Show("Please Log Off before closing the Application");
                   // regarding to quote from msdn, you say: "Everything ok, keep closing"
                   e.Cancel = false;
                    // I'll talk about this one later.
                    this.ControlBox = false;
                    // unnecessary thing, because your method already finished it's job
                    return;
            }
            else
            {
                // Later ..
                this.ControlBox = true;
                // regarding to quote from msdn, you say: "Something goes wrong. Stop closing form"
                e.Cancel = true;
            }
        }

Ok, now few words about ControlBox. I suppose it would be more understandable for user if you will show/hide ControlBox in some other place. E.g. make it hidden when loading form and show it while logging off.

Also just a few thoughts in loud. I don't know if that's suited for your situation, but maybe you could ask user if he wants to log off and sign him off if he wants also and in formClosing event (insdead of showing/hiding ControlBox).

From the form load method, you can disable the button.

button1.Enabled = false;


On the click event enable the button.

button1.Enabled = true;

Hello, S2009.
Ok, now few words about ControlBox. I suppose it would be more understandable for user if you will show/hide ControlBox in some other place. E.g. make it hidden when loading form and show it while logging off.

Also just a few thoughts in loud. I don't know if that's suited for your situation, but maybe you could ask user if he wants to log off and sign him off if he wants also and in formClosing event (insdead of showing/hiding ControlBox).

Spot on. You are cancelling the close event when everything is fine and allwoing the app to close if they havent logged off :)

Also, i'd second what antenka said; it would be more intuitive to hide the close box whilst the user is logged in rather than having it dissappear whilst they are trying to close the app.
Also, if you wanted to give the user the choice of logging off then it would be something like:

DialogResult result = MessageBox.Show("You must log off before closing the applicaiton. Would you like to be logged off now?", "Error Closing", MessageBoxButtons.YesNoCancel);
            if (result == DialogResult.Yes)
            {
                //Log them out
            }
            else
            {
                e.Cancel = true;
            }
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.