Thread: Color not working when using an mouse event.

  1. #1
    Registered User
    Join Date
    Oct 2006
    Location
    UK/Norway
    Posts
    485

    Color not working when using an mouse event.

    First of all, im not really sure how to explain this very well as I am finding it very very strange.

    I am trying to draw a grid into a form and have the colours of the grid change if they are selected by the user.

    Here is my draw and mouse event handler:
    Code:
     private void OnPaint(object sender, PaintEventArgs e)
            {
                Painter.Instance.ClearScreen(Color.Blue);
                //m_mainGrid[0].IsSelected = true;
                foreach (GridNode node in m_mainGrid)
                    node.Draw(true, true);
            }
    
            private void OnClick(object sender, EventArgs e)
            {
                m_mainGrid[0].IsSelected = true;
                Invalidate();
            }
    GridNode.Draw is defined like this:
    Code:
    public void Draw(bool showId, bool showPos)
            {
                if (IsSelected)
                    Painter.Instance.FillBorderRectangle(Rect, Color.Green, Color.Black);
                else
                    Painter.Instance.FillBorderRectangle(Rect, Color.Red, Color.Black);
            }
    The problem I am having is that even when the node is selected it gets drawn as red. The debugger breaks into the DrawMethod and sets it to Green and it never breaks into the line where it goes to red. But still the image on my screen is red.

    If I uncomment the m_mainGrid[0].IsSelected = true line, it does show up as green.

    Any ideas?

  2. #2
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    Don't you want to be using e.Graphics from the OnPaint() method to draw with? What is your Painter class?
    If you understand what you're doing, you're not learning anything.

  3. #3
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    There can be a lot of things wrong. Try to exclude some situations like
    1) Maybe you have overlapping nodes? Try only with one (no foreach)
    2) Is the code working, I mean is it re-drawing something i.e. initially it is black and becomes red, even though you want it green, but at least it re-draws the node.

    But e.Graphics is probably what you want. You can use your design and pass the e.Graphics to function Draw and use something like Graphics.FillRectangle(..) and the such to do what you want from the Draw function

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 8
    Last Post: 01-18-2008, 04:06 AM
  2. Friggin Mouse on Laptop
    By OneStiffRod in forum Tech Board
    Replies: 2
    Last Post: 03-27-2003, 07:49 PM
  3. Game Design Topic #2 - Keyboard or Mouse?
    By TechWins in forum Game Programming
    Replies: 4
    Last Post: 10-08-2002, 03:34 PM
  4. Mouse + Animation Trouble
    By destroyerBEACON in forum A Brief History of Cprogramming.com
    Replies: 8
    Last Post: 05-17-2002, 06:54 AM
  5. Mouse Input????
    By Brandon in forum Game Programming
    Replies: 1
    Last Post: 11-11-2001, 10:04 AM