我已經開始在我的程序中創建大量按鈕,1到96。計劃是,當一個按鈕被按下時,它會變成紅色,隨機會在附近的其他按鈕上捕捉。Windows Phone C# - 按鈕陣列的按鈕顏色
我遇到的問題是,按鈕不希望變成紅色。下面,我將展示我嘗試的代碼可以這樣做:
public MainPage()
{
InitializeComponent();
numbers = new[] {
button1, button2, button3, button4, button5, button6, button7, button8, button9, button10,
button11, button12, button13, button14, button15, button16, button17, button18, button19,
button20, button21, button22, button23, button24, button25, button26, button27, button28,
button29, button30, button31, button32, button33, button34, button35, button36, button37,
button38, button39, button40, button41, button42, button43, button44, button45, button46,
button47, button48, button49, button50, button51, button52, button53, button54, button55,
button56, button57, button58, button59, button60, button61, button62, button63, button64,
button65, button66, button67, button68, button69, button70, button71, button72, button73,
button74, button75, button76, button77, button78, button79, button80, button81, button82,
button83, button84, button85, button86, button87, button88, button89, button90, button91,
button92, button93, button94, button95, button96
};
}
private void ButtonSequence(int bRef)
{
Random random = new Random();
int activeCell = bRef;
int randomNumber = random.Next(0, 10);
int steps = 0;
int randomMaxRange = 2;
SolidColorBrush red = new SolidColorBrush(Colors.Red);
numbers[activeCell].Background = red;
if (activeCell == 0)
{
randomNumber = random.Next(0, 10);
if (randomNumber < randomMaxRange)
{
steps++;
ButtonSequence(1);
}
此功能繼續,並通過調用:
private void button1_Click(object sender, RoutedEventArgs e)
{
ButtonSequence(0);
}
和其他人。
有沒有人有這個修復? 謝謝。
看看這個http://stackoverflow.com/questions/4991041/c-sharp-change-a-buttons-background-color –