如何使ListBox刷新其项目文本?

时间:2020-03-05 18:53:01  来源:igfitidea点击:

我正在为尚未意识到诸如ListBox之类的控件不必包含字符串的人提供一个示例。他一直在存储格式化的字符串,并通过复杂的解析循环将数据从" ListBox"中取出来,我想向他展示一种更好的方法。

我注意到,如果我在ListBox中存储了一个对象,然后更新了一个会影响ToString的值,那么ListBox不会自行更新。我试过在控件上调用RefreshUpdate,但是都行不通。这是我正在使用的示例的代码,它要求我们将一个列表框和一个按钮拖到表单上:

Public Class Form1

    Protected Overrides Sub OnLoad(ByVal e As System.EventArgs)
        MyBase.OnLoad(e)

        For i As Integer = 1 To 3
            Dim tempInfo As New NumberInfo()
            tempInfo.Count = i
            tempInfo.Number = i * 100
            ListBox1.Items.Add(tempInfo)
        Next
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        For Each objItem As Object In ListBox1.Items
            Dim info As NumberInfo = DirectCast(objItem, NumberInfo)
            info.Count += 1
        Next
    End Sub
End Class

Public Class NumberInfo

    Public Count As Integer
    Public Number As Integer

    Public Overrides Function ToString() As String
        Return String.Format("{0}, {1}", Count, Number)
    End Function
End Class

我认为问题可能出在使用字段,并尝试实现INotifyPropertyChanged,但这没有效果。 (我使用字段的原因是因为这是一个示例,我不想添加几十行与我演示的主题无关的行。)

老实说,我以前从未尝试过像这样更新项目。过去,我一直在添加/删除项目,而不是对其进行编辑。所以我从来没有注意到我不知道如何进行这项工作。

那我想念什么呢?

解决方案

回答

在列表框的数据源和数据源属性之间使用数据源属性和BindingSource对象。然后刷新。

更新添加的示例。

像这样:

Public Class Form1

    Private datasource As New List(Of NumberInfo)
    Private bindingSource As New BindingSource

    Protected Overrides Sub OnLoad(ByVal e As System.EventArgs)
        MyBase.OnLoad(e)

        For i As Integer = 1 To 3
            Dim tempInfo As New NumberInfo()
            tempInfo.Count = i
            tempInfo.Number = i * 100
            datasource.Add(tempInfo)
        Next
        bindingSource.DataSource = datasource
        ListBox1.DataSource = bindingSource
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        For Each objItem As Object In datasource
            Dim info As NumberInfo = DirectCast(objItem, NumberInfo)
            info.Count += 1
        Next
        bindingSource.ResetBindings(False)
    End Sub
End Class

Public Class NumberInfo

    Public Count As Integer
    Public Number As Integer

    Public Overrides Function ToString() As String
        Return String.Format("{0}, {1}", Count, Number)
    End Function
End Class

回答

我对vb.net不太了解,但是在C中,我们应该使用数据源,然后通过调用listbox.bind()来绑定它。

回答

如果从ListBox派生,则可以调用RefreshItem受保护的方法。只需以我们自己的类型重新公开此方法。

public class ListBox2 : ListBox {
    public void RefreshItem2(int index) {
        RefreshItem(index);
    }
}

然后更改设计器文件以使用我们自己的类型(在这种情况下,为ListBox2)。

回答

当我需要更新列表框时,可以使用此类。

更新列表中的对象,然后调用其中一个包含的方法,具体取决于我们是否具有可用的索引。如果要更新列表中包含的对象,但是没有索引,则必须调用RefreshItems并更新所有项目。

public class RefreshingListBox : ListBox
{
    public new void RefreshItem(int index)
    {
        base.RefreshItem(index);
    }

    public new void RefreshItems()
    {
        base.RefreshItems();
    }
}

回答

这有点不专业,但是可以。
我刚刚删除并添加了该项目(也再次选择了它)。
该列表是根据"显示和更改"属性进行排序的,因此对我来说再合适不过了。副作用是引发了其他事件(索引已更改)。

if (objLstTypes.SelectedItem != null)
{
 PublisherTypeDescriptor objType = (PublisherTypeDescriptor)objLstTypes.SelectedItem;
 objLstTypes.Items.Remove(objType);
 objLstTypes.Items.Add(objType);
 objLstTypes.SelectedItem = objType;
}

回答

如果objLstTypes是ListBox名称
使用
objLstTypes.Items.Refresh();
希望这行得通...

回答

BindingList自己处理更新绑定。

using System;
using System.ComponentModel;
using System.Windows.Forms;

namespace TestBindingList
{
    public class Employee
    {
        public string Name { get; set; }
        public int Id { get; set; }
    }

    public partial class Form1 : Form
    {
        private BindingList<Employee> _employees;

        private ListBox lstEmployees;
        private TextBox txtId;
        private TextBox txtName;
        private Button btnRemove;

        public Form1()
        {
            InitializeComponent();

            FlowLayoutPanel layout = new FlowLayoutPanel();
            layout.Dock = DockStyle.Fill;
            Controls.Add(layout);

            lstEmployees = new ListBox();
            layout.Controls.Add(lstEmployees);

            txtId = new TextBox();
            layout.Controls.Add(txtId);

            txtName = new TextBox();
            layout.Controls.Add(txtName);

            btnRemove = new Button();
            btnRemove.Click += btnRemove_Click;
            btnRemove.Text = "Remove";
            layout.Controls.Add(btnRemove);

            Load+=new EventHandler(Form1_Load);
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            _employees = new BindingList<Employee>();
            for (int i = 0; i < 10; i++)
            {
                _employees.Add(new Employee() { Id = i, Name = "Employee " + i.ToString() }); 
            }

            lstEmployees.DisplayMember = "Name";
            lstEmployees.DataSource = _employees;

            txtId.DataBindings.Add("Text", _employees, "Id");
            txtName.DataBindings.Add("Text", _employees, "Name");
        }

        private void btnRemove_Click(object sender, EventArgs e)
        {
            Employee selectedEmployee = (Employee)lstEmployees.SelectedItem;
            if (selectedEmployee != null)
            {
                _employees.Remove(selectedEmployee);
            }
        }
    }
}

回答

lstBox.Items[lstBox.SelectedIndex] = lstBox.SelectedItem;