0
votes

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]);   
4
What research have you done ? is this your home work ?wcraft
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 functionZeal

4 Answers

6
votes

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);
}
2
votes

This should do the trick:

Declare the array and an int in the class

string[] MonthName = { "Jan", "Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec" };
    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;
}
0
votes
  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>
            {
                   "January",
                   "February",
                   "March",
                   "April",
                   "May",
                   "July",
                   "August",
                   "September",
                   "October",
                   "November",
                   "December"
            };

private void button1_Click(object sender, EventArgs e)
{
        if(string.IsNullOrEmpty(labelHiddenCounter.Text))
            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();
}
0
votes

If you want random months to appear whenever you click the button use.

        var random = new Random();
        var months = new List<string>
        {
             "Jan", 
             "Feb", 
             "Mar", 
             "Apr", 
             "May", 
             "Jun", 
             "Jul", 
             "Aug", 
             "Sep", 
             "Oct", 
             "Nov", 
             "Dec"
        };

        int index = random.Next(months.Count);
        label1.Text = (months[index]);