【问题标题】:Overridden c# TabControl OnClick is not called未调用覆盖的c#TabControl OnClick
【发布时间】:2016-04-27 22:29:09
【问题描述】:

我正在使用 Microsoft Visual Studio 2013 并用 c# 编写。我编写了 TabControl 类的后代并重写了 OnClick 方法,然后更改了现有的 TabControl 元素以使用新类。一切都编译并运行,我在构造函数中的断点已到达,但它没有使用 OnClick 覆盖!这是 TabControl 后代代码,在此先感谢您的帮助!

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace LumaSense.Imaging.Calibration.UI
{
    public partial class TabControlModified : TabControl
    {
        public TabControlModified()
        {
            InitializeComponent();
        }

        private bool superuser;
        public int lastTabSelectedIndex = 0;

        public bool Superuser
        {
            get { return superuser; }
            set { superuser = value; }
        }

        public int LastTabSelectedIndex
        {
            get { return lastTabSelectedIndex; }
            set
            { this.lastTabSelectedIndex = value; }
        }

        protected override void OnClick(EventArgs e)
        {
            // SelectedIndex and tab have already changed before we get here
            if (this.superuser == false)
            {
                if (this.SelectedIndex <= this.lastTabSelectedIndex)
                {
                    this.LastTabSelectedIndex = this.SelectedIndex;
                    base.OnClick(e);
                }
                else
                {
                    base.OnClick(e);
                    this.SelectedIndex = lastTabSelectedIndex;
                }
            }
            else
            {
                this.LastTabSelectedIndex = this.SelectedIndex;
                base.OnClick(e);
            }
        }
    }
}

【问题讨论】:

  • 当您实例化此自定义选项卡控件时,您是否向控件添加任何页面?除非有任何页面,否则它不会显示单击时触发的选项卡而不是 OnClick 方法。

标签: c# visual-studio overriding controls


【解决方案1】:

这个 sn-p 工作得很好。您需要确保已将页面添加到您的标签控件,以便标签显示并可供用户单击。

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }

    private void Form1_Load(object sender, EventArgs e)
    {
        var mTab = new MyTab();
        mTab.Location = new System.Drawing.Point(100, 100);

        // The OnClick will only work on the 
        // Tabs themselves so Pages must be added to display the Tabs.
        var mtabPage1 = new System.Windows.Forms.TabPage();
        mTab.Controls.Add(mtabPage1);

        this.Controls.Add(mTab);
    }

    class MyTab : TabControl
    {
        protected override void OnClick(EventArgs e)
        {
            MessageBox.Show("I was clicked.");

            base.OnClick(e);
        }
    }
}

【讨论】:

  • 我发现使用取消选择和选择而不是创建 TabControl 的后代可以解决问题。不知何故,当使用 TabControl 后代时,添加 TabControlModified .SelectedIndex 的 Reactive UI 绑定搞砸了,被覆盖的 OnClick 不再是调用者。
【解决方案2】:

我发现使用取消选择和选择而不是创建 TabControl 的后代可以解决问题。不知何故,当使用 TabControl 后代时,添加 TabControlModified .SelectedIndex 的 Reactive UI 绑定搞砸了,被覆盖的 OnClick 不再是调用者。

【讨论】:

    猜你喜欢
    • 2016-02-29
    • 1970-01-01
    • 2021-03-30
    • 1970-01-01
    • 2011-09-04
    • 1970-01-01
    • 2011-08-26
    • 1970-01-01
    相关资源
    最近更新 更多