简体   繁体   English

单击按钮如何在C#中生成随机颜色?

[英]How can i generate random colors in C# with a button click?

I have a program which sends planes to different destination and it's implemented using threading. 我有一个程序,可以将飞机发送到不同的目的地,并且使用线程来实现。 But the problem is my planes don't generate in random colors on button click, it generates random colors whenever the program is run. 但是问题是我的飞机在单击按钮时不会生成随机颜色,而是在运行程序时生成随机颜色。 (I think this is because i haven't passed the method properly in the button clicking method) (我认为这是因为我没有在按钮单击方法中正确传递该方法)

Question is how can i change the code so that it generates random colors whenever the button is clicked? 问题是如何更改代码,以便每当单击按钮时它都会生成随机的颜色?

And my code is given below. 我的代码如下。 (There are few radio buttons which will prompt the user for the destination and when the button is clicked the planes will go) (只有少数单选按钮会提示用户输入目的地,单击该按钮时飞机将飞行)

my code for the button click instance 我的按钮单击实例代码

    private void rbutton1_checked(Object sender, System.EventArgs e)
    {
        if (((RadioButton)sender).Checked == true)         
            destination = 1;
    }

    private void rbutton2_checked(Object sender, System.EventArgs e)
    {
        if (((RadioButton)sender).Checked == true)
            destination = 2;
    }

    private void rbutton3_checked(Object sender, System.EventArgs e)
    {
        if (((RadioButton)sender).Checked == true)
            destination = 3;
    }

    private void rbutton4_checked(Object sender, System.EventArgs e)
    {
        if (((RadioButton)sender).Checked == true)
            destination = 4;
    }

    private void btn_Click(object sender,
                           System.EventArgs e)
    {
        if (destination == 0) MessageBox.Show("Select an Option Please!!");

        else
        {
            for (int q = 0; q <1000; q++)
            {
                locked = !locked;
                this.btn.BackColor = locked ? Color.Pink : Color.LightGreen;
                lock (this)
                {
                    if (!locked)
                        Monitor.Pulse(this);
                }
            }
        }
    }

And my code for the random color generation which is not in the same class ( In the Form) 我的代码是用于随机颜色生成的,它不在同一类中(在窗体中)

public Color generateRandomColor()
    {

        Random random = new Random();
        Color color = Color.FromArgb(random.Next(255), random.Next(255), random.Next(255));
        return color;

    }

Create on instance of Random class on load of your program. 在程序加载时在Random类的实例上创建。 Then when user click on button to generate random color, it will give you different values. 然后,当用户单击按钮生成随机颜色时,它将为您提供不同的值。 The problem is that you're always creating a new instance of the Random class, and this object starts with the same seed value by default, so you have always the same "random" number. 问题在于您总是在创建Random类的新实例,并且该对象默认情况下以相同的种子值开头,因此您始终具有相同的“随机数”。

But, pay attention that with code (in any language) you will get only a pseudo random. 但是请注意,使用任何语言的代码,您只会得到一个伪随机数。 To get really random number you must have specific hardware to this. 要获得真正的随机数,您必须对此具有特定的硬件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM