如何使一个ListBox刷新其项目的文字? [英] How do I make a ListBox refresh its item text?

查看:340
本文介绍了如何使一个ListBox刷新其项目的文字?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在做一个例子的人谁还没有意识到,像列表框控件没有包含字符串;他一直存储格式的字符串,并通过复杂的分​​析铁圈跳获取数据备份出来的列表框,我想给他有一个更好的办法。

I'm making an example for someone who hasn't yet realized that controls like ListBox don't have to contain strings; he had been storing formatted strings and jumping through complicated parsing hoops to get the data back out of the ListBox and I'd like to show him there's a better way.

我注意到,如果我有存储在的ListBox对象然后更新一个值,影响的ToString 中, 列表框不更新。我已经打过电话刷新更新上的控制,但是没有作品。下面是我用这个例子的code,它需要你拖动列表框和一个按钮到表单:

I noticed that if I have an object stored in the ListBox then update a value that affects ToString, the ListBox does not update itself. I've tried calling Refresh and Update on the control, but neither works. Here's the code of the example I'm using, it requires you to drag a listbox and a button onto the form:

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的的,但是这并没有影响。 (我使用领域的原因是因为它是一个例子,我不觉得像加什么都没有做的题目,我展现着几十行。)

I thought that perhaps the problem was using fields and tried implementing INotifyPropertyChanged, but this had no effect. (The reason I'm using fields is because it's an example and I don't feel like adding a few dozen lines that have nothing to do with the topic I'm demonstrating.)

老实说,我从来没有尝试过的地方像这样的更新项目;过去我一直添加/删除项目,而不是编辑它们。所以,我从来没有注意到,我不知道如何使这项工作。

Honestly I've never tried updating items in place like this before; in the past I've always been adding/removing items, not editing them. So I've never noticed that I don't know how to make this work.

所以我缺少什么?

推荐答案

的BindingList处理由自身更新绑定。

BindingList handles updating the bindings by itself.

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

这篇关于如何使一个ListBox刷新其项目的文字?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆