Solved

Color public property of custom user control problem

Posted on 2008-06-24
6
1,657 Views
Last Modified: 2010-04-21
I have created a partially working custom control that basically draws a grid of lines.

The two public color properties do not seem to work.

When I add the control to a new project, in the properties window, I select the color and the color does not change. After that first attempt, I cannot even select a color.

Attached is the code for the control, and at the bottom are the two color properties: 'BorderColor', and 'CellColor'
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
 
namespace gridCustomControl
{
    public partial class GridCustomControl : UserControl
    {
        private int _cols;
        private int _rows;
        private int _width;     // Cell width
        private int _height;    // Cell height
        private float _zoom;
        private Color _cellColor;
        private Color _borderColor;
 
        public GridCustomControl()
        {
            InitializeComponent();
 
            // The following SetStyles are needed for double buffering:
            SetStyle(ControlStyles.DoubleBuffer, true);
            SetStyle(ControlStyles.AllPaintingInWmPaint, true);
            SetStyle(ControlStyles.UserPaint, true);
 
            _cols = Cols;
            _rows = Rows;
            _width = CellWidth;
            _height = CellHeight;
            _cellColor = CellColor;
            _borderColor = BorderColor;
 
            this.ControlSize = new Size(_width * _cols, _height * _rows);
        }
 
        protected override void OnPaint(PaintEventArgs e)
        {
            base.OnPaint(e);
            Graphics g = e.Graphics;
            g.FillRectangle(Brushes.White, this.ClientRectangle);
 
            int cellHeight = this.CellHeight;   
            int cellWidth = this.CellWidth; 
            int nbrColumns = this.Cols;
            int nbrRows = this.Rows;
 
            // Draw cells
            for (int row = 0; row < nbrRows; row++)
            {
                for (int col = 0; col < nbrColumns; col++)
                {
                    Point cellLocation = new Point(col * cellWidth, row * cellHeight);
                    Rectangle cellRect = new Rectangle(cellLocation.X,
                                                       cellLocation.Y,
                                                       cellWidth, cellHeight);
 
                    if (cellRect.IntersectsWith(e.ClipRectangle))
                    {
                        g.FillRectangle(new SolidBrush(_cellColor), cellRect);
                        g.DrawRectangle(new Pen(_borderColor, 1), cellRect);
                    }
                    else
                    {
                        // Do nothing...
                    }
                }
            }
            this.Invalidate(); // Redraw the whole control
        }
 
        [Browsable(true),
            DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
 
        public int Cols
        {
            get { return _cols; }
            set { _cols = value; }
        }
 
        public int Rows
        {
            get { return _rows; }
            set { _rows = value; }
        }
 
        public int CellWidth
        {
            get { return _width; }
            set { _width = value; }
        }
 
        public int CellHeight
        {
            get { return _height; }
            set { _height = value; }
        }
 
        public float Zoom
        {
            get { return _zoom; }
            set { _zoom = value; }
        }
 
        public Size ControlSize
        {
            // This property references the 'this' because there
            // aren't any private members that need to be created
            // to hold the size value.
 
            get { return this.Size; }
            set 
            {
                _width = value.Width;
                _height = value.Height;
 
                // 1 is added otherwize the most very most right line and
                // the very most bottom line will not show.
                this.Size = new Size(_width * _cols + 1, _height * _rows + 1);
 
                this.Invalidate(); // Redraw the whole control
            }
        }
 
        public Color CellColor
        {
            get { return _cellColor; }
            set { _cellColor = value; }
        }
 
        public Color BorderColor
        {
            get { return _borderColor; }
            set { _borderColor = value; }
        }
    }
}

Open in new window

0
Comment
Question by:yaronusa
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
6 Comments
 
LVL 96

Accepted Solution

by:
Bob Learned earned 500 total points
ID: 21857245
Try this.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
 
public partial class GridControl : UserControl
{
    private int _cols = 3;
    private int _rows = 3;
    private int _width = 100;     // Cell width
    private int _height = 100;    // Cell height
    private float _zoom;
    private Color _cellColor = Color.White;
    private Color _borderColor = Color.Black;
 
    public GridControl()
    {
        // The following SetStyles are needed for double buffering:
        this.SetStyle(ControlStyles.DoubleBuffer | ControlStyles.AllPaintingInWmPaint | ControlStyles.UserPaint, true);
 
        this.SetControlSize();
    }
 
    protected override void OnPaint(PaintEventArgs e)
    {
        Graphics g = e.Graphics;
        g.FillRectangle(Brushes.White, this.ClientRectangle);
 
        int cellHeight = this.CellHeight;
        int cellWidth = this.CellWidth;
        int nbrColumns = this.Columns;
        int nbrRows = this.Rows;
 
        // Draw cells
        for (int row = 0; row < nbrRows; row++)
        {
            for (int col = 0; col < nbrColumns; col++)
            {
                Rectangle cellRect = new Rectangle(col * cellWidth, row * cellHeight, cellWidth, cellHeight);
 
                if (cellRect.IntersectsWith(e.ClipRectangle))
                {
                    g.FillRectangle(new SolidBrush(_cellColor), cellRect);
                    g.DrawRectangle(new Pen(_borderColor, 1), cellRect);
                }
                else
                {
                    // Do nothing...
                }
            }
        }
    }
 
    [Browsable(true),
        DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
 
    public int Columns
    {
        get { return _cols; }
        set
        {
            _cols = value;
            this.SetControlSize();
        }
    }
 
    public int Rows
    {
        get { return _rows; }
        set
        {
            _rows = value;
            this.SetControlSize();
        }
    }
 
    public int CellWidth
    {
        get { return _width; }
        set
        {
            _width = value;
            this.SetControlSize();
        }
    }
 
    public int CellHeight
    {
        get { return _height; }
        set
        {
            _height = value;
            this.SetControlSize();
        }
    }
 
    public float Zoom
    {
        get { return _zoom; }
        set
        {
            _zoom = value;
            this.Invalidate();
        }
    }
 
    public void SetControlSize()
    {
        // 1 is added otherwize the most very most right line and
        // the very most bottom line will not show.
        this.Size = new Size(_width * _cols + 1, _height * _rows + 1);
 
        this.Invalidate(); // Redraw the whole control
    }
 
    [Browsable(false), DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
    public new Size Size
    {
        get { return base.Size; }
        set { base.Size = value; }
    }
 
    public Color CellColor
    {
        get { return _cellColor; }
        set
        {
            _cellColor = value;
            this.Invalidate();
        }
    }
 
    public Color BorderColor
    {
        get { return _borderColor; }
        set
        {
            _borderColor = value;
            this.Invalidate();
        }
    }
}

Open in new window

0
 

Author Comment

by:yaronusa
ID: 21859160
I put your code in and got on error on the custom control designer:

The error is:

Error      1      'gridCustomControl.GridCustomControl.Dispose(bool)': no suitable method found to override      C:\Users\PMC TB3\Documents\Visual Studio 2005\Projects\gridCustomControl\gridCustomControl\GridCustomControl.Designer.cs      14      33      gridCustomControl

But I think one difference I see is that you put default values...
namespace gridCustomControl
{
    partial class GridCustomControl
    {
        /// <summary>
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.IContainer components = null;
 
        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
 
        //ERROR ON THIS NEXT LINE:
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }
 
        #region Component Designer generated code
 
        /// <summary>
        /// Required method for Designer support - do not modify 
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            this.SuspendLayout();
            // 
            // GridCustomControl
            // 
            this.Name = "GridCustomControl";
            this.Size = new System.Drawing.Size(300, 239);
            this.ResumeLayout(false);
 
        }
 
        #endregion
 
 
 
    }
}

Open in new window

0
 

Author Comment

by:yaronusa
ID: 21859216
I also see that you do not call initialize component, I'm trying to implement your suggestions as I type this...
0
Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

 

Author Comment

by:yaronusa
ID: 21859700
OK, it worked! I'm sure you aren't TOO surprised though! Thanks for saving me a ton of time...

I would have told it worked sooner, but my internet connection went down and I actually drove to Kinkos to make sure you weren't continuing to figure it out... THANK YOU!
0
 

Author Comment

by:yaronusa
ID: 21859728
I tried to distribute the points... but I get this message:

Notice: The Asker has requested that this comment be accepted as the solution and the points be refunded to the Asker's account.

I'm assuming that is you and all is well... thanks again.
0
 

Author Closing Comment

by:yaronusa
ID: 31470143
THANK YOU!!!!!!!!!!!
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

As game developers, we quickly learn that Artificial Intelligence (AI) doesn’t need to be so tough.  To reference Space Ghost: “Moltar, I have a giant brain that is able to reduce any complex machine into a simple yes or no answer. (http://www.youtu…
Recently, in one of the tech-blogs I usually read, I saw a post about the best-selling video games through history. The first place in the list is for the classic, extremely addictive Tetris. Well, a long time ago, in a galaxy far far away, I was…
In this video, viewers will be given step by step instructions on adjusting mouse, pointer and cursor visibility in Microsoft Windows 10. The video seeks to educate those who are struggling with the new Windows 10 Graphical User Interface. Change Cu…
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question