C# 多次单击按钮时更改标签文本

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14596486/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 12:22:39  来源:igfitidea点击:

C# change label text when button clicked multiple times

c#visual-studio-2010

提问by Callum James

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]);   

采纳答案by A J

This should do the trick:

这应该可以解决问题:

Declare the array and an int in the class

在类中声明数组和一个int

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;
}

回答by Simon Whitehead

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

回答by Thimmarasu Lanka

  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.
  1. 将数组初始化移到点击处理程序之外。
  2. 有一个隐藏标签,用于存储计数器以确定要选择数组中的哪个元素。

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();
}

回答by nolimits05

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]);