缩短此 if 条件
几天前,我问了一个可能有点不清楚的问题。现在我已经编写了可以更好地说明问题的代码。请先看下面的代码:
int d;
d = DateTime.Today.Day;
if (d==1)
{
hyperlinkButton1.Background=new SolidColorBrush(Colors.Black);
}
else if (d==2)
{
hyperlinkButton2.Background=new SolidColorBrush(Colors.Black);
}
else if (d==3)
{
hyperlinkButton3.Background=new SolidColorBrush(Colors.Black);
}
else if (d==4)
{
hyperlinkButton4.Background=new SolidColorBrush(Colors.Black);
}
else if (d==5)
{
hyperlinkButton5.Background=new SolidColorBrush(Colors.Black);
}
else if (d==6)
{
hyperlinkButton6.Background=new SolidColorBrush(Colors.Black);
}
else if (d==7)
{
hyperlinkButton7.Background=new SolidColorBrush(Colors.Black);
}
else if (d==8)
{
hyperlinkButton8.Background=new SolidColorBrush(Colors.Black);
}
else if (d==9)
{
hyperlinkButton9.Background=new SolidColorBrush(Colors.Black);
}
else if (d==10)
{
hyperlinkButton10.Background=new SolidColorBrush(Colors.Black);
}
else if (d==11)
{
hyperlinkButton11.Background=new SolidColorBrush(Colors.Black);
}
else if (d==12)
{
hyperlinkButton12.Background=new SolidColorBrush(Colors.Black);
}
else if (d==13)
{
hyperlinkButton13.Background=new SolidColorBrush(Colors.Black);
}
else if (d==14)
{
hyperlinkButton14.Background=new SolidColorBrush(Colors.Black);
}
else if (d==15)
{
hyperlinkButton15.Background=new SolidColorBrush(Colors.Black);
}
else if (d==16)
{
hyperlinkButton16.Background=new SolidColorBrush(Colors.Black);
}
else if (d==17)
{
hyperlinkButton17.Background=new SolidColorBrush(Colors.Black);
}
else if (d==18)
{
hyperlinkButton18.Background = new SolidColorBrush(Colors.Black);
}
else if (d==19)
{
hyperlinkButton19.Background=new SolidColorBrush(Colors.Black);
}
else if (d==20)
{
hyperlinkButton20.Background=new SolidColorBrush(Colors.Black);
}
else if (d==21)
{
hyperlinkButton21.Background=new SolidColorBrush(Colors.Black);
}
else if (d==22)
{
hyperlinkButton22.Background=new SolidColorBrush(Colors.Black);
}
else if (d==23)
{
hyperlinkButton23.Background=new SolidColorBrush(Colors.Black);
}
else if (d==24)
{
hyperlinkButton24.Background=new SolidColorBrush(Colors.Black);
}
else if (d==25)
{
hyperlinkButton25.Background=new SolidColorBrush(Colors.Black);
}
else if (d==26)
{
hyperlinkButton26.Background=new SolidColorBrush(Colors.Black);
}
else if (d==27)
{
hyperlinkButton2.Background=new SolidColorBrush(Colors.Black);
}
else if (d==28)
{
hyperlinkButton28.Background=new SolidColorBrush(Colors.Black);
}
else if (d==29)
{
hyperlinkButton29.Background=new SolidColorBrush(Colors.Black);
}
else if (d==30)
{
hyperlinkButton30.Background=new SolidColorBrush(Colors.Black);
}
else
{
hyperlinkButton31.Background=new SolidColorBrush(Colors.Black);
}
我的问题(作为初学者)是这样的:在 C# 中是否有任何方法可以通过使应用程序根据 d< 的值确定必须更改哪个超链接按钮背景来缩短此条件/代码>?
A few days ago I asked a question that might be a little unclear. Now I have written code that might illustrate the question better. Please look at the code below first:
int d;
d = DateTime.Today.Day;
if (d==1)
{
hyperlinkButton1.Background=new SolidColorBrush(Colors.Black);
}
else if (d==2)
{
hyperlinkButton2.Background=new SolidColorBrush(Colors.Black);
}
else if (d==3)
{
hyperlinkButton3.Background=new SolidColorBrush(Colors.Black);
}
else if (d==4)
{
hyperlinkButton4.Background=new SolidColorBrush(Colors.Black);
}
else if (d==5)
{
hyperlinkButton5.Background=new SolidColorBrush(Colors.Black);
}
else if (d==6)
{
hyperlinkButton6.Background=new SolidColorBrush(Colors.Black);
}
else if (d==7)
{
hyperlinkButton7.Background=new SolidColorBrush(Colors.Black);
}
else if (d==8)
{
hyperlinkButton8.Background=new SolidColorBrush(Colors.Black);
}
else if (d==9)
{
hyperlinkButton9.Background=new SolidColorBrush(Colors.Black);
}
else if (d==10)
{
hyperlinkButton10.Background=new SolidColorBrush(Colors.Black);
}
else if (d==11)
{
hyperlinkButton11.Background=new SolidColorBrush(Colors.Black);
}
else if (d==12)
{
hyperlinkButton12.Background=new SolidColorBrush(Colors.Black);
}
else if (d==13)
{
hyperlinkButton13.Background=new SolidColorBrush(Colors.Black);
}
else if (d==14)
{
hyperlinkButton14.Background=new SolidColorBrush(Colors.Black);
}
else if (d==15)
{
hyperlinkButton15.Background=new SolidColorBrush(Colors.Black);
}
else if (d==16)
{
hyperlinkButton16.Background=new SolidColorBrush(Colors.Black);
}
else if (d==17)
{
hyperlinkButton17.Background=new SolidColorBrush(Colors.Black);
}
else if (d==18)
{
hyperlinkButton18.Background = new SolidColorBrush(Colors.Black);
}
else if (d==19)
{
hyperlinkButton19.Background=new SolidColorBrush(Colors.Black);
}
else if (d==20)
{
hyperlinkButton20.Background=new SolidColorBrush(Colors.Black);
}
else if (d==21)
{
hyperlinkButton21.Background=new SolidColorBrush(Colors.Black);
}
else if (d==22)
{
hyperlinkButton22.Background=new SolidColorBrush(Colors.Black);
}
else if (d==23)
{
hyperlinkButton23.Background=new SolidColorBrush(Colors.Black);
}
else if (d==24)
{
hyperlinkButton24.Background=new SolidColorBrush(Colors.Black);
}
else if (d==25)
{
hyperlinkButton25.Background=new SolidColorBrush(Colors.Black);
}
else if (d==26)
{
hyperlinkButton26.Background=new SolidColorBrush(Colors.Black);
}
else if (d==27)
{
hyperlinkButton2.Background=new SolidColorBrush(Colors.Black);
}
else if (d==28)
{
hyperlinkButton28.Background=new SolidColorBrush(Colors.Black);
}
else if (d==29)
{
hyperlinkButton29.Background=new SolidColorBrush(Colors.Black);
}
else if (d==30)
{
hyperlinkButton30.Background=new SolidColorBrush(Colors.Black);
}
else
{
hyperlinkButton31.Background=new SolidColorBrush(Colors.Black);
}
My question (as a beginner) is this: is there any way in C# to shorten this condition by making the application determine which hyperlinkbutton background it has to change depending on the value of d
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
定义相关控件的数组,并使用整数键入该数组,记住数组是从 0 开始的,而不是从 1 开始的。
Define an array of the relevant controls, and use the integer to key into the array, remembering that arrays are 0-based and not 1-based.
基于数组的方法在一般级别上是多个 if 语句的一个很好的替代方案,但由于这也被标记为 Silverlight,因此您可能有兴趣利用 FrameworkElement.FindName 方法(如果您可以依赖使用公共前缀命名 HyperlinkButton 的约定)。
The array based approach is a good alternative at a general level to the multiple if statements but since this is also tagged as Silverlight, you might be interested in taking advantage of the FrameworkElement.FindName Method if you can rely on the convention of naming the HyperlinkButtons with a common prefix.
ETC。
etc.