如何确定右键单击哪个选项卡?

时间:2015-04-28 23:55:21

标签: c# tabs

我已经多次询问过这个问题,所有答案都是一样的。由于某种原因,它在我的程序中无法正常工作。

这是每个人都对这个问题给出的基本答案......

def create
  @staff = Staff.assign_attributes(staff_params)

  @staff.staff_types.each_with_index do |staff_type, index|
    if (type = Type.find_by(department: staff_type.type.department)).present?
      @staff.staff_type[index].assign_attribute(:id, type.id)
    end
  end

  if @staff.save
    flash[:success] = "Your account has been created successfully"
    redirect_to staff_path(@staff)
  else
    render 'new'
  end
end

这与我使用的代码相同,但是当我右键单击第二个选项卡时,它总是关闭第一个选项卡。

当我调试问题时,这就是我得到的......

for (int i = 0; i < tabs.TabCount; ++i) {
    if (tabs.GetTabRect(i).Contains(e.Location)) {
     //tabs.Controls[i]; // this is your tab
    }
}

enter image description here

正如您所看到的,即使我点击第二个标签,位置(2 + 56 = 58)也位于第一个标签中。

我做错了什么?这段代码重复了很多次,我发现很难相信它不起作用。看起来e.Location从与标签开始的位置不同的位置开始。

更新:当您右键单击选项卡以显示上下文菜单时,这是我正在运行的例程。

e.Location: x=57, y=7
rect(0):    x=2, y=2, width=56, height=18
rect(1):    x=58, y=2, width=99, height=18

2 个答案:

答案 0 :(得分:0)

您可以为TabControl设置所选事件,并从EvenArg中拉出所选选项卡。

private void tabControlStudent_Selected(object sender, TabControlEventArgs e) 
{
  if (e.TabPage == tabPageGuardianInfo)
  {
    loadGuardianList();
    selectGuardian();
  }
  else if (e.TabPage == tabPageTransactionInfo)
  {
    loadTransactions();
    loadPaymentAccount();
  }
}

答案 1 :(得分:0)

正如Rajeev在评论中指出的那样,我们无法看到您的点击事件处理程序是如何附加的,因此可能是问题所在。做以下工作对我有用。

public MainWindow()
{
    InitializeComponent();
    this.tabControl1.MouseClick += tabControl1_MouseClick;
}

void tabControl1_MouseClick(object sender, MouseEventArgs e)
{
   if (e.Button == MouseButtons.Right)
   {
       for (int i = 0; i < tabControl1.TabCount; i++)
       {
           if (tabControl1.GetTabRect(i).Contains(e.Location))
           {
               Console.WriteLine("Right Clicked on tab {0}", i);
           }
       }             
   }
}

更新:查看点击位置和矩形,我和你一样,虽然我只在位置为58时才更改标签,即使在56处我应理论上位于标签1之上。这是因为它们重叠,活动标签将位于另一个上方,每侧2个。 enter image description here

上述两个实例的位置:

Right Clicked on tab 0
{X=57,Y=7}
{X=2,Y=2,Width=56,Height=18}
{X=58,Y=2,Width=99,Height=18}

Right Clicked on tab 1
{X=58,Y=7}
{X=2,Y=2,Width=56,Height=18}
{X=58,Y=2,Width=99,Height=18}