由于要求MyRect要是只读属性,不能执行invalidate,即使我将它改成非只读属性,并在set{}中添加invalidate方法,它也并不执行.大家帮帮忙

解决方案 »

  1.   

     public void Add(myRect rect)
                {
                    List.Add(rect);
                    //试试在这里进行重绘
                }
                
      

  2.   

    不行,那里没有办法调用invalidate,但是我在add和remove下添加了事件,但似乎只有初始化控件的时候触发了事件,其他时候都没有触发.
      

  3.   

    感觉你的处理逻辑存在问题。不是没有触发,是没给它响应的时间。
    invalidate 之后调用 update试试。或者直接调用refresh试试。
    this.invalidate();
    this.update();
    或者
    this.refresh();
      

  4.   

    这样不能解决问题,我在design time状态下要及时刷新,而不是run time下
      

  5.   

    对,但是什么时候响应呢?应该是添加了新的项,或者是删除了项,又或者更改了项的属性值是进行刷新,那么我要对myRect添加事件,当集合里面的内容有变化时,进行invalidate.但是如何为myRect添加事件,我不太清楚
      

  6.   

    try:        public partial class Userconrol : Control
            {
                public Userconrol()
                {
                    myRect.MyRectChanged += myRect_MyRectChanged;
                }            void myRect_MyRectChanged(object sender, EventArgs e)
                {
                    Invalidate();
                }            private MyRectCollectin myRect = new MyRectCollectin();            public class MyRectCollectin : CollectionBase
                {
                    public event EventHandler MyRectChanged;                public myRect this[int index]
                    {
                        get
                        {
                            return (myRect)List[index];
                        }
                    }
                    public void Add(myRect rect)
                    {
                        List.Add(rect);
                        if (MyRectChanged != null)
                            MyRectChanged(rect, null);
                    }
                    public void Remove(int index)
                    {
                        if (index > Count - 1 || index < 0)
                        {
                            MessageBox.Show("Index not valid!");
                        }
                        else
                        {
                            List.RemoveAt(index);
                            if (MyRectChanged != null)
                                MyRectChanged(index, null);
                        }
                    }
                }
            }
      

  7.   


    试过了,仍然没有立即刷新,添加集合中新的项时,在add处添加断点,似乎并没有走进去.因此没有触发这个事件,但是集合中的count确实增加了1.
      

  8.   

    public class MyCircle 
        {
            private Point position;
            private int radius;
            public Point Position
            {
                get
                {
                    return position;
                }
                set
                {
                    position = value;
                }
            }
            public int Radius
            {
                get
                {
                    return radius;
                }
                set
                {
                    radius = value;
                }
            }
        }
        public partial class MyButton : Control
        {
      public MyCircleCollection m_MyCircle = new MyCircleCollection();
         public MyButton()
            {
                this.SetStyle(ControlStyles.OptimizedDoubleBuffer | ControlStyles.AllPaintingInWmPaint | ControlStyles.UserPaint, true);
                m_MyCircle.ItemChanged += OnItemChanged;
            }
            public void OnItemChanged(object sender,EventArgs e)
            {
                this.Invalidate();
            }
            public class MyCircleCollection : CollectionBase
            {   
                public event EventHandler ItemChanged;            public MyCircle this[int index]
                {
                    get
                    {
                        return (MyCircle)List[index];
                    }
                }
                public void Add(MyCircle circle)
                {
                    List.Add(circle);
                    ItemChanged(this, null);
                }
                public void Remove(int index)
                {
                    if (index > Count - 1 || index < 0)
                    {
                        MessageBox.Show("Index not valid!");
                    }
                    else
                    {
                        List.RemoveAt(index);
                    }
                    ItemChanged(this, null);
                }
               
            }
      protected override void OnPaint(PaintEventArgs e)
            {
    if (MyCircle.Count != 0)
                {
                    foreach (MyCircle c in MyCircle)
                    {
                        e.Graphics.DrawEllipse(Pens.Black, c.Position.X, c.Position.Y, c.Radius, c.Radius);//Draw circle
                    }
                }
            }
    }
    大致是这样,就是在一个自定义控件上画圆圈,其实和你的代码差不多.但是不起效果.
      

  9.   

    请问如何判断委托事件有没有被执行?比如在design time下我添加了一个圆圈吧,然后run一下,itemchanged事件这时被调用了,圆圈也在控件上画出来了,但是如果我在propertygrid中再添加一个圆圈在集合里,那么第二个圆圈没有被画上去,但是把鼠标移到控件内,那个圆圈就出来了.因为我在mouseenter中进行了重绘.
      

  10.   

    测试了一下,没有问题:    public class MyCircle
        {
            private Point position;
            private int radius;
            public Point Position
            {
                get
                {
                    return position;
                }
                set
                {
                    position = value;
                }
            }
            public int Radius
            {
                get
                {
                    return radius;
                }
                set
                {
                    radius = value;
                }
            }
        }
        public partial class MyButton : Control
        {
            public MyCircleCollection m_MyCircle = new MyCircleCollection();
            public MyButton()
            {
                //这句得去掉this.SetStyle(ControlStyles.OptimizedDoubleBuffer | ControlStyles.AllPaintingInWmPaint | ControlStyles.UserPaint, true);
                m_MyCircle.ItemChanged += OnItemChanged;
            }
            public void OnItemChanged(object sender, EventArgs e)
            {
                this.Invalidate();
            }
            public class MyCircleCollection : CollectionBase
            {
                public event EventHandler ItemChanged;            public MyCircle this[int index]
                {
                    get
                    {
                        return (MyCircle)List[index];
                    }
                }
                public void Add(MyCircle circle)
                {
                    List.Add(circle);
                    ItemChanged(this, null);
                }
                public void Remove(int index)
                {
                    if (index > Count - 1 || index < 0)
                    {
                        MessageBox.Show("Index not valid!");
                    }
                    else
                    {
                        List.RemoveAt(index);
                    }
                    ItemChanged(this, null);
                }        }
            protected override void OnPaint(PaintEventArgs e)
            {
                if (m_MyCircle.Count != 0)
                {
                    foreach (MyCircle c in m_MyCircle)
                    {
                        e.Graphics.DrawEllipse(Pens.Black, c.Position.X, c.Position.Y, c.Radius, c.Radius);//Draw circle
                    }
                }
            }
        }调用:    public partial class Form3 : Form
        {
            public Form3()
            {
                InitializeComponent();            this.Load += Form3_Load;
            }        void Form3_Load(object sender, EventArgs e)
            {
                MyButton btn = new MyButton();
                btn.Name = "myBtn";
                btn.Width = this.Width;
                btn.Height = this.Height;
                btn.Location = new Point(5, 5);
                this.Controls.Add(btn);        }        private void button1_Click(object sender, EventArgs e)
            {
                MyButton btn = this.Controls["myBtn"] as MyButton;
                Random r = new Random();
                btn.m_MyCircle.Add(new MyCircle { Position = new Point(r.Next(0, this.Width / 2), r.Next(this.Height / 2)), Radius = 10 });
            }
        }
      

  11.   

    您这样用代码去添加的确没问题,要直接在属性栏上更改,就像更改其他控件一样.点击MyCircle属性,会跳出一个dialog,然后添加半径圆心.点击ok,关闭dialog,这时应该添加的圆已经画在控件上了,但是实际却没有.代码也的确生成了.就是没有重绘.
      

  12.   

    这个我解决不了……
    您接着等大神吧,
    good luck