0

I have searched for a while to find a fix for this and hoped that using the 'SelectionChangeCommitted' event as per this answer ( How to prevent selectedindexchanged event when DataSource is bound? ) would work but unfortunately no such luck.

Basically I want to set my Listbox.DataSource to null, although it contains two objects at the time. It seems like when I hit the line matchupListBox.Datasource = null it jumps to the _SelectedIndexChanged event and enters my other method (LoadMatchup())

If anyone could shed some light on this or advise me on how to set it to null another way (again, SelectionChangeCommitted didn't work) I'd appreciate it. Full code for my class is below:

namespace TrackerUI
{
    public partial class TournamentViewerForm : Form
    {
        private TournamentModel tournament;
        List<int> rounds = new List<int>();
        List<MatchupModel> selectedMatchups = new List<MatchupModel>();

    public TournamentViewerForm(TournamentModel tournamentModel)
    {
        InitializeComponent();

        tournament = tournamentModel;

        LoadFormData();
        LoadRounds();
    }

    private void LoadFormData()
    {
        tournamentName.Text = tournament.TournamentName;
    }

    private void WireUpMatchupsList()
    {
        matchupListBox.DataSource = null;
        matchupListBox.DataSource = selectedMatchups;
        matchupListBox.DisplayMember = "DisplayName";
    }

    private void WireUpRoundsList()
    {
        roundDropDown.DataSource = null;
        roundDropDown.DataSource = rounds;
    }

    private void LoadRounds()
    {
        rounds = new List<int>();

        rounds.Add(1);
        int currRound = 1;

        foreach (List<MatchupModel> matchups in tournament.Rounds)
        {
            if (matchups.First().MatchupRound > currRound)
            {
                currRound = matchups.First().MatchupRound;
                rounds.Add(currRound);
            }
        }

        WireUpRoundsList();
    }

    private void roundDropDown_SelectedIndexChanged(object sender, EventArgs e)
    {
        LoadAllMatchups();
    }

    private void LoadAllMatchups()
    {
        int round = (int)roundDropDown.SelectedItem;

        foreach (List<MatchupModel> matchups in tournament.Rounds)
        {
            if (matchups.First().MatchupRound == round)
            {
                selectedMatchups = matchups;
            }
        }

        WireUpMatchupsList();
    }

    private void LoadMatchup()
    {
        MatchupModel m = (MatchupModel)matchupListBox.SelectedItem;

        for (int i = 0; i < m.Entries.Count; i++)
        {
            if (i == 0)
            {
                if (m.Entries[0].TeamCompeting != null)
                {
                    teamOneName.Text = m.Entries[0].TeamCompeting.TeamName;
                    teamOneScoreValue.Text = m.Entries[0].Score.ToString();

                    teamTwoName.Text = "<bye>";
                    teamTwoScoreValue.Text = "0";
                }
                else
                {
                    teamOneName.Text = "Not yet set.";
                    teamOneScoreValue.Text = "";
                }

            }

            if (i == 1)
            {
                if (m.Entries[0].TeamCompeting != null)
                {
                    teamTwoName.Text = m.Entries[1].TeamCompeting.TeamName;
                    teamTwoScoreValue.Text = m.Entries[1].Score.ToString();
                }
                else
                {
                    teamTwoName.Text = "Not yet set.";
                    teamTwoScoreValue.Text = "";
                }
            }
        }
    }

    private void matchupListBox_SelectedIndexChanged(object sender, EventArgs e)
    {
        LoadMatchup();
    }
}

}

Oh by the way, probably important info I almost forgot, is that it errors on the second last method on line

MatchupModel m = (MatchupModel)matchupListBox.SelectedItem;
for (int i = 0; i < m.Entries.Count; i++)

because m is now null, although SelectedItem is 2 (int for a MatchUp model) on the form.

Thanks in advance kind people.

1
  • 2
    A object row count is set to -1 when constructed before any column data is added and then when columns are added the number of rows is set to zero. So any events that look at rows should test for for count being greater than zero.
    – jdweng
    Apr 21, 2020 at 10:28

2 Answers 2

0

You can leave out the matchupListBox.DataSource = null;

0

Try out something like this

    {
        private TournamentModel tournament;
        BindingList<int> rounds = new BindingList<int>();
        BindingList<MatchupModel> selectedMatchups = new();


        public TournamentViewerForm(TournamentModel tournamentModel)
        {
            InitializeComponent();
            tournament = tournamentModel;

            WireUpLists();

            LoadFormData();
            LoadRounds();
        }

        private void WireUpLists()
        {
            roundDropDown.DataSource = rounds;
            matchupListBox.DataSource = selectedMatchups;
            matchupListBox.DisplayMember = "DisplayName";
        }
        private void LoadFormData()
        {
            TournamentName.Text = tournament.TournamentName;
        }
        private void LoadRounds()
        {
            rounds.Clear();

            rounds.Add(1);
            int currRound = 1;

            foreach (List<MatchupModel> matchups in tournament.Rounds)
            {
                if (matchups.First().MatchupRound > currRound)
                {
                    currRound = matchups.First().MatchupRound;
                    rounds.Add(currRound);
                }
            }
            LoadMatchups(1);
        }

        private void roundDropDown_SelectedIndexChanged(object sender, EventArgs e)
        {
            LoadMatchups((int)roundDropDown.SelectedItem);
        }
        private void LoadMatchups(int round)
        {
            foreach (List<MatchupModel> matchups in tournament.Rounds)
            {
                if (matchups.First().MatchupRound == round)
                {
                    matchupListBox.SelectedIndexChanged -= matchupListBox_SelectedIndexChanged;
                    selectedMatchups.Clear();  
                    matchupListBox.SelectedIndexChanged += matchupListBox_SelectedIndexChanged;

                    foreach (MatchupModel m in matchups)
                    {
                            selectedMatchups.Add(m);
                    }

                }
            }
            if (selectedMatchups.Count > 0)
            {
                LoadMatchup(selectedMatchups.First()); 
            }
        }

         private void LoadMatchup()
    {
        MatchupModel m = (MatchupModel)matchupListBox.SelectedItem;

        for (int i = 0; i < m.Entries.Count; i++)
        {
            if (i == 0)
            {
                if (m.Entries[0].TeamCompeting != null)
                {
                    teamOneName.Text = m.Entries[0].TeamCompeting.TeamName;
                    teamOneScoreValue.Text = m.Entries[0].Score.ToString();

                    teamTwoName.Text = "<bye>";
                    teamTwoScoreValue.Text = "0";
                }
                else
                {
                    teamOneName.Text = "Not yet set.";
                    teamOneScoreValue.Text = "";
                }

            }

            if (i == 1)
            {
                if (m.Entries[0].TeamCompeting != null)
                {
                    teamTwoName.Text = m.Entries[1].TeamCompeting.TeamName;
                    teamTwoScoreValue.Text = m.Entries[1].Score.ToString();
                }
                else
                {
                    teamTwoName.Text = "Not yet set.";
                    teamTwoScoreValue.Text = "";
                }
            }
        }
    }
     private void matchupListBox_SelectedIndexChanged(object sender, EventArgs e)
        {
            LoadMatchup((MatchupModel)matchupListBox.SelectedItem);
        }
       

       
    }

I changed the selectedMatchups global variable type from List to that of BindingList

and then disabled the matchupListSelectedIndexChanged event just before i clear the selectedMatchups from the LoadMatchups function with selectedMatchups.Clear() and then re-eanabled after enable it just again.

This is so because this line of code selectedMatchups.Clear() triggers the matchListSelectedIndexChange event to run when not appropiate

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.