如何随机分配按钮内容和颜色?

时间:2015-07-27 14:51:00

标签: c# wpf

我无法想出一种方法,可以随机分配按钮的内容和匹配颜色,如果有人可以帮助我,我会非常感激。

目前,分配我的按钮内容和颜色的代码是

    public void SetButtonContent(TextBlock reagentText, TextBlock transMetalText,
    Button opt1, Button opt2, Button opt3, Button opt4, Button opt5, Button opt6, Button opt7, Button opt8)
    {
        string pickedMetal = CycleThroughMetals(); //randomly picks a metal 
        string pickedReagent = CycleThroughReagents(); //randomly picks reagent

        reagentText.Text = pickedReagent;
        transMetalText.Text = pickedMetal;

        BrushConverter bc = new BrushConverter();  
        switch (pickedMetal)
        {
            case "Copper":
                opt1.Content = string.Format("{0} \n {1} \n{2}",Cu.hexAqColour, Cu.hexAqFormula, Cu.hexAqState);
                opt1.Background = Brushes.Blue;

                opt2.Content = string.Format("{0} \n {1} \n{2}",Cu.dilNaOHRctColour, Cu.dilNaOHRctFormula, Cu.dilNaOHRctState);
                opt2.Background = Brushes.Blue;

                opt3.Content = string.Format("{0} \n {1}",Cu.excessNaOHColour, Cu.excessNaOHFormula, Cu.excessNahOHState);
                opt3.Background = Brushes.Blue;

                opt4.Content = string.Format("{0} \n {1} \n{2}", Cu.dilNH3RctColour, Cu.dilNH3RctFormula, Cu.dilNH3RctState);
                opt4.Background = Brushes.Blue;

                opt5.Content = string.Format("{0} \n {1} \n{2}",Cu.ExcessNH3Colour, Cu.ExcessNH3Formula, Cu.ExcessNH3State);
                opt5.Background = Brushes.DarkBlue;

                opt6.Content = string.Format("{0} \n {1} \n{2}",Cu.saltRctColour, Cu.saltRctFormula, Cu.saltRctState);

                opt7.Content = string.Format("{0} \n {1} \n{2}",Cu.Na2CO3RctColour, Cu.Na2CO3RctFormula, Cu.Na2CO3RctState);
                opt7.Background = Brushes.Blue;

                opt8.Content = string.Format("{0} \n {1} \n{2}",Cu.ClRctColour, Cu.ClRctFormula, Cu.ClRctState);
                opt8.Background = Brushes.Yellow;
                break;


            default:
                break;
        }

我只在switch语句中包含了一个案例,只是为了让它更容易阅读,但在我的代码中有6个案例,但都执行几乎相同的功能。

但我想随机而不是手动分配按钮内容,但我不确定如何执行此操作。

我也非常感谢任何建设性的批评,因为我是WPF和C#的初学者。

1 个答案:

答案 0 :(得分:3)

一个简单的解决方案是将按钮,内容和颜色存储在列表中,随机化它们,然后像​​这样分配它们的值:

public void SetButtonContent(TextBlock reagentText, TextBlock transMetalText,
    Button opt1, Button opt2, Button opt3, Button opt4, Button opt5, Button opt6, Button opt7, Button opt8)
{
    string pickedMetal = CycleThroughMetals(); //randomly picks a metal 
    string pickedReagent = CycleThroughReagents(); //randomly picks reagent

    List<Button> buttons = new List<Button>{opt1, opt2, opt3, opt4, opt5, opt6, opt7, opt8};
    List<Tuple<string, Brush>> contentsAndColors;

    reagentText.Text = pickedReagent;
    transMetalText.Text = pickedMetal;

    BrushConverter bc = new BrushConverter();  
    switch (pickedMetal)
    {
        case "Copper":
            contentsAndColors = new List<Tuple<string, Brush>>{
                new Tuple<string, Brush>(string.Format("{0} \n {1} \n{2}",Cu.hexAqColour, Cu.hexAqFormula, Cu.hexAqState), Brushes.Blue),
                new Tuple<string, Brush>(string.Format("{0} \n {1} \n{2}",Cu.dilNaOHRctColour, Cu.dilNaOHRctFormula, Cu.dilNaOHRctState), Brushes.Blue ),
                new Tuple<string, Brush>(string.Format("{0} \n {1}",Cu.excessNaOHColour, Cu.excessNaOHFormula, Cu.excessNahOHState),Brushes.Blue ),
                new Tuple<string, Brush>(string.Format("{0} \n {1} \n{2}", Cu.dilNH3RctColour, Cu.dilNH3RctFormula, Cu.dilNH3RctState),Brushes.Blue ),
                new Tuple<string, Brush>(string.Format("{0} \n {1} \n{2}",Cu.ExcessNH3Colour, Cu.ExcessNH3Formula, Cu.ExcessNH3State),Brushes.DarkBlue ),
                new Tuple<string, Brush>(string.Format("{0} \n {1} \n{2}",Cu.saltRctColour, Cu.saltRctFormula, Cu.saltRctState),Brushes.Blue ),
                new Tuple<string, Brush>(string.Format("{0} \n {1} \n{2}",Cu.Na2CO3RctColour, Cu.Na2CO3RctFormula, Cu.Na2CO3RctState),Brushes.Yellow ),
                new Tuple<string, Brush>(string.Format("{0} \n {1} \n{2}",Cu.ClRctColour, Cu.ClRctFormula, Cu.ClRctState),Brushes.Blue );
            };
            break;
        default:
            break;
    }

    //ensure buttons, contents and colors are of the same size
    System.Diagnostics.Debug.Assert(buttons.Count != contents.Count || buttons.Count != colors.Count, "Lists are not same in size");

    //randomize the order
    Random rng = new Random();
    buttons.OrderBy(x => rng.Next());
    contentsAndColors.OrderBy(x => rng.Next());
    //assign content and color to buttons
    for(int iButton = 0; iButton < buttons.Count; ++iButton)
    {
        buttons[iButton].Content = contentsAndColors[iButton].Item1;
        buttons[iButton].Background = contentsAndColors[iButton].Item2;
    }
}

关于一般提示/批评:

OrderBy(x => rng.Next());语句结合了linq和lambda函数,这些函数在操作列表时非常有用,并且lambda函数通常在很多方面都很有用,我建议阅读它们。

关于OrderBy(x => rng.Next());:您是否为金属创建了枚举而不是使用字符串?通过这种方式,您可以安全地防止打字错误,因为编译器会为您检查这些错误。你可以这样简单地创建一个枚举:

public enum Metals
{ 
    Copper,
    Iron,
    Gold
}

稍后您可以这样使用其值:

switch (pickedMetal)
{ 
    case Metals.Copper:

如果按钮数量会在开发时间内发生变化,您还可以考虑在方法声明中使用参数列表:

public void SetButtonContent(TextBlock reagentText, TextBlock transMetalText, params Button[] opts)
{
    Button opt1 = opts[0];
}

params是一个关键字,它允许您在函数调用中编写多个对象,然后将其组合到一个数组中。因此,您先前的SetButtonContent来电不会受到伤害。