温馨提示:本文翻译自stackoverflow.com,查看原文请点击:c# - Listbox UI Stuck because of SelectedIndexChanged event
c#

c# - 列表框UI因SelectedIndexChanged事件而卡住

发布于 2020-03-31 23:43:34

发生的事情是,如果我在listBox1_SelectedIndexChanged方法中包含代码,则每次按下添加现金,保存或刷新按钮时UI都会卡住。

如果我删除listBox1_SelectedIndexChanged中的代码,它会很好地工作。

        private void LoadAccounts()
        {
            accounts.Clear();
            accounts = DBAccess.LoadAccounts();
            UpdateListBox();
        }

        private void UpdateListBox()
        {
            listBox1.DataSource = null;
            listBox1.DataSource = accounts;
            listBox1.DisplayMember = "Username";

        }

        private void btnAddCash_Click(object sender, EventArgs e)
        {
            string userName = listBox1.GetItemText(listBox1.SelectedItem);

            if ( Int32.TryParse(txtAddCash.Text, out int cash) )
            {
                DialogResult dr = MessageBox.Show($"Add {txtAddCash.Text} cash to {userName}?", "Add Cash", MessageBoxButtons.YesNo);
                if (dr == DialogResult.No)
                    return;

                AccountModel acc = new AccountModel();

                acc.Username = userName;
                acc.LeftCash = cash;

                DBAccess.AddCash(acc);

                txtAddCash.Text = "";

                LoadAccounts();
            }
        }

        private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            string selected = ((AccountModel)listBox1.SelectedItem).Username;
            int leftCash = accounts.Where(a => a.Username == selected).Select(a => a.LeftCash).First();

            lblLeftCash.Text = leftCash.ToString();
        }

我简述了发生了什么 在此处输入图片说明

就像更新的用户界面在旧列表框用户界面的后面

查看更多

提问者
marneee
被浏览
10
Oliver 2020-01-31 20:29

我已经尽力重建了您的代码。我使用列表作为数据库。我无法重现你的错误,但我有一个NullReferenceExceptionlistBox1_SelectedIndexChanged,如果我不写

if (listBox1.SelectedItem == null) return;

您可以尝试将此“如果”添加到您的SelectedIndexChanged活动中吗?

private void LoadAccounts()
{
    accounts.Clear();
    accounts = accountsOnDatabase.ToList();
    UpdateListBox();
}

private void btnAddCash_Click(object sender, EventArgs e)
{
    string userName = listBox1.GetItemText(listBox1.SelectedItem);

    if (int.TryParse(txtAddCash.Text, out int cash))
    {
        DialogResult dr = MessageBox.Show($"Add {txtAddCash.Text} cash to {userName}?", "Add Cash", MessageBoxButtons.YesNo);
        if (dr == DialogResult.No)
            return;

        AccountModel AccOnDataBase = accountsOnDatabase.Where(a => a.Username == userName).FirstOrDefault();
        if (AccOnDataBase == null) return;

        AccountModel acc = new AccountModel();
        acc.Username = userName;
        acc.LeftCash = cash + AccOnDataBase.LeftCash; //DBAccess.AddCash

        accountsOnDatabase.RemoveAll(a => a.Username == acc.Username);
        accountsOnDatabase.Add(acc);

        txtAddCash.Text = "";

        LoadAccounts();
    }
}
private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{
    if (listBox1.SelectedItem == null) return;

    string selected = ((AccountModel)listBox1.SelectedItem).Username;
    int leftCash = accounts.Where(a => a.Username == selected).Select(a => a.LeftCash).First();

    label1.Text = leftCash.ToString();
}

最新帖子

最新文章