Chris
Chris

Reputation: 3

Index change event C#

I was just wondering if there was anyway to execute the index change event for the first iteration.

My code looks like the following.

    private void cboxEvent_IndexChange(object sender, EventArgs e)
    {

        int value;

        value = cboxEvent.SelectedIndex;

        resetListBoxes(); 

        cboxEvent.SelectedIndex = value;

        csvExport(); 


    }

    private void cboxSLA_IndexChange(object sender, EventArgs e)
    {
        int value;

        value = cboxSLA.SelectedIndex;

        resetListBoxes(); 

        cboxNPA.SelectedIndex = value;

        csvExport(); 
    }
    private void cboxNPA_IndexChange(object sender, EventArgs e)
    {
        int value;

        value = cboxNPA.SelectedIndex;

        resetListBoxes(); 

        cboxNPA.SelectedIndex = value;

        csvExport(); 

    }

The problem is that once an index changes it resets the other listboxes and their Index change method is activated as well. Therefore it executes Their IndexChange method.

I would like for the code to be executed only once for the first Index Changed.

Any ideas?

Thanks in Advance

Chris

Upvotes: 0

Views: 1398

Answers (3)

Michael Yuwono
Michael Yuwono

Reputation: 2617

You can use .Focused property to manipulate
The idea is to set the focus off to disable the second iteration.

You would need a Label as a Focus Switcher

Declare one as a global variable;

Label x;

It would be like this

private void cboxEvent_IndexChange(object sender, EventArgs e)
{
    if(cboxEvent.Focused)
    {
        int value;
        value = cboxEvent.SelectedIndex;
        resetListBoxes(); 
        csvExport(); 
        x.Focus();
    }
    if(!cboxEvent.Focused)
        cboxEvent.SelectedIndex = value;
}

And goes the same to another 2 Combo Boxes..

Upvotes: 0

Andrey Korneyev
Andrey Korneyev

Reputation: 26846

You can rewrite your IndexChanged handlers in this manner (same for all handlers):

private bool _IgnoreIndexChange;

private void cboxEvent_IndexChange(object sender, EventArgs e)
{
    if (_IgnoreIndexChange)
        return;

    _IgnoreIndexChange = true;
    try
    {
        int value;
        value = cboxEvent.SelectedIndex;
        resetListBoxes(); 
        cboxEvent.SelectedIndex = value;
        csvExport(); 
    }
    finally
    {
        _IgnoreIndexChange = false;
    }    
}

So if any index will be changed by user - only IndexChanged handler of that combobox will run, no others.

Upvotes: 1

Sudhakar Tillapudi
Sudhakar Tillapudi

Reputation: 26209

keep a boolean variable to check wether the IndexChanged Event fired from user or from another event handler and proceed further only if the IndexChanged event fired from the user.

Upvotes: 1

Related Questions