Worm
Worm

Reputation: 19

Cycling Backwards through Tabs in Tab Control with Button Click

Below is the code I'm using to cycle through the tabs going one direction:

       private void button2_Click(object sender, EventArgs e)
        { 
            projInfo.SelectedIndex = (projInfo.SelectedIndex + 1) % projInfo.TabCount;
        }

Question:

How can I change this code to have it cycle backwards (or right to left) instead of left to right? I tried changing the +1 to -1 but to no luck.

Upvotes: 0

Views: 59

Answers (1)

bati06
bati06

Reputation: 307

Changing +1 to -1 is a right direction. You just have to handle one special case when the current selected index is 0 and the next one have to be tabCount-1.

private void button2_Click(object sender, EventArgs e)
{
    var newIndex = projInfo.SelectedIndex - 1;
    if (newIndex < 0) newIndex = projInfo.TabCount - 1;
    projInfo.SelectedIndex = newIndex;
}

Upvotes: 1

Related Questions