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


    }
}

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.