我正在使用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);
}
}
}
}
答案 0 :(得分:0)
这个代码段运行得很好。您需要确保已将选项卡控件添加到选项卡控件中,以便显示选项卡并供用户单击使用。
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);
}
}
}
答案 1 :(得分:0)
我发现使用取消选择和选择而不是创建TabControl的后代修复了问题。不知何故,当使用TabControl后代时,添加TabControlModified .SelectedIndex的Reactive UI绑定搞砸了事情并且不再调用被覆盖的OnClick。