2012-02-03 63 views
2

前几天我问了一个question可能有点不清楚。现在我已经编写了可以更好地说明问题的代码。请看下面的代码第一:缩短这个,如果条件

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#中的任何方式通过使应用程序确定它依赖于改变其hyperlinkbutton背景缩短这个条件d的值?

回答

3

的基于阵列的方法是在一般的水平,多一个很好的选择,如果声明,但因为这也标记为Silverlight的,你可能有兴趣参加的FrameworkElement.FindName Method的优势,如果你可以依靠的约定用一个公共前缀命名HyperlinkBut​​tons。

var hyperlinkButton = this.FindName("hyperlinkButton" + DateTime.Now.Day) as HyperlinkButton; 
if (hyperlinkButton != null) 
{ 
    hyperlinkButton.Background = new SolidColorBrush(Colors.Black); 
} 
+0

感谢您的帮助,这真的让我的生活更轻松;) – DreamNet 2012-02-04 08:32:46

0
switch(d) 
{ 
case 1: doThings(); break; 
case 2: doThings2(); break; 
case 3: 
    doSomeThings(); 
    doMoreThings(); 
    break; 
default: 
    runThingsIfDIsNotListed(); 
    break; 
} 

4

定义相关的控制的阵列,并使用整数钥匙插入阵列,记住阵列0为基础的,而不是基于1的。

var buttons = new [] { 
    hyperlinkButton1, 
    hyperlinkButton2, 
    hyperlinkButton3, 
    hyperlinkButton4, 
    hyperlinkButton5, 
    hyperlinkButton6, 
    hyperlinkButton7, 
    hyperlinkButton8, 
    hyperlinkButton9, 
    // ... 
} 

//.... 

buttons[DateTime.Today.Day-1].Background=new SolidColorBrush(Colors.Black);