Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
382 views
in Technique[技术] by (71.8m points)

c# - Customizing a TabControl for the Closing of Individual Tabs

My scenario is the following:

I am working on a winforms application in C# that has a button inside the main page of a tabcontrol that will generate another tabpage each time that it is clicked. Each new tabpage will contain a layout defined by a user control.

My Questions are:

  1. How can I allow the user to then close one of the tabs that were created dynamically at runtime?

  2. How might I go about modifying the tabcontrol itself so that it has a small 'X' in each tab that the user may click on in order to close that particular tab? (Like Firefox has)

  3. How can I expose the SelectedIndex property of the tabcontrol to the user control if I want to close the tab with a button inside the user control instead?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I found this code and was very helpful to me:

private void tabControl_MouseUp(object sender, MouseEventArgs e)
{
    // check if the right mouse button was pressed
    if(e.Button == MouseButtons.Right)
    {
        // iterate through all the tab pages
        for(int i = 0; i < tabControl1.TabCount; i++)
        {
            // get their rectangle area and check if it contains the mouse cursor
            Rectangle r = tabControl1.GetTabRect(i);
            if (r.Contains(e.Location))
            {
                // show the context menu here
                System.Diagnostics.Debug.WriteLine("TabPressed: " + i);
             }
        }
    }
}

TabControl: How To Capture Mouse Right-Click On Tab


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...