Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何随机分配按钮内容和颜色?_C#_Wpf - Fatal编程技术网

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

C# 如何随机分配按钮内容和颜色?,c#,wpf,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)

我想不出一种方法来随机分配按钮的内容和匹配颜色,如果有人能帮助我,我将非常感激

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

    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语句中只包含了一个case with,只是为了让它更易于阅读,但在我的代码中有6个case,但它们都执行几乎相同的函数

但我想随机分配按钮内容,而不是手动分配,但我不确定如何做到这一点


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

一个简单的解决方案是将您的按钮、内容和颜色存储在列表中,随机排列,然后按如下方式分配它们的值:

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;
    }
}
稍后,您可以通过以下方式使用其值:

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
不会受到影响。

只是想澄清一下:您有8个按钮和8个内容,您想将8个内容随机分配给8个按钮吗?@goobering是的:)非常感谢!我遇到的问题是每个按钮都必须是特定的颜色,而不是随机的,因此,例如,如果按钮包含hexaaqua字符串,则它需要为蓝色,因此我将获得错误代码“类型异常”System.ArgumentOutOfRangeException“发生在mscorlib.dll中,但未在用户代码中处理其他信息:索引超出范围。必须为非负且小于集合的大小。“,这意味着什么?您应该检查发生此异常的行,通常这意味着您尝试访问不存在的枚举(如列表)元素。如果你的颜色只有5个元素,你可以调用颜色[9]。还要注意的是,索引从0开始,因此,如果枚举中有1个元素,则可以通过listVariable[0]访问该元素。我编辑了答案,因此颜色现在取决于内容,因为它们绑定到随机列表中的同一元素。请确保编辑您的问题以反映这种不同的请求行为