Transparency for windows forms textbox
You need to try out something like this.
Add a new user control , say CustomTextBox and change
public partial class CustomTextBox : UserControl
to
public partial class CustomTextBox : TextBox
You will then get the following error saying that the 'AutoScaleMode' is not defined. Delete the following line in the Designer.cs class.
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
Make changes to the constructor of your newly added control as follows.
public partial class CustomTextBox : TextBox
{
public CustomTextBox()
{
InitializeComponent();
SetStyle(ControlStyles.SupportsTransparentBackColor |
ControlStyles.OptimizedDoubleBuffer |
ControlStyles.AllPaintingInWmPaint |
ControlStyles.ResizeRedraw |
ControlStyles.UserPaint, true);
BackColor = Color.Transparent;
}
}
Build, close the custom control designer if open and you will be able to use this control on any other control or form.
Drop it from the toolbox as shown below
Windows Forms Transparent TextBox C# Existing solutions don't work
The solution in your linked answer works fine. If you're not using the designer it doesn't matter... you can still use the same solution. InitializeComponent()
is simply a method that's created by the code generator in the designer file. If you ever want to know what it does to create controls (it can be very informational to have a look) then create a control using the designer and then inspect the .Designer.cs
file.
EDIT: It acts a little funny. You can override OnPaint
to fix the white background and disappearing text, see below. Not a "finished" implementation, the cursor doesn't seem to know where to go, but this should get you in the right direction.
using System;
using System.Drawing;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
for (int i = 0; i < 3; i++)
{
var x = new UserControl1 {Location = new Point(0, i*20)};
this.Controls.Add(x);
}
}
}
public class UserControl1 : TextBox
{
public UserControl1()
{
SetStyle(ControlStyles.SupportsTransparentBackColor |
ControlStyles.OptimizedDoubleBuffer |
ControlStyles.AllPaintingInWmPaint |
ControlStyles.ResizeRedraw |
ControlStyles.UserPaint, true);
BackColor = Color.Transparent;
TextChanged += UserControl2_OnTextChanged;
}
protected override void OnPaint(PaintEventArgs e)
{
var backgroundBrush = new SolidBrush(Color.Transparent);
Graphics g = e.Graphics;
g.FillRectangle(backgroundBrush, 0, 0, this.Width, this.Height);
g.DrawString(Text, Font, new SolidBrush(ForeColor), new PointF(0,0), StringFormat.GenericDefault);
}
public void UserControl2_OnTextChanged(object sender, EventArgs e)
{
Invalidate();
}
}
}
Textbox transparent like a hole on my winform
Color.Transparent is 0,255,255,255 in ARGB. that means its white with no opacity. since the transparency key is for non-transparent colors (24Bit RGB) it doesnt use the Alpha part. That means. Color.Transparent == Color.White for the TransparencyKey
So the behaviour you are having is 100% expected, TransparencyKey is white, and TextBox BackColor is white. Therefor the textbox Background is not visible. not a bug at all.
You can fix this by not setting the TransparencyKey at all, or setting it to Color.Empty or some other Color. Or you can change the BackgroundColor of the TextBox if you want to keep white as the TransparencyKey.
Having White as TransparencyKey is not a good idea though (u have seen why). i would recommend using Cyan or Magenta (rarely used colors) if you still want to use it
Making a TextBox Transparent
Not too complex, after some searching on StackOverflow, I found this answer.
Basically, you need to subclass TextBox
and in its constructor add the appropriate style.
Final stpe, set its BackColor
to Transparent. Hope it helps.
Related Topics
Oledbparameters and Parameter Names
How to Fix the Microsoft Visual Studio Error: "Package Did Not Load Correctly"
Xml Serialize Generic List of Serializable Objects
Performance Difference for Control Structures 'For' and 'Foreach' in C#
Setting the User-Agent Header for a Webclient Request
Why Does Calling a Method in My Derived Class Call the Base Class Method
Get Month Name from Month Number
Convert JSON String to JSON Object C#
How to Call a Vbscript File in a C# Application
How to Set Properties on Struct Instances Using Reflection
Dynamically Adding Properties to an Expandoobject
How to Quickly Check If Two Data Transfer Objects Have Equal Properties in C#
How to Do Template Specialization in C#
File I/O with Streams - Best Memory Buffer Size
Thread.Sleep for Less Than 1 Millisecond