检测2个UserControl实例之间的切换

时间:2019-03-11 19:57:29

标签: c# winforms

我有一个源自UserControl的类(如下)。用户一次可以打开此控件的多个实例。 我希望能够检测到用户何时从一个切换到另一个,即单击了一个,然后又单击了另一个。

OnEnterOnLoad仅在首次显示控件时被调用
从未调用OnLeave
从未调用OnGotFocusOnLostFocus,但显然不鼓励使用它们。
我该怎么办?

 public partial class MyView : System.Windows.Forms.UserControl
 {
         // Before `OnLoad`
         // WM_NCCREATE, WM_NCCALCSIZE WM_CREATE, WM_SIZE, WM_MOVE,WM_REFLECT, WM_SHOWWINDOW, WM_PARENTNOTIFY
         // After `OnLoad`, Before `OnEnter`
         // WM_WINDOWPOSCHANGING, WM_NCCALCSIZE, WM_WINDOWPOSCHANGED, WM_SIZE, WM_GETTEXTLENGTH, WM_GETTEXT, (WM_WINDOWPOSCHANGING, WM_NCCALCSIZE, WM_WINDOWPOSCHANGED, WM_SIZE, WM_CHILDACTIVATE, WM_MOVE, WM_SHOWWINDOW
        private const int WM_ACTIVATE = 0x006;
        protected override void WndProc(ref Message message)
        {
            if (message.Msg == WM_ACTIVATE)
            {
                System.Diagnostics.Trace.WriteLine("MyView.WndProc(): message: " + message);
            }
            base.WndProc(ref message);
        }

        // called before OnEnter
        protected override void OnLoad(System.EventArgs eventArgs)
        {
            base.OnLoad(eventArgs);

            Form form1 = this.FindForm();      // null
            Form parentForm = this.ParentForm; // null
        }

        // called only on first display, not when the user switches between the 2 controls
        protected override void OnEnter(EventArgs eventArgs)
        {
            base.OnEnter(eventArgs);
        }

        protected override void OnLeave(EventArgs eventArgs)
        {
            base.OnLeave(eventArgs);
        }

        // Never called
        protected override void OnGotFocus(EventArgs eventArgs)
        {
            base.OnGotFocus(eventArgs);
        }

        // Never called
        protected override void OnLostFocus(EventArgs eventArgs)
        {
            base.OnGotFocus(eventArgs);
        }
 }

2 个答案:

答案 0 :(得分:1)

这是用户衍生控件的完整源代码。它按原样工作。

在表单上,​​我创建了控件的两个实例,然后可以单击它们。正如我所做的那样,正方形从红色切换为蓝色。

您应该能够在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();
        }
    }
}

要完成答案,这里是form1.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;
    }
}

答案 1 :(得分:1)

要求:“我希望能够检测到操作员...何时单击了一个,然后单击了另一个。”

很明显,您希望在单击该控件或其任何子控件时得到通知。为此,您必须预订所有子控件的Click事件。

protected override void OnControlAdded(ControlEventArgs e)
{
     // subscribe to click event of e.Control
     e.Control.Click += OnChildClicked;
}
// TODO: de-subscribe in OnControlRemoved

public void OnChildClicked(object Sender, EventArgs e)
{
    // Clicked on child control, act as if Clicked on me
    this.OnClick(e);
}

任何类似点击的事件都将调用click事件,即使它是通过键盘完成的,例如通过按Enter键。

您写了您想要的鼠标单击。如果您确实只希望单击鼠标,请使用MouseClick事件。如果操作员使用TAB来选择您的一个子控件,然后按Enter键单击它,则不会收到通知。