locked
Disable form Controls Without Being Gray!!! RRS feed

  • Question

  • Hello,

    I am developing an application and i have some controls (Checkbox, combobox mostly) which i want to Disable while the form is in "VIEW" mode, so user cannot change it's value. I don't want to disable these controls cause they are slightly visible to user then.


    Thanks Stefanos
    Monday, September 24, 2007 12:13 PM

Answers

  •  

    Hi OratIos,

    Yes, only setting the BackColor of a standard panel to Color.Transparent doesn’t do what you want. You need create your own panel inherit from the standard panel. Try something like the following:

    Code Snippet

        public class AlphaPanel : Panel

        {

            const int WS_EX_TRANSPARENT = 0x00000020;

     

            protected override CreateParams CreateParams

            {

                get

                {

                    CreateParams cp = base.CreateParams;

                    cp.ExStyle |= WS_EX_TRANSPARENT;

                    return cp;

                }

            }

     

            protected override void OnPaintBackground(PaintEventArgs e)

            {//do not paint the background

            }

     

            protected override void OnPaint(PaintEventArgs e)

            {

                SolidBrush brush = new SolidBrush(Color.Transparent);

                e.Graphics.FillRectangle(brush, new Rectangle(0, 0, this.Width, this.Height));

            }

    }

     

    Then use this alpha panel to make the control ‘disable’ like the following:

    Code Snippet

        public partial class Form1 : Form

        {

            public Form1()

            {

                InitializeComponent();

            }

     

            AlphaPanel alphapanel = new AlphaPanel();

     

            private void btnView_Click(object sender, EventArgs e)

            {

                alphapanel.Dock = DockStyle.Fill;

                this.Controls.Add(alphapanel);

                alphapanel.BringToFront();

                this.btnEditable.BringToFront();

            }

     

            private void btnEditable_Click(object sender, EventArgs e)

            {

                this.Controls.Remove(alphapanel);

            }

    }

     

    For more information about how to create a transparent control, check this thread.

    Best regards.
    Rong-Chun Zhang

    Wednesday, September 26, 2007 8:27 AM

All replies

  •  

    Put the controls in a container control (such as a Panel) and disable that instead.
    Monday, September 24, 2007 1:44 PM
  • Use ReadOnly=true instead of Enabled=false and

    set BackColor=SystemColors.Window (after setting it readonly).

    You may need to check for BackColorChanged event to reset the color.

     

    And btw subjects work with less than three exclamation marks ...

    Monday, September 24, 2007 2:02 PM
  • This is the same think with the disable property combobox and checkbox have, but it's smarter to disable one panel than disabling lots of checkboxes and comboboxes. The result is the same and my controls are stil gray. It would be fine if i could inherit the color of my checkboxes and comboboxes. Is there a way on doing that?

     

    Thanks.

     

    Monday, September 24, 2007 4:17 PM
  •  

    CheckBox and Combobox Doesn't have ReadOnly property in their Class. I can inherit that property by creating a new combobox class, but i am thinking that there would be another way to lock controls.

     

    Another aproach is to dock above all cotrols a transparent panel, but transparency doesn't work well in v.s.

     

    Thanks for your help.

    Monday, September 24, 2007 4:22 PM
  •  

    Hi OratIos,

    Yes, only setting the BackColor of a standard panel to Color.Transparent doesn’t do what you want. You need create your own panel inherit from the standard panel. Try something like the following:

    Code Snippet

        public class AlphaPanel : Panel

        {

            const int WS_EX_TRANSPARENT = 0x00000020;

     

            protected override CreateParams CreateParams

            {

                get

                {

                    CreateParams cp = base.CreateParams;

                    cp.ExStyle |= WS_EX_TRANSPARENT;

                    return cp;

                }

            }

     

            protected override void OnPaintBackground(PaintEventArgs e)

            {//do not paint the background

            }

     

            protected override void OnPaint(PaintEventArgs e)

            {

                SolidBrush brush = new SolidBrush(Color.Transparent);

                e.Graphics.FillRectangle(brush, new Rectangle(0, 0, this.Width, this.Height));

            }

    }

     

    Then use this alpha panel to make the control ‘disable’ like the following:

    Code Snippet

        public partial class Form1 : Form

        {

            public Form1()

            {

                InitializeComponent();

            }

     

            AlphaPanel alphapanel = new AlphaPanel();

     

            private void btnView_Click(object sender, EventArgs e)

            {

                alphapanel.Dock = DockStyle.Fill;

                this.Controls.Add(alphapanel);

                alphapanel.BringToFront();

                this.btnEditable.BringToFront();

            }

     

            private void btnEditable_Click(object sender, EventArgs e)

            {

                this.Controls.Remove(alphapanel);

            }

    }

     

    For more information about how to create a transparent control, check this thread.

    Best regards.
    Rong-Chun Zhang

    Wednesday, September 26, 2007 8:27 AM
  • Hi Rong

     

    Thank you very much. That's what i need!!!

    Friday, September 28, 2007 10:16 AM