hey guys i just started using C# im trying to change the backcolor when the mouse if pressed and moved, it works fine when the window is small, however when i maximize the window the colors stop changing and keeps on crashing saying that Color.FromArgb(x,y,g+=1)
cannot exceed 255,i tried preventing this using an if statement but still doesnt work. thanks in advance :)

this is my code so far:

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

namespace _4
{
    class Class1 : Form
    {
        int x;
        int y;
        int q=0;
        int r = 20;
        int b = 80;
        int g = 2;
        Point loc;
        Class1()
        {
            this.MouseMove += new MouseEventHandler(Class1_MouseMove);
            BackColor = Color.FromArgb(15, 150, 144);


        }



        void Class1_MouseMove(object sender, MouseEventArgs e)
        {

            if (e.Button == System.Windows.Forms.MouseButtons.Left)
            {
                loc = e.Location;

                x = loc.X;
                y = loc.Y;

                if(r>=255 ||r<0)
                {
                    r = 0;
                }
                if (b >= 255 || b < 0)
                {
                    b = 0;
                } if (g >= 255 || g < 0)
                {
                    g = 0;
                }

                BackColor = Color.FromArgb(x, y, g += 1);

            }

        }

        static void Main()
        {
            Application.Run(new Class1());
        }


    }
}

Edited 3 Years Ago by Nick Evan: Fixed formatting

PLease use code tags when posting code.
Maybe you could apply a modulo operator.

Myvalue = MyOtherValue % 256; // Myvalue will always between 0 and 255
Comments
The proper way to do this ;)

Looks like your problem is that you're not checking the range of the x any y variables that are used as input to Color.FromArgb call. They should be in the range of 0 to 255. You're correctly checking the range of the g variable.

Edited 3 Years Ago by Nick Evan: Fixed formatting

PLease use code tags when posting code.
Maybe you could apply a modulo operator.

Myvalue = MyOtherValue % 256; // Myvalue will always between 0 and 255

eh sorry but what are code tags and a modulo operator ?

Looks like your problem is that you're not checking the range of the x any y variables that are used as input to Color.FromArgb call. They should be in the range of 0 to 255. You're correctly checking the range of the g variable.

okay im gonna try that...thnx =D

This article has been dead for over six months. Start a new discussion instead.