Myles McDonnell
Myles McDonnell

Reputation: 13335

Grey out winform app except open dialog

I would like to "grey out" my winform app all except the currently open dialog, is this possible?

Upvotes: 3

Views: 1354

Answers (2)

Guish
Guish

Reputation: 5160

A little twisty but it seem to simulate the "grey out" feeling. You need to use the form deactivate event and the form activate event. You can make every of your forms inherits from this class. It didn't seem to hurt the performance.

public class GrayingOutForm : Form
{
    public GrayingOutForm()
    {
        this.Activated += this.Form1_Activated;
        this.Deactivate += this.Form1_Deactivate;
    }

    private readonly List<Control> _controlsToReEnable = new List<Control>() ;
    private void Form1_Activated(object sender, EventArgs e)
    {
        foreach (var control in _controlsToReEnable)
            control.Enabled = true;

    }

    private void Form1_Deactivate(object sender, EventArgs e)
    {
        _controlsToReEnable.Clear();
        foreach (var control in this.Controls)
        {
            var titi = control as Control;
            if (titi != null && titi.Enabled)
            {
               titi.Enabled = false;//Disable every controls that are enabled
               _controlsToReEnable.Add(titi);  //Add it to the list to reEnable it after
            }
        }
    }
}

Now you can move freely between yours windows and every window seem to desactivate.

Upvotes: 1

Knaģis
Knaģis

Reputation: 21485

You should use ShowDialog() instead of Show(). That will disable all other windows except for the new one.

To "grey out" visually you will have to set form.Enabled=false; manually and revert it once the dialog is closed (which is not too hard since ShowDialog() is a blocking call).

Upvotes: 1

Related Questions