C #: Changing the BackColor button has no effect

I have a problem with C # buttons in Windows Forms.

I create several programs programmatically and add them to the form after.

Interestingly, each modification of these buttons (location and size), with the exception of the modification BackColor, is easily performed. The color of the button 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();

        }
    }

Anyone ideas?

A similar question was asked here , but the answers to it were not very helpful:

  • Trying to use Invokegives me a runtime error that has not yet been created DispWin.
  • Setting UseVisualStyleBackColorto false does not change anything.
  • Settings BackColorand ForeColoror display DispWinonly after adding and formatting buttons also had no effect.

?

+4
4

, , UseVisualStyleBackColor = true

, UseVisualStyleBackColor false .

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));
    }
}

enter image description here

+9

FlatStyle System, , .

+2

, BackgroundImage. BackColor.

+1

In the properties window for the button. Find the “FlatStyle” property and change it from “System” to “Flat”, “Standard” or “Popup” and you can see the color change of the button. I just fixed my problem with this.

0
source

All Articles