yaronusa
asked on
Color public property of custom user control problem
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'
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; }
}
}
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
I also see that you do not call initialize component, I'm trying to implement your suggestions as I type this...
ASKER
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!
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!
ASKER
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.
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.
ASKER
THANK YOU!!!!!!!!!!!
ASKER
The error is:
Error 1 'gridCustomControl.GridCus
But I think one difference I see is that you put default values...
Open in new window