Determining which tab is open

I am trying to get tabpageone that was right-clicked, in other words tabpage, that opened contextmenustrip.

There is a toolstripmenuitemcalled Close, which I used to close the tab that I clicked on.

I used this code:

public partial class USBrowser : Form

    {
        private Point lastpoint;
    }


private void closeTabToolStripMenuItem_Click(object sender, EventArgs e)
{
    for (int i = 0; i < browserTabControl.TabCount; i++)
    {
        Rectangle rec = browserTabControl.GetTabRect(i);
        if (rec.Contains(this.PointToClient(lastpoint)))
           closeTab(i);//this function closes the tab at specific index                
    }
}

    protected override void OnMouseClick(MouseEventArgs e)
    {
        base.OnMouseClick(e);
        if (e.Button == MouseButtons.Right)
            lastpoint = Cursor.Position;

    }

I also added (when adding tabpage):

    browserTabControl.TabPages.Insert(browserTabControl.TabCount - 1,WebPage);
    browserTabControl.SelectTab(WebPage);
    browserTabControl.SelectedTab.MouseClick += SelectedTab_MouseClick;

    void SelectedTab_MouseClick(object sender, MouseEventArgs e)
    {
        if (e.Button == MouseButtons.Right)
            lastpoint = Cursor.Position;
    }

The problem is that the last point is always (0,0) !! What for? Any other suggested idea is welcome thanx in advance.

+4
source share
1 answer

. OnMouseClick(), . MouseClick , TabControl. , , , .

, , . , , , :

    private void closeToolStripMenuItem_Click(object sender, EventArgs e) {
        tabControl1.SelectedTab.Dispose();
    }

, , , . , , , . :

    private TabPage RightClickedTab;

    private void contextMenuStrip1_Opening(object sender, CancelEventArgs e) {
        RightClickedTab = tabControl1.SelectedTab;
        var pos = tabControl1.PointToClient(Cursor.Position);
        for (int tab = 0; tab < tabControl1.TabCount; ++tab) {
            if (tabControl1.GetTabRect(tab).Contains(pos)) {
                RightClickedTab = tabControl1.TabPages[tab];
                break;
            }
        }
    }

    private void closeToolStripMenuItem_Click(object sender, EventArgs e) {
        if (RightClickedTab != null) RightClickedTab.Dispose();
    }
+4

Source: https://habr.com/ru/post/1528120/


All Articles