Click here to Skip to main content
15,907,326 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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
C#
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
C#
<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.
Posted
Updated 1-Jan-17 21:14pm
v2

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.
 
Share this answer
 
I think instead of refresh this is what you need:
but[i].BackColor = Color.FromName("Control");


Let me know if it solves your problem.
 
Share this answer
 
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.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900