C# WINFORM CHANGE SELECTED TABCONTROL IMAGE

john rob

Total Post:108

Points:756
Posted by  john rob
C# 
C#
 2930  View(s)
Ratings:
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. AVADHESH PATEL

    Post:604

    Points:4228
    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;
                }
                else
                {
                    tabControl1.TabPages[i].ImageIndex = i;
                }
            }

    I hope it helpful to you!

Answer

NEWSLETTER

Enter your email address here always to be updated. We promise not to spam!