TransparencyKey Property on Forms - c#

I would like to swith my form background transparency with Visual C# in a Windows Forms Application.
I used
BackColor = Color.White;
TransparencyKey = Color.White;
Now I want to switch back to "not transparent". How can I accomplish that? Just switching the BackColor makes the elements on the form look strange and it feels ugly.
I guess there is a way to reset the property.

this is the original value:
this.TransparencyKey = Color.Empty;
You could set this, and then nothing will be transparent.

How about storing the previous values of BackColor and TransparencyKey in local variables, and restoring them when you want to revert to non-transparent? For instance:
private Color _oldBG;
private Color _oldTPKey;
private void MakeTransparent() {
_oldBG = BackColor;
_oldTPKey = TransparencyKey;
BackColor = Color.White;
TransparencyKey = Color.White;
}
private void MakeNonTransparent() {
BackColor = _oldBG;
TransparencyKey = _oldTPKey;
}

It's eleven years later, but I ran into this problem. So for any other readers: I set the transparency key mistakenly. A post from another forum said you could right click it, at design, and then reset it, but reset was disabled (grayed out). So I simply backspaced out the 'White' (in design) that was set. (I am confident it set it to 'Color.Empty' as one of the guys here noted.) Problem solved. I should say that I was using an ancient version of .net. Hopefully this will also work in more up to date versions.

Related

Weird grey background on checkbox

I am creating an application for a new RPG that I am creating. The application has a specific theme and I want to change the checkboxes and buttons to black and the cyan color I chose. On loading the form I use the following code to change the color of the boxes.
coloredCheckBoxes.Add(checkBox1);
coloredCheckBoxes.Add(checkBox2);
foreach (CheckBox checkBox in coloredCheckBoxes)
{
checkBox.FlatStyle = FlatStyle.Flat;
checkBox.FlatAppearance.BorderSize = 1;
checkBox.FlatAppearance.BorderColor = Color.FromArgb(0, 211, 211);
checkBox.FlatAppearance.CheckedBackColor = Color.Black;
checkBox.FlatAppearance.MouseOverBackColor = Color.Black;
checkBox.FlatAppearance.CheckedBackColor = Color.Black;
checkBox.BackColor = Color.Black;
}
No matter how many colors I change the back color of the check boxes still remain this weird grey color.
The check boxes also become lighter if I move my cursor over the boxes as if checkBox.FlatAppearance.MouseOverBackColor = Color.Black; was not used. Is there a way to fix this issue hopefuly without creating a custom checkbox class?
I understand what you need finally.(I remove any old answer)
Try this,
- MetroFramework UI - Original by Peter.
- MetroFramework UI - Forked, dennismagno(Recommand).
- MetroFramework UI - Forked, thielj.
Also provided source code, so you can modify it.

Changing form color depending on answer

I have searched, and tried answers. I have the code, but I'm unsure why it isn't working. I am trying to change the backcolor of the form from green back to white. I believe I have it correct, but it doesn't show the first color, only the last.
if (PassBox1.Text == PassBox2.Text)
{
this.BackColor = Color.FromArgb(0, 255, 0);
// voice.Speak("Correct ", SpeechVoiceSpeakFlags.SVSFDefault);
this.BackColor = Color.FromArgb(192,192,192);
}
When this code runs and backcolor is set to (0,255,0) which is followed by a blocking operation voice.speak, this does not give chance to the ui to refresh and soon as voice speak is done, the backcolor changes to (192...) before ui gets changes to show the other color.
in this case you would be much happier if you created a subclassed text box which allows you to flash colors. you can use some ideas from this post
https://stackoverflow.com/a/4147406/2903863

Winforms combobox background

I've tried to change the background of a ComboBox using:
comboBox.BackColor = Color.Crimson;
And I get white borders like this:
You can set additionally to the BackColor the FlatStyle to flat:
this.comboBox1.BackColor = Color.Red;
this.comboBox1.FlatStyle = FlatStyle.Flat;
Or in the designer:
This is the result:
However, if you want to add more extra features, it's advisable to create your own component, which would be a little bit more complicated.

Unexpected transparency instead of white

I am working on a GUI , and it is showing Transparency for no reason in place of white color of the image itself , what might be the cause of this ?
Original GUI before executing application:
Upon Executing the Application:
To solve this issue, reset the TransparencyKey back to its default value, or remove the assignment:
this.TransparencyKey = Color.Empty;
instead of
this.TransparencyKey = Color.Transparent;
References: https://stackoverflow.com/a/13426429/1132334, and documentation:
When the TransparencyKey property is assigned a Color, the areas of the form that have the same BackColor will be displayed transparently

Cannot use Color.FromArgb in DataGridView cell BackColor

I am looping through a DataGridView control and adding rows dynamically. I am setting the BackColor property of each cell based on the following logic:
if (bidVolume != null)
{
this.Rows[this.RowCount - 1].Cells[1].Style.BackColor = Color.Green;
}
else
{
this.Rows[this.RowCount - 1].Cells[1].Style.BackColor = Color.FromArgb(150, Color.Green);
}
This is causing problems, the color is not properly set visually and when re-sizing the DataGridView it looks like this:
When I don't use Color.FromArgb and just use Color.Red for example, then everything works fine ..
Is it possible to set a cell BackColor using Color.FromArgb ?
Thanks
You cannot use Color.FromArgb, because DataGridView won't accept transparent colors. This is probably caused by the fact that the cells and DataGridView are not transparent (by default). What you are looking for is probalby this; you may want to set BackColor to color between White and Green.
If I am mistaken and this is not what you want please explain your need for alpha channel in the cell.
You got the reason why. To overcome this, use the protected SetStyle method to override the behaviour. Something like:
class MyDgv : DataGridView
{
public MyDgv()
{
this.SetStyle(ControlStyles.SupportsTransparentBackColor, true); //this is the key
//and now you can do what you want.
this.Rows[this.RowCount - 1].Cells[1].Style.BackColor = Color.FromArgb(150, Color.Green);
}
}
From documentation:
The BackColor property does not support transparent colors unless the
SupportsTransparentBackColor value of
System.Windows.Forms.ControlStyles is set to true.
The BackColor property is an ambient property. An ambient property is
a control property that, if not set, is retrieved from the parent
control. For example, a Button will have the same BackColor as its
parent Form by default. For more information about ambient properties,
see the AmbientProperties class or the Control class overview.
Old question, but I ran into the same issue. The simplest solution is just to use the Color.FromArgb() override that excludes the alpha parameter from the constructor. As long as you don't specify an alpha transparency, the color works perfectly.
public static Color SeaFoamGreen = Color.FromArgb(20, 125, 115);

Categories

Resources