C#中:更改按钮背景色没有效果 [英] C#: Changing Button BackColor has no effect

查看:521
本文介绍了C#中:更改按钮背景色没有效果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在使用Windows窗体C#按钮的问题。

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.

在code看起来是这样的:

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

        }
    }

想法吗?

有一个类似的问题被问这里但答案是不是非常有帮助:

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


  • 尝试使用调用给我, DispWin 尚未创建运行时错误。

  • 设置 UseVisualStyleBackColor 假的变化罢了。

  • 设置背景色前景色或显示 DispWin 只添加和格式化后的按钮也没有影响。

  • Trying to use Invoke gives me the run-time error that DispWin is not yet created.
  • Setting UseVisualStyleBackColor to false changes nothing.
  • Setting BackColor and ForeColor or Showing DispWin only after adding and formatting the Buttons also had no effect.

我在哪里去了?

推荐答案

您正试图设置颜色,但你忽略它说 UseVisualStyleBackColor = TRUE

You are trying to set up the color, but then you override it saying UseVisualStyleBackColor = true

如果你想使用你的自定义颜色,您需要设置 UseVisualStyleBackColor 或只是颜色会结束了在鼠标应用到该按钮。

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.

上传至GitHub的一个简单的测试

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

结果

这篇关于C#中:更改按钮背景色没有效果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆