如何将背景颜色设置为默认值? [英] How do I set the backcolor to default?

查看:92
本文介绍了如何将背景颜色设置为默认值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个代码,它创建了一个按钮数组,然后使用插入排序算法对它们进行排序,它将按钮颜色设置为绿色以进行比较,将颜色设置为红色进行交换。

now ..为什么按钮保持颜色,如何将它们设置为默认backcolur





这是交换

i have a code wich creates an array of buttons then it sorts them using the insertion sort algorithm and it colours the buttons green for comparison and red for swapping.
now..why does the buttons remain coloured and how can i set them to default backcolur


this is the swapping

public  void exchange(int[] A, int m, int n)
       {
           string s;
           int temp;
           but[m].BackColor = Color.Red;
           System.Threading.Thread.Sleep(100);
           but[n].BackColor = Color.Pink;
           System.Threading.Thread.Sleep(100);

           temp = A[m];
           s = but[m].Text;
           A[m] = A[n];
           but[m].Text = but[n].Text;
           A[n] = temp;
           but[n].Text = s;

           but[m].Refresh();

           but[n].Refresh();


       }







这个是排序




and this is the sorting

<pre>public  void sort(int[] A)
        {
            int i, j;
            int N = A.Length;

            for (j = 1; j < N; j++)
            {
                for (i = j; i > 0 && A[i] < A[i - 1]; i--)
                {
                    but[i-1].BackColor = Color.Green;
                    System.Threading.Thread.Sleep(100);
                    but[i].BackColor = Color.GreenYellow;
                    System.Threading.Thread.Sleep(100);
                    but[i].Refresh();
                    but[i - 1].Refresh();
                    exchange(A, i, i - 1);
                }
            }
        }



带有表格的图片 [ ^ ]



我尝试了什么:



我尝试了刷新方法,但是那个'工作。


picture with form[^]

What I have tried:

i've tryed the refresh method but that dosen't work.

推荐答案

解决方案非常简单。在更改BackColor的颜色之前,请将其保存在变量中。您甚至可以在调试器中检查BackColor的内容。
The solution is pretty simple. Save the value of BackColor in a variable before you change it's color. You could even examine the content of BackColor in the debugger.


我认为不是刷新这是您需要的:

I think instead of refresh this is what you need:
but[i].BackColor = Color.FromName("Control");





如果它能解决您的问题,请告诉我。



Let me know if it solves your problem.


您好,

您可以尝试@Dave解决方案,它存储按钮的前一个backColor。

如果你想设置默认颜色(按钮控制背面颜色),你可以试试这个: -



Hi,
You can try the @Dave Solution which store the previous backColor of the Button.
If you want to set the default color(which is button control back color) then you can try this:-

but[i].BackColor = default(Color);



这将设置按钮的默认背景颜色。



我希望这可行。


this will set the default Back Color of a button.

I hope this will work.


这篇关于如何将背景颜色设置为默认值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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