-5

I want to set the background color of the richtextbox to transparent In c# can you help me please Its a winform application

  • Transparent with respect to what? There is a reason RichTextBox doesn't support transparency, and that's because it is difficult decide what to show behind it. It can be anywhere in a component hierarchy. For it to draw itself, then, it needs to know something about all the components up the tree. You can hack it to be transparent all the way through the form, but you've not been clear about what you are trying to achieve. – J... Sep 20 '14 at 12:05
  • That is not an option, not supported by the native Windows control. It can be made to work on Windows 8 but that's not something you can count on today. Windows 7 is the new XP. – Hans Passant Sep 20 '14 at 12:08

1 Answers1

11
    public class TransparentLabel : RichTextBox
    {
        public TransparentLabel()
        {
            this.SetStyle(ControlStyles.Opaque, true);
            this.SetStyle(ControlStyles.OptimizedDoubleBuffer, false);
            this.TextChanged += TransparentLabel_TextChanged;
            this.VScroll += TransparentLabel_TextChanged;
            this.HScroll += TransparentLabel_TextChanged;
        }

        void TransparentLabel_TextChanged(object sender, System.EventArgs e)
        {
            this.ForceRefresh();
        }
        protected override CreateParams CreateParams
        {
            get
            {
                CreateParams parms = base.CreateParams;
                parms.ExStyle |= 0x20;  // Turn on WS_EX_TRANSPARENT
                return parms;
            }
        }
        public void ForceRefresh()
        {
            this.UpdateStyles();
        }
    }

and the answer is look like this: enter image description here

Mehdi Khademloo
  • 2,754
  • 2
  • 20
  • 40