为什么属性网格显示的颜色不正确?

时间:2019-05-14 13:31:12

标签: c# winforms

我有一个属性网格,每当我单击面板上的矩形时,它就会显示该矩形的属性。除颜色外,所有属性都是正确的,颜色始终保持默认值(白色)。我想知道属性网格是否可以显示矩形的颜色。

以下是当前外观的屏幕截图:
example screenshot

这是我创建矩形的方式:
这是我的矩形课

internal class rectangle : Shape
{
        public int length { get; set; }
        public int width { get; set; }

        Random rand = new Random();
        Rectangle newRectangle;

        public override Rectangle GetRectangle()
        {
            return this.newRectangle;
        }

        private Color randomColor()
        {
            int r = rand.Next(0, 256); // Random number between 0 - 255
            int g = rand.Next(0, 256);
            int b = rand.Next(0, 256);

            Color color = Color.FromArgb(r, g, b);
            return color;
        }

        private Rectangle makeRectangle(int startx, int starty, int width, int length)
        {
            newRectangle = new Rectangle(startx, starty, width, length);
            return newRectangle;
        }

        public override void Draw(Graphics g)
        {
            using (SolidBrush brush = new SolidBrush(randomColor()))
            {
                g.FillRectangle(brush, makeRectangle(startx, starty, width, length));
            }
        }
}

矩形类是从Shape派生的

internal abstract class Shape
    {
        public Color color { get; set; }
        public int thickness { get; set; }
        public int startx { get; set; }
        public int starty { get; set; }

        public abstract Rectangle GetRectangle();   
        public virtual void Draw(Graphics g)
        {
        }
    }

在主窗体中,我绘制矩形:

// Rectangle 1 
List<Shape> shapes = new List<Shape>();
rectangle rectangle = new rectangle();
rectangle.startx = 100;
rectangle.starty = 200;
rectangle.width = 200;
rectangle.length = 100;
shapes.Add(rectangle);

rectangle.Draw(g);

以下是MouseClick事件以显示属性:

private void panel1_MouseClick(object sender, MouseEventArgs e)
{
            var currentPoints = e.Location;
            Boolean isRectangle = false;
            foreach (Shape shape in shapes)
            {
                if (shape.GetRectangle().Contains(currentPoints))
                {
                    isRectangle = true;
                    propertyGrid1.SelectedObject = shape;
                    objectType = shape.GetType();
                }
            }

            if (isRectangle)
            {
                //MessageBox.Show("This is a " + objectType);
                propertyGrid1.Show();

            }
}

根据提供的代码,我正在创建具有随机rgb颜色的SolidBrush。所以我希望属性网格显示正确的颜色。谢谢!

1 个答案:

答案 0 :(得分:2)

您正在使用Draw方法创建随机颜色来创建笔刷,但并未将此颜色分配给要在属性网格中显示的对象的任何属性。

您应该将此随机颜色分配给要在属性网格中显示的Shaperectangle对象。

rectangle rectangle = new rectangle();
rectangle.color = randomColor(); // <========================
rectangle.startx = 100;
rectangle.starty = 200;
rectangle.width = 200;
rectangle.length = 100;

然后在Draw方法中使用该颜色

using (SolidBrush brush = new SolidBrush(this.color))
{
    g.FillRectangle(brush, makeRectangle(startx, starty, width, length));
}