Это полный исходный код для пользовательского элемента управления.Он работает как есть.
В форме я создал два экземпляра элемента управления и могу нажать на них.Как и я, квадраты переключаются с красного на синий.
Вы должны иметь возможность устанавливать точки останова в методах OnGotFocus и OnLostFocus.Недействительные есть для перерисовки.
Волшебство в том, что есть два элемента управления (как и у вас), но кроме того, переопределение OnButtonclicked устанавливает фокус.Я полагаю, что это часть, которая отсутствует в вашем примере.
using System;
using System.Drawing;
using System.Windows.Forms;
namespace DesktopApp1
{
public partial class MyView : System.Windows.Forms.UserControl
{
protected Color SelectedColor { get; set; } = Color.Red;
protected Color NormalColor { get; set; } = Color.Blue;
protected override void OnPaint(PaintEventArgs e)
{
using (SolidBrush blueBrush = new SolidBrush(this.Focused?SelectedColor:NormalColor))
using (Pen blackPen = new Pen(Color.Black, 3))
{
e.Graphics.FillRectangle(blueBrush, ClientRectangle);
Rectangle inset = new Rectangle(this.ClientRectangle.X + 1, this.ClientRectangle.Y + 1, this.ClientRectangle.Width -3 , this.ClientRectangle.Height - 3);
e.Graphics.DrawRectangle(blackPen, inset);
}
base.OnPaint(e);
}
private void OnButton1Clicked(object sender, EventArgs e)
{
this.Select();
}
protected override void OnGotFocus(EventArgs e)
{
Invalidate();
base.OnGotFocus(e);
}
protected override void OnLostFocus(EventArgs e)
{
base.OnLostFocus(e);
Invalidate();
}
}
}
Чтобы завершить ответ, вот форма 1.Designer.cs
namespace DesktopApp1
{
partial class Form1
{
/// <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>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows Form 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.myView2 = new DesktopApp1.MyView();
this.myView1 = new DesktopApp1.MyView();
this.SuspendLayout();
//
// myView2
//
this.myView2.Location = new System.Drawing.Point(189, 12);
this.myView2.Name = "myView2";
this.myView2.Size = new System.Drawing.Size(150, 150);
this.myView2.TabIndex = 1;
//
// myView1
//
this.myView1.Location = new System.Drawing.Point(9, 12);
this.myView1.Name = "myView1";
this.myView1.Size = new System.Drawing.Size(150, 150);
this.myView1.TabIndex = 0;
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 16F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(355, 175);
this.Controls.Add(this.myView2);
this.Controls.Add(this.myView1);
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayout(false);
}
#endregion
private MyView myView1;
private MyView myView2;
}
}