-4

I have the following array:

int[] month = new int[12] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12 };

This represents my months in a year.

Now I have two buttons, and a label. The label should only display the value of the current item in the array. When I press, lets say, the next button, It should move from eg. 5 to 6 in the array, and now display 6 in the label.

4

1 に答える 1

1

Try the below code it will help you...

Set this array as Global

int[] month = new int[12] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12 };

Load event

label1.Text = month[4].ToString();

Next Button Click

        int ind = Array.IndexOf<int>(month, Convert.ToInt32(label1.Text));
        if ((ind + 1) != 12)
            label1.Text = month[ind + 1].ToString();
        else
            MessageBox.Show("End of the Array element");

Previous button Click

            int ind = Array.IndexOf<int>(month, Convert.ToInt32(label1.Text));
            if ((ind - 1) != -1)
                label1.Text = month[ind - 1].ToString();
            else
                MessageBox.Show("End of the Array element");
于 2013-02-05T12:48:51.453 に答える