2 Replies - 9594 Views - Last Post: 06 November 2009 - 05:30 PM Rate Topic: -----

#1 Alderdragon  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 50
  • Joined: 06-November 09

Creating a new color: FromArgb?

Post icon  Posted 06 November 2009 - 04:51 PM

Hello :)

I'm having a slight complication when trying to create a new color using the FromArgb method in my Windows Application in C#.

The goal of my program is to create a new color at random, using three randomly generated numbers from 0-255. That color is then used as the BackColor of the window, randomly generated each time the program compiles.

Here is what I currently have:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace BackColor
{
	public partial class Form1 : Form
	{
		public Form1()
		{
			InitializeComponent();
		}

		private void Form1_Paint(object sender, PaintEventArgs e)
		{


			Graphics g = e.Graphics;
			Random generator = new Random(DateTime.Now.Millisecond);
			int red = generator.Next(256);
			int green = generator.Next(256);
			int blue = generator.Next(256);
			Color newCol = new Color.FromArgb(red, green, blue);
			this.BackColor = newCol;
		}
	}
}



There is one error message that reads: "'System.Drawing.Color.FromArgb(int)' is a 'method' but is used like a 'type'. Can anyone give me a tip to use FromArgb correctly? Thanks!

Is This A Good Question/Topic? 0
  • +

Replies To: Creating a new color: FromArgb?

#2 Momerath  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1012
  • View blog
  • Posts: 2,444
  • Joined: 04-October 09

Re: Creating a new color: FromArgb?

Posted 06 November 2009 - 05:15 PM

Color newCol = new Color.FromArgb(red, green, blue);

Get rid of the 'new' keyword. You are trying to use it as a constructor (the 'type' error) when it's a static method.
Was This Post Helpful? 1
  • +
  • -

#3 Alderdragon  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 50
  • Joined: 06-November 09

Re: Creating a new color: FromArgb?

Posted 06 November 2009 - 05:30 PM

View PostMomerath, on 6 Nov, 2009 - 04:15 PM, said:

Color newCol = new Color.FromArgb(red, green, blue);

Get rid of the 'new' keyword. You are trying to use it as a constructor (the 'type' error) when it's a static method.


That solved the problem and it works now - Thanks :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1