繁体   English   中英

C# 设置 ListBox.DataSource = null 时出错(可能是事件触发问题?)

[英]C# Error when setting ListBox.DataSource = null (Possible event firing issue?)

我已经搜索了一段时间以找到解决此问题的方法,并希望按照此答案使用“SelectionChangeCommitted”事件( How to prevent selectedindexchanged event when DataSource is bound? )会起作用,但不幸的是没有这样的运气。

基本上我想将我的 Listbox.DataSource 设置为 null,尽管它当时包含两个对象。 好像当我点击 matchupListBox.Datasource = null 时它跳转到 _SelectedIndexChanged 事件并进入我的其他方法(LoadMatchup())

如果有人可以阐明这一点或建议我如何以另一种方式将其设置为 null(同样,SelectionChangeCommitted 不起作用),我将不胜感激。 我的 class 的完整代码如下:

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();
    }
}

}

哦,顺便说一句,可能是我差点忘记的重要信息,是它在倒数第二个在线方法上出错

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

因为 m 现在是 null,尽管 SelectedItem 在表单上是 2(MatchUp 模型的 int)。

在此先感谢好心人。

您可以matchupListBox.DataSource = null;

尝试这样的事情

    {
        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);
        }
       

       
    }

我将selectedMatchups全局变量类型从List更改为BindingList

然后在我使用selectedMatchups.Clear()LoadMatchups function 中清除selectedMatchups之前禁用matchupListSelectedIndexChanged事件,然后在再次启用它之后重新启用。

之所以如此,是因为这行代码selectedMatchups.Clear()会触发matchListSelectedIndexChange事件在不合适的时候运行

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM