C#颜色随机帮助 [英] c# color random help

查看:85
本文介绍了C#颜色随机帮助的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要从数组中生成随机颜色,但是我必须预先选择6种颜色.

I need to generate a random color from an array but I have to preselect 6 colors.

Color[] cColors = new Color[6] { Color.Red, Color.Blue, Color.Yellow, Color.White, Color.Green, Color.Orange };



如果选择(1到6),则需要选择(1到6),并且只能使用它们绘制多个重复的形状.
如果我重新启动程序,它将生成另一组随机颜色.

尝试使用int数组生成随机数,然后在颜色数组中调用它,但似乎不起作用.

任何帮助将不胜感激:)



If I choose (1 to 6), then I need to pick (1 to 6) of them and be able to only use those to draw out multiple repeating shapes.
If I restart the program it will generate another set of random colors.

Tried using an int array to generate random numbers then call it in the color array but it doesnt seem to work.

Any help would be appreciated :)

推荐答案

在Google上搜索,您将获得许多链接

检查以下链接

http://social.msdn.microsoft.com/Forums/da/winforms/thread/d4fb42d3-e4f7-4a76-b650-d3b673d52912 [
Search on google you will get many links

Check following link

http://social.msdn.microsoft.com/Forums/da/winforms/thread/d4fb42d3-e4f7-4a76-b650-d3b673d52912[^]


尝试一下

通过System.Drawing使用此命名空间;
Try this

Use this namespace using System.Drawing;
 protected void btn_Click(object sender, EventArgs e)
    {
        Color[] cColors = new Color[6] { Color.Red, Color.Blue, Color.Yellow, Color.White, Color.Green, Color.Orange };
        Color[] cColorsTemp = new Color[6];
        int[] array = new int[] { 1, 2, 3, 4, 5, 6 };
        System.Random rnd = new System.Random();
        for (int i = array.Length - 1; i >= 0; i--)
        {
            int n = rnd.Next(array.Length - 1);
            cColorsTemp[i] = cColors[n];
            array = array.Where(val => val != n).ToArray();
            Color Temp = cColors[n];
            cColors = cColors.Where(val => val != Temp).ToArray();
        }
// use cColorsTemp array for your work 
    }


这篇关于C#颜色随机帮助的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆