更新列表框 [英] Update a list box

查看:72
本文介绍了更新列表框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Hello All,


我在列表框中有一个项目列表。(列表框中的项目是从文本框中添加的,例如(listBox1.Items。添加(textBox1.Text)),现在如果我必须改变列表框例如:如果在列表框中有一个名为Electricity的项目,现在用户决定通过在文本框中键入来替换Electriciy和一个名为Food的项目我该怎么办这个??谢谢大家....我正在研究VS 2008 Winform。

Hello All,


I have a list of items in the listbox.(The items to the list box were added from the textbox eg ( listBox1.Items.Add(textBox1.Text)), Now If I have to alter the Listbox eg: If there is an item called Electricity in the listbox and now the user decides to replace Electriciy with an Item called Food by typing in the textbox how do I do this?? Thank you all....I am working on VS 2008 Winform.

推荐答案

使用KeyValuePair类:

int key = someNo。

字符串值=" value"。

KeyValuePair< int,String> kvp = new KeyValuePair< string,string>(key,value);



为所有项目,创建上面的对象(记住键应该是唯一的),其中value将表示要显示的字符串,如Electicity等。


现在如果用户选择一个项目。然后获取所选项目并将值更改为新值或替换旧键值对迭代m与新的。
use KeyValuePair class:
int key=someNo.
String value="value".
KeyValuePair<int,String> kvp=new KeyValuePair<string,string>(key,value);


for all the item, create above object(remember key should be unique), where value will represent the string to display, like "Electicity" etc.

now if user select an item. then get the selected item and change the value to new value or replace the old key value pair item with new one.


键值对是一个很好的建议。但是,您也可以使用项目索引属性,该属性对于每个项目也是唯一的。可能有多个具有不同索引的电力项目。要编辑,您可以指定编辑过的测试,例如:

The key value pair is a good suggestion. However, you may also use the item index property which is also unique for each item. There may be number of ''electricity'' items with different indexes. To edit you may assign the edited test like:

展开 | 选择 | Wrap | 行号


我之前给出的解决方案有点复杂。我忘记了babai28建议的方式:)


以下列方式做任何事情:
my earlier given solution is bit complex. i forgot the way babai28 has suggested :)

any way do in following way:

int index = listBox1.SelectedIndex ;

listBox1.Items [index] =" new_val" ;;
int index = listBox1.SelectedIndex;
listBox1.Items[index] = "new_val";


这篇关于更新列表框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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