I'm using windows forms in C# and I need to make a textbox's background color transparent. I have a trackbar that goes from 0 to 255 that is supposed to control it, but I'm having some trouble. I created a question earlier today asking the exact same thing, but no success.
Here is the code I currently have:
private void trackAlpha_ValueChanged(object sender, EventArgs e)
{
newColor = Color.FromArgb(trackAlpha.Value, colorDialog.Color.R, colorDialog.Color.G, colorDialog.Color.B);
colorDialog.Color = newColor; // The Windows dialog used to pick the colors
colorPreview.BackColor = newColor; // Textbox that I'm setting the background color
}
The problem is that absolutely nothing happens. Any ideas on why this is not working?
On the previous question, this nice guy said something about SetStyle(ControlStyles.SupportsTransparentBackColor, true);
, but I have no idea on where I should put this.
You need to try out something like this.
Add a new user control , say CustomTextBox and change
to
You will then get the following error saying that the 'AutoScaleMode' is not defined. Delete the following line in the Designer.cs class.
Make changes to the constructor of your newly added control as follows.
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
Create a new control which inherits from TextBox, set the style to allow tranparency in the constructor. Then use your new control instead of TextBox
Do this in your constructor:
This will allow your new control to have a transparent background color.
You can read more about control styles here; MSDN: Control Styles, this may help as well; Inheriting from a Windows Forms Control with Visual C#
I never liked having to make my own inherited controls for this. So I made a wrapper function to the private SetStyle function.
Try using it instead of creating your own class?
bool itWorked = SetStyle(myControl, ControlStyles.SupportsTransparentBackColor, true);
Sorry to uncover old posts, however, been searching for a few days now to find a solution to this awful problem of no transparency for textboxes!!! (Amazingly MSAccess has a check state to show transparency!)
Anyways, I have built a VB workaround, however, it is very crude and whilst may help a lot of people would also like any input from the more hard-core'rs with any insights...
It basically uses the textbox, then sizes it away and replaces with a label (hence now representing a transparent "appearing" textbox. Also couple of other things like stopping the beeping if press enter on a single line textbox.
To use - Create a new class and paste ALL the code over the top this should create two custom objects ( CTextBox and CLabel ) - you only need to use CTEXTBOX in your form design.
Easily converts into C, if that's your language, but please let me know if have any suggestions?