类似于按钮选中时颜色会变化

解决方案 »

  1.   

    自定义什么了?lable textbox gridview combobox?
      

  2.   

    自定义了一个控件,控件里面包含Panel PictureBox,label
      

  3.   


    private Color pictureBoxBorderColor = Color.Black;        private void pictureBox1_Paint(object sender, PaintEventArgs e)
            {
                ControlPaint.DrawBorder(e.Graphics,
                                    this.pictureBox1.ClientRectangle,
                                    pictureBoxBorderColor, 1, ButtonBorderStyle.Solid,
                                    pictureBoxBorderColor, 1, ButtonBorderStyle.Solid,
                                    pictureBoxBorderColor, 1, ButtonBorderStyle.Solid,
                                    pictureBoxBorderColor, 1, ButtonBorderStyle.Solid);
            }        private void pictureBox1_Click(object sender, EventArgs e)
            {
                if (pictureBoxBorderColor == Color.Black)
                {
                    pictureBoxBorderColor = Color.Red;
                }
                else
                {
                    pictureBoxBorderColor = Color.Black;
                }
                pictureBox1.Invalidate();
                pictureBox1.Update();
            }
      

  4.   

    panel中多个picturebox  红边框代表选中 黑边框代表未选中private void Init()
            {
                foreach (var item in panel1.Controls)
                {
                    if(item.GetType()==typeof(PictureBox))
                    {
                        PictureBox picture = (PictureBox)item;
                        picture.Paint += new PaintEventHandler(picturePaint);
                        picture.Click += new EventHandler(pictureClick);                    pictureColorDic.Add(picture, Color.Black);
                    }
                }
            }        private Dictionary<PictureBox, Color> pictureColorDic = new Dictionary<PictureBox, Color>();        void picturePaint(object sender, PaintEventArgs e)
            {
                var pictureControl = (sender as PictureBox);            var color = pictureColorDic.FirstOrDefault(a => a.Key == pictureControl).Value;
                if (color == null) { return; }            ControlPaint.DrawBorder(e.Graphics, pictureControl.ClientRectangle,
                    color, 1, ButtonBorderStyle.Solid,
                    color, 1, ButtonBorderStyle.Solid,
                    color, 1, ButtonBorderStyle.Solid,
                    color, 1, ButtonBorderStyle.Solid);        }        void pictureClick(object sender, EventArgs e)
            {
                var pictureControl = (sender as PictureBox);            var dic = pictureColorDic.FirstOrDefault(a => a.Key == pictureControl);
                if (dic.Key != null)
                {
                    pictureColorDic.Remove(pictureControl);
                    if (dic.Value == Color.Black)
                    {
                        pictureColorDic.Add(pictureControl, Color.Red);
                    }
                    else
                    {
                        pictureColorDic.Add(pictureControl, Color.Black);
                    }
                }
                pictureControl.Invalidate();
                pictureControl.Update();
            }