I want to set the background color of the richtextbox to transparent In c# can you help me please Its a winform application
Asked
Active
Viewed 6,163 times
-5
-
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 Answers
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:

Mehdi Khademloo
- 2,754
- 2
- 20
- 40
-
-
What is your mean?? Just a feature in c#, by setting the style in a control. What is your mean? – Mehdi Khademloo Sep 20 '14 at 13:50
-
-
-
@valter the answer was a bit wrong, but now, it's OK. excuse me :) – Mehdi Khademloo Sep 20 '14 at 14:54
-
-
-
-
@MuhammadNasirKhan the easiest way to this is: Drag a RichTextBox1 to form and go to the Form1.Designer.cs and replace the RichTextBox1 to TransparentLabel (don't forget the namespace) – Mehdi Khademloo Sep 20 '14 at 17:18
-
-
1Hardly. There simply is no real tranparency in Winforms Controls. Period. If you don't take my word, aks Hans or MSDN. It only can be faked. – TaW Sep 20 '14 at 18:10
-
@TaW Yes, The winForm basically dose not support the transparency background of control, but we can do some trick for this, like this that i'm mentioned before: http://stackoverflow.com/questions/25830079/how-to-make-the-background-of-a-label-transparent-in-c-sharp/25830214#25830214 – Mehdi Khademloo Sep 20 '14 at 18:15
-
The problem with this is that it will no update when things below change. Even if you notice and update that background image, the control on top will not notice it,; it only asks its parent once, upon creation, for the background its transparent parts are supposed to shine through. After that it'll show just that old image. To workaround that you'd have to delete and re-create the control..Not so much a workaround but a deep hack. – TaW Sep 20 '14 at 18:19
-
-
Hm, I just played around a little and have to admit, it works better than I had expected. May well be good enough for the OP! – TaW Sep 20 '14 at 18:36
-
Try to move the *RichTextBox* and see what happens. No tranparency. Game over. – γηράσκω δ' αεί πολλά διδασκόμε Sep 20 '14 at 20:42