更有效的方式来获得当前的星期和运行代码
本文关键字:运行 代码 方式 有效 | 更新日期: 2023-09-27 18:10:13
我在这里有一段效率极低的代码,它做了一个相当简单的工作-检测它是星期几,并使用字符串设置一堆文本框。代码工作得很好,但我认为有更好的方法来做到这一点?
if (DateTime.Now.DayOfWeek == DayOfWeek.Monday)
{
time7.Text = "TUESDAY";
time8.Text = "WEDNESDAY";
time9.Text = "THURSDAY";
time10.Text = "FRIDAY";
time11.Text = "SATURDAY";
time12.Text = "SUNDAY";
time13.Text = "MONDAY";
}
if (DateTime.Now.DayOfWeek == DayOfWeek.Tuesday)
{
time7.Text = "WEDNESDAY";
time8.Text = "THURSDAY";
time9.Text = "FRIDAY";
time10.Text = "SATURDAY";
time11.Text = "SUNDAY";
time12.Text = "MONDAY";
time13.Text = "TUESDAY";
}
if (DateTime.Now.DayOfWeek == DayOfWeek.Wednesday)
{
time7.Text = "THURSDAY";
time8.Text = "FRIDAY";
time9.Text = "SATURDAY";
time10.Text = "SUNDAY";
time11.Text = "MONDAY";
time12.Text = "TUESDAY";
time13.Text = "WEDNESDAY";
}
if (DateTime.Now.DayOfWeek == DayOfWeek.Thursday)
{
time7.Text = "FRIDAY";
time8.Text = "SATURDAY";
time9.Text = "SUNDAY";
time10.Text = "MONDAY";
time11.Text = "TUESDAY";
time12.Text = "WEDNESDAY";
time13.Text = "THURSDAY";
}
if (DateTime.Now.DayOfWeek == DayOfWeek.Friday)
{
time7.Text = "SATURDAY";
time8.Text = "SUNDAY";
time9.Text = "MONDAY";
time10.Text = "TUESDAY";
time11.Text = "WEDNESDAY";
time12.Text = "THURSDAY";
time13.Text = "FRIDAY";
}
if (DateTime.Now.DayOfWeek == DayOfWeek.Saturday)
{
time7.Text = "SUNDAY";
time8.Text = "MONDAY";
time9.Text = "TUESDAY";
time10.Text = "WEDNESDAY";
time11.Text = "THURSDAY";
time12.Text = "FRIDAY";
time13.Text = "SATURDAY";
}
if (DateTime.Now.DayOfWeek == DayOfWeek.Sunday)
{
time7.Text = "MONDAY";
time8.Text = "TUESDAY";
time9.Text = "WEDNESDAY";
time10.Text = "THURSDAY";
time11.Text = "FRIDAY";
time12.Text = "SATURDAY";
time13.Text = "SUNDAY";
}
var textBoxes = new List<TextBox>{time7, time8};//add others
var current = DateTime.Now;
foreach(var tb in textBoxes)
{
current = current.AddDays(1);
tb.Text = current.DayOfWeek.ToString().ToUpper();
}
类似这样的东西,你也可以使用其他的变体,比如for(var i = 0...)
循环,但是你明白了。
另外,您可以使用MVVM
模式绑定到textboxes
,这更容易。
1)创建文本框数组
2)设置动态数(0 ~ 6)为天
3)然后用循环函数
填充文本框for(i=0;i<=6:i++)
{textbox[i].text=dynamic_function_in_form_load(i);}
这个怎么样:
void YourFunction()
{
// a list of your day-names: since it seems that you are maintaining some old code, I presume that implementation of localization is out of question, so you can use a list..
var dateNames = new List<string>() { "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday" };
// a list of your textboxes that hold names of days
var tbs = new List<TextBox>() { textBox1, textBox2, textBox3, textBox4, textBox5, textBox6, textBox7 };
// current day
var x = (int)DateTime.Now.DayOfWeek;
shift(dateNames, tbs, x);
}
void shift(List<string> dates, List<TextBox> tbs, int x)
{
for (int i = 0; i < x; i++)
{
dates.Insert(7, dates[0]);
dates.Remove(dates[0]);
}
for (int i = 0; i < dates.Count; i++)
{
tbs[i].Text = dates[i];
}
}
这并不"好",但仍然比你现在的维护更容易。
这个适合我:
var labels =
typeof(DayOfWeek).GetEnumNames()
.Concat(typeof(DayOfWeek).GetEnumNames())
.Skip((int)DateTime.Now.DayOfWeek + 1)
.Take(7)
.Select(x => x.ToUpperInvariant())
.ToArray();
time7.Text = labels[0];
time8.Text = labels[1];
time9.Text = labels[2];
time10.Text = labels[3];
time11.Text = labels[4];
time12.Text = labels[5];
time13.Text = labels[6];