C#: Changing Button BackColor has no effect
Solution 1
You are trying to set up the color, but then you override it saying UseVisualStyleBackColor = true
if you want to use your custom color, you need to set UseVisualStyleBackColor
to false
or the color will only be applied to the button upon mouse over.
a simple test uploaded to GitHub
public partial class mainForm : Form
{
Random randonGen = new Random();
public mainForm()
{
InitializeComponent();
}
private void mainForm_Load(object sender, EventArgs e)
{
populate();
}
private void populate()
{
Control[] buttonsLeft = createButtons().ToArray();
Control[] buttonsRight = createButtons().ToArray();
pRight.Controls.AddRange(buttonsRight);
pLeft.Controls.AddRange(buttonsLeft);
}
private List<Button> createButtons()
{
List<Button> buttons = new List<Button>();
for (int i = 1; i <= 5; i++)
{
buttons.Add(
new Button()
{
Size = new Size(200, 35),
Enabled = true,
BackColor = GetColor(),
ForeColor = GetColor(),
UseVisualStyleBackColor = false,
Left = 20,
Top = (i * 40),
Text = String.Concat("Button ", i)
});
}
return buttons;
}
private Color GetColor()
{
return Color.FromArgb(randonGen.Next(255), randonGen.Next(255), randonGen.Next(255));
}
}
result
Solution 2
If FlatStyle
for button is set to System
, it will not show any backcolor rather use backcolor from template of system colors.
Solution 3
Make sure you do not have a BackgroundImage set. This overrides the BackColor.
Lionel Pöffel
Updated on July 09, 2022Comments
-
Lionel Pöffel almost 2 years
I'm having a problem with C# buttons in Windows Forms.
I've create a number of buttons programmatically and add them to a form afterwards.
Interestingly, every modification to those buttons (location and size) except for the modification of the
BackColor
is readily executed. Only the button's color remains unchanged.The code looks something like this:
public class SimpleSortAlgDisplayer : ISortAlgDisplayer { #region ISortAlgDisplayer Member void ISortAlgDisplayer.Init(int[] Data) { this.DataLength = Data.Length; this.DispWin = new CurrentSortStateWin(); this.DispWin.Show(); this.DispWin.Size = new Size(60 + (10 * this.DataLength), 120); this.myArrayElements = new Button[this.DataLength]; for (int i = 0; i < this.DataLength; i++) { this.myArrayElements[i] = new Button(); //begin of series of invoked actions this.myArrayElements[i].Size=new Size(5,(int)(((80)*(double)Data[i])/1000)); this.myArrayElements[i].Location = new Point(30 + (i * 10), 90-(this.myArrayElements[i].Size.Height)); this.myArrayElements[i].Enabled = true; this.myArrayElements[i].BackColor = Color.MidnightBlue; this.myArrayElements[i].UseVisualStyleBackColor = true; this.DispWin.Controls.Add(this.myArrayElements[i]); this.myArrayElements[i].Refresh(); } }
Ideas anyone?
A similar question was asked here but the answers to it were not very helpful:
- Trying to use
Invoke
gives me the run-time error thatDispWin
is not yet created. - Setting
UseVisualStyleBackColor
to false changes nothing. - Setting
BackColor
andForeColor
or ShowingDispWin
only after adding and formatting the Buttons also had no effect.
Where am I going wrong?
- Trying to use
-
Lionel Pöffel over 12 yearsAs I wrote I also tried setting UseVisualStyleBackColor to false but also this had no effect
-
Hans Olsson over 12 years@Lionel: It's still relevant though since if you try something that fix your other problem, you might not notice it due to this, so definitely remove the
UseVisualStyleBackColor
line. -
Hans Olsson over 12 yearsThough I think it's only relevant if
UseVisualStyleBackColor
is set totrue
before theBackColor
is set, since I think setting theBackColor
automatically setsUseVisualStyleBackColor
tofalse
. -
balexandre over 12 years@Lionel Pöffel added github link, fell free to clone it and test it, then debug your own code.
-
Lionel Pöffel over 12 yearsthanks for adding the link. I'm not yet seeing where could be the important difference. Of course I tried creating a new Form like your's and, not much surprisingly, setting BackColor worked there. The only idea I have at the moment is that your populate() method is called from an event handler where my method is public and can be called directly if an objyct of type SimpleSortAlgDisplayer has been created.
-
Lionel Pöffel over 12 yearsI got it: In the main program.cs Application.EnableVisualStyles() is set by default when creating a windows Forms application. Commenting this out solved the problem (although the buttons don't look as nice. Thanks everyone here.
-
balexandre over 12 yearsIf my answer have helped you, don't forget to upvote or set as correct answer, so others in the future could know what to do easily. :)
-
DiSaSteR over 6 yearsthis tortured me for 18 hours! thank you, removing BackgroundImage solved the problem