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());
        }


    }
}

Dani AI

Generated

The exception comes from passing raw mouse coordinates into Color.FromArgb. Color component values must be 0–255, so when the form is maximized the mouse X/Y can easily exceed that range. hit the core issue: the existing range checks reset r/b/g, but the actual call uses x and y, so those checks never prevent the error. Also, doing g += 1 inside the Color.FromArgb call makes the increment and bounds logic harder to reason about.

A robust approach is to map the mouse position into the 0–255 range relative to the client area (keeps the color change proportional at any window size), clamp the results, and increment any cycling channel before the call. Example pattern:

// inside MouseMove with left button down
int r = (x * 255) / Math.Max(1, this.ClientSize.Width);
r = Math.Min(255, Math.Max(0, r));
int g = (y * 255) / Math.Max(1, this.ClientSize.Height);
g = Math.Min(255, Math.Max(0, g));
b++;
if (b > 255) b = 0;
BackColor = Color.FromArgb(r, g, b);

Alternatives: wrapping with modulo (as suggested) keeps values in range but produces repeating bands; simple clamping will saturate to 0/255. Important notes: guard against negative e.Location (mouse outside client area), avoid incrementing inside the FromArgb call (increment first), and reduce work in MouseMove (throttle updates or require a small delta) to avoid excessive repaints. Finally, confirm that the variables actually used for R/G/B are the ones being checked and updated — the original code declared r and b but passed x/y instead, which is why the existing checks had no effect.

Recommended Answers

All 5 Replies

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

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

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.

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

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

>>> modulo operator:

http://msdn.microsoft.com/en-us/library/0w4e0fzs.aspx

>>> code tags:

Select your code click in the message window on (CODE), now your code is surrounded by tags. If you are posting C# code, make the start tag look like (CODE)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.