抱歉,我是新手,只是想知道是否有人可以提供帮助,我每次点击按钮时都会尝试让我的标签更改其文字。不知道我应该怎么做。任何人都可以帮助我。
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]);
答案 0 :(得分:7)
这样做可能更容易:
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);
}
答案 1 :(得分:2)
这应该可以解决问题:
在类
中声明数组和intstring[] MonthName = { "Jan", "Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec" };
static int i = 0;
然后在按钮中单击
protected void btnAddMonth_Click(object sender, EventArgs e)
{
lblMonth.Text = MonthName[i];
i = (i+1) % 12;
}
答案 2 :(得分:0)
类似下面的代码。请注意,这不是最优雅的代码,但我想保持简单,因为你说你是新手。
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();
}
答案 3 :(得分:0)
如果您希望在单击按钮时出现随机月份。
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]);