How do I make a ListBox refresh its item text?

前端 未结 10 1966
后悔当初
后悔当初 2020-12-03 04:42

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

相关标签:
10条回答
  • 2020-12-03 04:46

    I don't know much about vb.net but in C# you should use datasource and then bind it by calling listbox.bind() would do the trick.

    0 讨论(0)
  • 2020-12-03 04:49

    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);
                }
            }
        }
    }
    
    0 讨论(0)
  • 2020-12-03 04:51

    If you use a draw method like:

    private void listBox1_DrawItem(object sender, DrawItemEventArgs e)
    {
        e.DrawBackground();
        e.DrawFocusRectangle();
    
        Sensor toBeDrawn = (listBox1.Items[e.Index] as Sensor);
        e.Graphics.FillRectangle(new SolidBrush(toBeDrawn.ItemColor), e.Bounds);
        e.Graphics.DrawString(toBeDrawn.sensorName, new Font(FontFamily.GenericSansSerif, 14, FontStyle.Bold), new SolidBrush(Color.White),e.Bounds);
    }
    

    Sensor is my class.

    So if I change the class Color somewhere, you can simply update it as:

    int temp = listBoxName.SelectedIndex;
    listBoxName.SelectedIndex = -1;
    listBoxName.SelectedIndex = temp;
    

    And the Color will update, just another solution :)

    0 讨论(0)
  • 2020-12-03 04:57

    I use this class when I need to have a list box that updates.

    Update the object in the list and then call either of the included methods, depending on if you have the index available or not. If you are updating an object that is contained in the list, but you don't have the index, you will have to call RefreshItems and update all of the items.

    public class RefreshingListBox : ListBox
    {
        public new void RefreshItem(int index)
        {
            base.RefreshItem(index);
        }
    
        public new void RefreshItems()
        {
            base.RefreshItems();
        }
    }
    
    0 讨论(0)
  • 2020-12-03 04:59

    Use the datasource property and a BindingSource object in between the datasource and the datasource property of the listbox. Then refresh that.

    update added example.

    Like so:

    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
    
    0 讨论(0)
  • 2020-12-03 05:02
    lstBox.Items[lstBox.SelectedIndex] = lstBox.SelectedItem;
    
    0 讨论(0)
提交回复
热议问题