简体   繁体   中英

C# winform change selected tabcontrol image

I have a winform application written in C#. I had an imageList in my winform and I have a tabcontrol and each of the tab I assign a image as icon for the tab by changing ImageIndex.

However they have only 1 image for each tab and I want them to change to another image for the selected tab (like another highlighted image for active one). I have an idea to add all images to the imageList (both active and inactive images) and change the imageIndex of the selected tab. But I am not sure how to do it in practical.

Here are my current codes that I can come up with:

Inside SelectedIndexChange event, I have a function:

foreach (TabPage tab in tabControl1) 
{
    if (tab.index == tabControl1.SelectedIndex) { <---how to get the index?
        tab.imageIndex = tab.index + tabControl1.TabCount;
    } else {
        tab.imageIndex = tab.index;
    }
}

I came up with a solution

for (int i=0; i<tabControl1.TabPages.Count; i++)
        {
            if (tabControl1.TabPages[i] == tabControl1.SelectedTab)
            {
                tabControl1.TabPages[i].ImageIndex = i + tabControl1.TabPages.Count;
            }
            else
            {
                tabControl1.TabPages[i].ImageIndex = i;
            }
        }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM