0

I have an assignment for school and i got a bit stuck in the process.

The idea is to make a program in C# where you can visualize how the insertion sorting algorithm works and for that I'm using an array of buttons with random generated numbers.

It colours green for comparison and red for swapping.

Why do the buttons remain coloured?

public partial class Form1 : Form
{
    Button[] but; 
    int[] A;
    int nr_den = 0;
    int s1 = 0;
    int s2 = 0;

    public Form1()
    {
        InitializeComponent();
    }

    private void button1_Click(object sender, EventArgs e)
    {
        nr_den = Convert.ToInt16(textBox1.Text);
        s1 = Convert.ToInt16(textBox2.Text);
        s2= Convert.ToInt16(textBox3.Text);
        A = new int[nr_den+1];
        Random r = new Random();

        for (int i = 1; i <= nr_den; i++)
        {

            A[i] = r.Next(s1, s2);
        }

        but = new Button[nr_den + 1];

        for (int i = 1; i <= nr_den; i++)
        {
            but[i] = new Button();
            but[i].Text = A[i].ToString();
            but[i].Width = 40;
            but[i].Height = 40;
            flowLayoutPanel1.Controls.Add(but[i]);
        }
    }

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

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

    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(400);
                but[i].BackColor = Color.GreenYellow;
                System.Threading.Thread.Sleep(400);
                but[i].Refresh();
                but[i - 1].Refresh();
                exchange(A, i, i - 1);
            }
        }
    }

    private void button2_Click(object sender, EventArgs e)
    {
        for (int i=1;i<=nr_den;i++)
        richTextBox1.Text += A[i]+ " ";

        richTextBox1.Text += " \n";

        sort(A);
    }

    private void button3_Click(object sender, EventArgs e)
    {
        flowLayoutPanel1.Controls.Clear();
    }
}

form

9
  • Instead of rearranging the buttons themselves, it would be easier to rearrange a backing array and just update the buttons after every pass. Commented Jan 1, 2017 at 21:46
  • If you put the thread to sleep it wont repaint what you just did Commented Jan 1, 2017 at 21:48
  • @Abion47 I'm sorting the A array but i don't know how to update the button array. the colouring is done by the position of thet button in the sorted array *sorry for my english i know i'm not verry good at it Commented Jan 1, 2017 at 21:51
  • Ok i managed to update the buttons. Now the only problem is the colouring. Commented Jan 1, 2017 at 21:57
  • 1
    Because your code has not a single line that "uncolors" them Commented Jan 1, 2017 at 22:47

1 Answer 1

1

restore color after completion

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(400);
                but[i].BackColor = Color.GreenYellow;
                System.Threading.Thread.Sleep(400);
                but[i].Refresh();
                but[i - 1].Refresh();
                exchange(A, i, i - 1);
                but[i-1].BackColor = SystemColors.Control;
                but[i].BackColor = SystemColors.Control;
            }
        }
    }

restore color during process

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(400);
                but[i].BackColor = Color.GreenYellow;
                System.Threading.Thread.Sleep(400);
                but[i].Refresh();
                but[i - 1].Refresh();
                exchange(A, i, i - 1);
                but[i-1].BackColor = SystemColors.Control;
                but[i].BackColor = SystemColors.Control;
                System.Threading.Thread.Sleep(400);
                but[i].Refresh();
                but[i - 1].Refresh();
            }
        }
    }
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you verry much! I really apreciate your help!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.