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.