tags:

views:

51

answers:

1

Hi all.. This issue is driving me crazy..

This code raises index out of range exception for the messagebox:

    private void MainTabs_Selected(object sender, TabControlEventArgs e)
    {
        if (MainTabs.SelectedTab.Name == "ActiveDirectoryTab")
        {
            ADServerSelect.Items[0].Selected = true;
            MessageBox.Show(ADServerSelect.Items[0].Text);
        }
    }

But if the messagebox is the result of an event it works just fine:

    private void MainTabs_Selected(object sender, TabControlEventArgs e)
    {
        if (MainTabs.SelectedTab.Name == "ActiveDirectoryTab")
        {
            ADServerSelect.Items[0].Selected = true;
        }
    }

    private void testButton_Click(object sender, EventArgs e)
    {
        MessageBox.Show(ADServerSelect.Items[0].Text); 
    }

Any help would be greatly appreciated..

+2  A: 

looks like items added to the listview AFTER MainTabs_Selected event. can you check it?

Arseny
Beat me to it :D
Matt Ellen
This line of code works fine:ADServerSelect.Items[0].Selected = true; //select listview first itemSo I must assume the listview items are added before this event, isn't?
Shlomi