C# winform change selected tabcontrol image

Total Post:108


 3567  View(s)
Rate this:
Hi Expert!

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;

Thanks in advance! 
  1. Re: C# winform change selected tabcontrol image

    Hi John!

    You can try as following

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

    I hope it helpful to you!


Please check, If you want to make this post sponsored

You are not a Sponsored Member. Click Here to Subscribe the Membership.