1

I'm trying to make a sorting program with Windows Forms. The goal is to allow the user to create an array by taking a value from a trackBar that determines the size of the array. For example, if the trackbar value is set to 100, then the user would press the "Create Array" button, which would then generate an integer array with 100 numbers that are random and then display them on a Chart.

Then, the user would press another button to actually sort the array. However, because the array is defined in the scope of the button that actually creates the array, I don't know how to get it into the scope of the button that sorts it. After it is sorted, I want to keep the array sorted, so it needs to be changed on a global level.

I tried to define the variable on a class level within the Form1: Form class and have each control return a value, which would update the value for the array, but that doesn't work because my project doesn't have a static void Main() function, and I'm not sure how to implement that into Windows Forms with my current minimal knowledge of the program.

Code example is below:

public partial class Form1 : Form
    {


        public Form1()
        {
            InitializeComponent();
        }
        // Initializes size of the array.
        public int arraySize;

        // Generates the array and links it to the chart for visualization.
        private void button2_Click(object sender, EventArgs e)
        {
            // the dataArray takes in the arraySize and returns an array
            // which is used to populate the chart.
            arraySize = trackBar1.Value;
            ArrayObject dataGenerator = new ArrayObject();
            int[] dataArray = dataGenerator.GenerateData(arraySize);

            chart1.Series["Data"].Points.Clear();
            int placement = 0;
            foreach (int dataPoint in dataArray)
            {
                this.chart1.Series["Data"].Points.AddXY(placement, dataPoint);
                placement += 1;
            }
        }

        // updates the label to show the current value that trackBar1 has selected.
        private void trackBar1_Scroll(object sender, EventArgs e)
        {
            trackbarValueLabel.Text = trackBar1.Value.ToString();
        }

        private void buttonSort_Click(object sender, EventArgs e)
        {

        }
    }

Worst-case scenario, I can create the array, chart it, sort, then re-chart it all within the same button control if need be, but I do want to keep them separated so the user can focus on creating a dataset they like, then have it sorted.

1 Answer 1

2

It sounds like you want the user to be able to do operations (sort) on the last array that was created. The last array represents a piece of state held by the form.

To make a form stateful, add a member variable at the form level. In this case you'd add an array and update it whenever you create an array.

public partial class Form1 : Form
{
    protected int[] _latestArray = new int[] {} ; 

    public Form1()
    {
        InitializeComponent();
    }

    // Initializes size of the array.
    public int arraySize = 0;

    // Generates the array and links it to the chart for visualization.
    private void button2_Click(object sender, EventArgs e)
    {
        // the dataArray takes in the arraySize and returns an array
        // which is used to populate the chart.
        arraySize = trackBar1.Value;
        ArrayObject dataGenerator = new ArrayObject();
        int[] dataArray = dataGenerator.GenerateData(arraySize);

        chart1.Series["Data"].Points.Clear();
        int placement = 0;
        foreach (int dataPoint in dataArray)
        {
            this.chart1.Series["Data"].Points.AddXY(placement, dataPoint);
            placement += 1;
        }
        //Save state
        _latestArray = dataArray;
    }

    private void buttonSort_Click(object sender, EventArgs e)
    {
        Array.Sort(_latestArray);
    }
}
Sign up to request clarification or add additional context in comments.

Comments

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.