CheckedListBox.RemoveAt(lastItemIndex) 导致列表中的显示问题

发布于 2024-09-16 02:06:34 字数 669 浏览 6 评论 0原文

我有一个绑定到自定义类型的通用列表的选中列表框。当我删除列表中的其他项目时,显示正常,但是当我删除列表中的最后一个项目时,列表将显示类型名称而不是显示名称。

_selResolutions.RemoveAt(selIndex);
cklResolutions.DataSource = null;
cklResolutions.BeginUpdate();
cklResolutions.DataSource = _selResolutions;
cklResolutions.DisplayMember = "LongDesc";
cklResolutions.ValueMember = "LongDesc";
cklResolutions.EndUpdate();
for (var i = 0; i < _selResolutions.Count; i++)
{
    cklResolutions.SetItemChecked(i, _selResolutions[i].Selected);
}

当使用上面的代码删除最后一个项目时,显示看起来像这样。

[ ] Resolution
[ ] Resolution
[ ] Resolution
[ ] Resolution
[ ] Resolution

为什么会发生这种情况?我在这里错过了什么吗?我该如何纠正这个问题?

I have a checkedlistbox that's bound to a generic list of custom type. When I remove other items on the list, the display is okay, but when I remove the last item on the list, the list shows up w/ the type name instead of the displayname.

_selResolutions.RemoveAt(selIndex);
cklResolutions.DataSource = null;
cklResolutions.BeginUpdate();
cklResolutions.DataSource = _selResolutions;
cklResolutions.DisplayMember = "LongDesc";
cklResolutions.ValueMember = "LongDesc";
cklResolutions.EndUpdate();
for (var i = 0; i < _selResolutions.Count; i++)
{
    cklResolutions.SetItemChecked(i, _selResolutions[i].Selected);
}

the display looks like this when the last item is removed w/ the above code.

[ ] Resolution
[ ] Resolution
[ ] Resolution
[ ] Resolution
[ ] Resolution

why is this happening? am i missing something here? how do i correct this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

方觉久 2024-09-23 02:06:34

尝试改用 BindingSource。这为数据源提供了一个可以通知 DGV 更改的视图:

using System;
using System.Collections.Generic;
using System.Windows.Forms;

class Form1 : Form
{
    [STAThread]
    static void Main()
    {
        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);
        Application.Run(new Form1());
    }

    List<MyItem> _selResolutions;
    CheckedListBox cklResolutions;

    public Form1()
    {
        Controls.Add(cklResolutions = new CheckedListBox { Dock = DockStyle.Fill });

        _selResolutions = new List<MyItem>();
        _selResolutions.Add(new MyItem { LongDesc = "One", Selected = true });
        _selResolutions.Add(new MyItem { LongDesc = "Two", Selected = false });
        _selResolutions.Add(new MyItem { LongDesc = "Three", Selected = false });
        _selResolutions.Add(new MyItem { LongDesc = "Four", Selected = true });

        cklResolutions.DataSource = new BindingSource(_selResolutions, null);
        cklResolutions.DisplayMember = cklResolutions.ValueMember = "LongDesc";

        UpdateCheckBoxes();

        cklResolutions.KeyUp += new KeyEventHandler(cklResolutions_KeyUp);
    }

    private void UpdateCheckBoxes()
    {
        for (int n = 0; n < _selResolutions.Count; n++)
            cklResolutions.SetItemChecked(n, _selResolutions[n].Selected);
    }

    void cklResolutions_KeyUp(object sender, KeyEventArgs e)
    {
        if (e.KeyCode == Keys.Delete)
        {
            int index = cklResolutions.SelectedIndex;
            if (index >= 0)
            {
                BindingSource bs = cklResolutions.DataSource as BindingSource;
                bs.RemoveAt(index);
                UpdateCheckBoxes();
            }
        }
    }
}

class MyItem
{
    public string LongDesc { get; set; }
    public bool Selected { get; set; }
}

Try using a BindingSource instead. This provides the DataSource a view that can inform the DGV of changes:

using System;
using System.Collections.Generic;
using System.Windows.Forms;

class Form1 : Form
{
    [STAThread]
    static void Main()
    {
        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);
        Application.Run(new Form1());
    }

    List<MyItem> _selResolutions;
    CheckedListBox cklResolutions;

    public Form1()
    {
        Controls.Add(cklResolutions = new CheckedListBox { Dock = DockStyle.Fill });

        _selResolutions = new List<MyItem>();
        _selResolutions.Add(new MyItem { LongDesc = "One", Selected = true });
        _selResolutions.Add(new MyItem { LongDesc = "Two", Selected = false });
        _selResolutions.Add(new MyItem { LongDesc = "Three", Selected = false });
        _selResolutions.Add(new MyItem { LongDesc = "Four", Selected = true });

        cklResolutions.DataSource = new BindingSource(_selResolutions, null);
        cklResolutions.DisplayMember = cklResolutions.ValueMember = "LongDesc";

        UpdateCheckBoxes();

        cklResolutions.KeyUp += new KeyEventHandler(cklResolutions_KeyUp);
    }

    private void UpdateCheckBoxes()
    {
        for (int n = 0; n < _selResolutions.Count; n++)
            cklResolutions.SetItemChecked(n, _selResolutions[n].Selected);
    }

    void cklResolutions_KeyUp(object sender, KeyEventArgs e)
    {
        if (e.KeyCode == Keys.Delete)
        {
            int index = cklResolutions.SelectedIndex;
            if (index >= 0)
            {
                BindingSource bs = cklResolutions.DataSource as BindingSource;
                bs.RemoveAt(index);
                UpdateCheckBoxes();
            }
        }
    }
}

class MyItem
{
    public string LongDesc { get; set; }
    public bool Selected { get; set; }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文