Take the 2-minute tour ×

Sorry I am new to this just wondering if someone can help, I am trying to get my label to change its text every time I click the button. Not sure how I should go about it. Can anyone help me please.

private void button1_Click(object sender, EventArgs e)
        string[] MonthName;
        MonthName = new string[12];

        MonthName[0] = "January";
        MonthName[1] = "February";
        MonthName[2] = "March";
        MonthName[3] = "April";
        MonthName[4] = "May";
        MonthName[5] = "June";
        MonthName[6] = "July";
        MonthName[7] = "August";
        MonthName[8] = "September";
        MonthName[9] = "October";
        MonthName[10] = "November";
        MonthName[11] = "December";

            label1.Text = (MonthName[0]);
            label1.Text = (MonthName[1]);   
share|improve this question
What research have you done ? is this your home work ? –  wcraft Jan 30 '13 at 4:12
Simon Whitehead show the fastest way to implement. But if you want to use static button captions (MonthName), you can declare them in class scope and use then in the button1_Click function –  Zeal Jan 30 '13 at 5:07
add comment

3 Answers

up vote 1 down vote accepted

This should do the trick:

Declare the array and an int in the class

    static int i = 0;

Then in the button click

protected void btnAddMonth_Click(object sender, EventArgs e)
    lblMonth.Text = MonthName[i];
    i = (i+1) % 12;
share|improve this answer
That was what I was after thank you –  Callum James Jan 30 '13 at 12:09
you are welcome, but keep in mind the counter is static. If it was helpful could u just mark it? –  A J Jan 30 '13 at 12:30
And one more thing, nect time when u post a question do a lot of research on it first, cos if u dont do that u might get banned here. So make sure u really cant get a solution by your own n then post. Good luck. –  A J Jan 30 '13 at 12:31
add comment

Might be easier to do it this way:

DateTime currentDate = new DateTime(DateTime.Now.Year, 1, 1); // Per Habib's suggestion

private void button1_Click(object sender, EventArgs e) {
    label1.Text = currentDate.ToString("MMMM");
    currentDate = currentDate.AddMonths(1);
share|improve this answer
+1 interesting :). IMO, your currentDate should be new DateTime(2013, 1, 1); so that the month always starts from January. –  Habib Jan 30 '13 at 4:29
@Habib Good suggestion, I will edit :) –  Simon Whitehead Jan 30 '13 at 4:34
add comment
  1. Move the array initialization outside the click handler.
  2. Have a hidden label that stores the counter to determine which element in the array to pick.

Something like the below code. Please mind that this is not the most elegant code, but I wanted to keep it simple as you said you are new to this.

var Months = new List<string>

private void button1_Click(object sender, EventArgs e)
            labelHiddenCounter.Text = "0";

        if(labelHiddenCounter.Text == "11")
            labelHiddenCounter.Text = "-1";

        var nextCounter = Convert.ToInt32(labelHiddenCounter.Text) + 1;

        label1.Text = (Months[nextCounter]);

        labelHiddenCounter.Text = nextCounter.ToString();
share|improve this answer
Why should he create another label if a simple int is enough? –  Areks Jan 30 '13 at 4:59
@Areks: Without a hidden label, how will he remember the current item index across multiple button clicks? –  Thimmarasu Lanka Feb 5 '13 at 8:27
Using an ordinary integer outside the button click event, for example. Your approach is 'hacky' at best. –  Areks Feb 7 '13 at 2:15
add comment

Your Answer


