'C# tabcontrol file not found

I have a problem that I don't understand. I have a tabcontrol with 2 tabs in it.

Tab 1: tab with datagridview in it

tab 2: tab with checklistbox in it

When I'm in the tab 2, I run this code without a problem:

foreach (int i in groupeCheckedListBox.CheckedIndices)
{
    groupeCheckedListBox.SetItemCheckState(i, CheckState.Unchecked);
}

but if I'm in the tab 1, it doesn't find the checklistbox so I decided to write this code:

if (tabControl1.SelectedTab == tabControl1.TabPages[1])
{
    foreach (int i in groupeCheckedListBox.CheckedIndices)
    {
        groupeCheckedListBox.SetItemCheckState(i,CheckState.Unchecked);
    }
}

but when I run it, I receive an error:

"System.ComponentModel.Win32Exception : 'Le fichier spécifié est introuvable'

which mean that it cannot find the file at the line "if (tabControl1.SelectedTab == tabControl1.TabPages[1])".

I also tried to go with:

tabControl1.SelectedTab = tabControl1.TabPages[1];

and

TabPage t = tabControl1.TabPages[0];
tabControl1.SelectTab(t);

with the same result.

Can someone explain me why it doesn't find the file please?



Solution 1:[1]

Hmmm tab controls in Winforms can be tricky, are you putting this logic in the SelectedIndexChanged event on the TabControl? Then you should be able to check the index value and determine which tabPage you have active and the controls in each tab should always be accessible and instantiated, I was able to check the selectedTab to the tabPage expected and it evaluated true:enter image description here

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1