【问题标题】:How do I find out what tab I'm right clicking on in winforms tabcontrol?如何找出我在winforms tabcontrol中右键单击的选项卡?
【发布时间】:2016-01-26 08:45:21
【问题描述】:

我正在右键单击 winforms tabcontrol 的选项卡条中的选定或未选定选项卡时出现上下文菜单条。它将关闭,并暂时关闭除此之外的所有内容。无论如何,我需要能够在按下右键时捕获鼠标在哪个选项卡上。有人知道怎么做吗?

我接受的另一种解决方案是在显示上下文菜单之前通过右键单击选择未选择的选项卡。

【问题讨论】:

    标签: c# tabcontrol


    【解决方案1】:

    如果tabs是你的tabcontrol,你可以在你的鼠标点击事件中添加这个代码来找到它

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

    【讨论】:

    • 这似乎是我想要的,当我得到它的工作时我会赞成/接受!
    • @IsaacB: np, gl 和其余的 :)
    【解决方案2】:

    这可能会有所帮助,它会捕获您用鼠标右键单击的位置,如果它位于任何选项卡的矩形上,则会选择该选项卡并显示右侧菜单

    private void tabControl1_MouseClick(object sender, MouseEventArgs e)
    {
          if (e.Button == MouseButtons.Right)
        {
                for (int i = 0; i < tabs.TabCount; ++i)
    
                    {
    
                    if (tabs.GetTabRect(i).Contains(e.Location)) 
                         {
    
                    tabControl1.SelectTab(i);
    
                    this.contextMenuStrip1.Show(this.tabControl1, e.Location);
    
                         }
    
                    }
        }
    }
    

    玩得开心:)

    【讨论】:

      【解决方案3】:

      事件处理程序的sender 参数通常为您提供您单击的对象。

      void whatever_OnClick(object sender, EventArgs e) {
        var tab = sender as TabControlClassHere;
      }
      

      【讨论】:

      • 它告诉我我点击的标签控件,这没有帮助。只有一个选项卡控件!无论如何感谢您的建议!
      猜你喜欢
      • 1970-01-01
      • 2021-10-10
      • 1970-01-01
      • 2015-06-08
      • 2010-12-09
      • 1970-01-01
      • 2011-11-21
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多