重命名列表框中的项目

时间:2010-12-18 19:15:10

标签: c# listbox

我想重命名列表框中的所选项目。 我该怎么做? 感谢。

2 个答案:

答案 0 :(得分:4)

编辑:几年后重新审视;以下是依赖于您正在使用的UI框架的方法。这假设您想要更改所选文本。

ASP.Net WebForms

protected void ChangeListBoxSelectedItemText(string textToChangeTo)
{
    lstBoxExample.SelectedItem.Text = textToChangeTo;
}

WPF - 假设ListBox包含Label对象

// To achieve this in WPF you have to cast the object
// This is because a ListBox can contain numerous types of UI objects
var selectedLabel = (Label)lstBoxExample.SelectedItem;
selectedLabel.Content = "Text to change to";

<强>的WinForms

// There may very well be a better way to do this
lstBoxExample.Items[lstBoxExample.SelectedIndex] = "New Item";

答案 1 :(得分:2)

ListBox包含对象。究竟是什么意思“重命名”一个项目?

如果您想要更改列表中显示的文本,您需要做的是更改对象,以便其ToString方法将返回所需的文本。

最常见的是,您可能在ListBox中存储字符串,在这种情况下,为了“重命名”某个项目,您必须删除旧项目并在同一索引中插入新文本。 / p>

相关问题