5 Replies - 390 Views - Last Post: 27 June 2020 - 10:32 PM Rate Topic: -----

#1 albert003   User is offline

  • D.I.C Addict

Reputation: 38
  • View blog
  • Posts: 867
  • Joined: 15-December 14

Circle randomly changes colours to red, yellow, orange

Posted 15 June 2020 - 10:55 AM

I have a new question for you guys. I wanted to put a smiley face on the screen with a smiley face which changes colour. I figured out how to draw circles but I cant figure out how to make it randomly change from orange,red and yellow.


I know how to make random numbers and strings in C#. I know how to draw an ellipse and make it into a circle. I just can't figure out how to make the circle randomly change colours.

This is how I would do it to show random numbers.
using System.IO;
using System;

class Program
{
    static void Main()
    {
        Random num = new Random();

        
        for(int x = 0; x < 10; x++)
        {
            int a = num.Next(1,10);
            int b = num.Next(1,10);
            int c = num.Next(1,10);
            Console.WriteLine(a+","+b+","+c);
        }
    }
}


Is This A Good Question/Topic? 0
  • +

Replies To: Circle randomly changes colours to red, yellow, orange

#2 DarenR   User is offline

  • D.I.C Lover

Reputation: 705
  • View blog
  • Posts: 4,524
  • Joined: 12-January 10

Re: Circle randomly changes colours to red, yellow, orange

Posted 15 June 2020 - 11:00 AM

Color randomColor = Color.FromArgb(rnd.Next(256), rnd.Next(256), rnd.Next(256));

        BackColor = randomColor;

Was This Post Helpful? 1
  • +
  • -

#3 albert003   User is offline

  • D.I.C Addict

Reputation: 38
  • View blog
  • Posts: 867
  • Joined: 15-December 14

Re: Circle randomly changes colours to red, yellow, orange

Posted 15 June 2020 - 12:32 PM

I found something similar to that online, it makes the whole back ground change random colours. My goal is to only make the circle which I've drawn change random colours only.
Was This Post Helpful? 0
  • +
  • -

#4 Skydiver   User is offline

  • Code herder
  • member icon

Reputation: 7404
  • View blog
  • Posts: 24,977
  • Joined: 05-May 12

Re: Circle randomly changes colours to red, yellow, orange

Posted 15 June 2020 - 12:35 PM

Or if simply a randomly picking between red, orange, or yellow:
var colors = new Color[] { Color.Red, Color.Orange, Color.Yellow };
var index = rnd.Next(colors.Length);
BackColor = colors[index];


Was This Post Helpful? 0
  • +
  • -

#5 Skydiver   User is offline

  • Code herder
  • member icon

Reputation: 7404
  • View blog
  • Posts: 24,977
  • Joined: 05-May 12

Re: Circle randomly changes colours to red, yellow, orange

Posted 15 June 2020 - 12:42 PM

Amending post #4 due to not seeing post #3.
var colors = new Color[] { Color.Red, Color.Orange, Color.Yellow };
var index = rnd.Next(colors.Length);
var randomColor = colors[index];
var brush = new SolidBrush(randomColor);
graphics.FillEllipse(brush, ...);



Just like when we were recommending that you pick up Programming Windows (5th Edition) by Charles Petzold when you were trying to learn Windows programming in C/C++, I recommend picking up the latest edition of Programming Windows by Charles Petzold with covers WinForms.

But if you are truly on the learning curve, I suggest skipping WinForms and going to straight to WPF. There are (many) bad habits that one can pickup while learning to program in WinForms that will have to unlearn when you are doing WPF. So might as well not even pick them up.
Was This Post Helpful? 1
  • +
  • -

#6 albert003   User is offline

  • D.I.C Addict

Reputation: 38
  • View blog
  • Posts: 867
  • Joined: 15-December 14

Re: Circle randomly changes colours to red, yellow, orange

Posted 27 June 2020 - 10:32 PM

I haven't picked up the book, yet. I was laid off and haven't been able to find a job. I plan to buy the book once I land a job. I am trying not to pick up and bad habits by watching different video from different people explaining how to do C#. As well as using my googlefu to look up things.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1