Kos*_*mos 3 .net c# listbox tostring
我有类TestClass已经ToString重写(它返回Name场).我有TestClass添加的实例,ListBox在某些时候我需要更改Name其中一个实例,然后我可以刷新它的文本ListBox?
using System;
using System.Windows.Forms;
namespace TestListBox
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
listBox1.Items.Add(new TestClass("asd"));
listBox1.Items.Add(new TestClass("dsa"));
listBox1.Items.Add(new TestClass("wqe"));
listBox1.Items.Add(new TestClass("ewq"));
}
private void button1_Click(object sender, EventArgs e)
{
((TestClass)listBox1.Items[0]).Name = "123";
listBox1.Refresh(); // doesn't help
listBox1.Update(); // same of course
}
}
public class TestClass
{
public string Name;
public TestClass(string name)
{
this.Name = name;
}
public override string ToString()
{
return this.Name;
}
}
}
Run Code Online (Sandbox Code Playgroud)
我遇到了同样的问题,并尝试了各种不同的方法来尝试让项目的显示文本真正反映底层项目的值。在检查了所有可用的属性后,我发现这是最简单的。
lbGroupList.DrawMode = DrawMode.OwnerDrawFixed;
lbGroupList.DrawMode = DrawMode.Normal;
它触发控件内的适当事件来更新显示的文本。
| 归档时间: |
|
| 查看次数: |
4815 次 |
| 最近记录: |