从组合框中删除选定的项目 [英] removing the selected item from combobox

查看:66
本文介绍了从组合框中删除选定的项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


我想从组合框中删除选定的项目.组合框是
绑定到数据库.
我使用了这样的代码

Hi,
I want to remove the selected item from the combobox. The combobox is
bound to database.
I used code like this

foreach (int selectedindex in combobox2.SelectedIndex.ToString())
{
  combobox2.Items.RemoveAt(selectedindex);
}


但它将返回错误,您无法为商品集合设置属性


but it will return an error you can''t set the property for item collection

if anyone has an idea about this help me with example.

推荐答案

Um.
我从哪里开始?

为什么要在int上调用ToString以便将其用作foreach中的Collection?
您为什么仍要尝试使用foreach?

当你做
Um.
Where do I start?

Why are you calling ToString on an int in order to use it as the Collection in a foreach?
Why are you trying to use a foreach anyway?

When you do
int x = 34;
foreach (int i in x.ToString())

循环内的"i"值将是整数x的字符串形式的每个字符的值:在这种情况下,循环将进行两次,其值i == 51并且i == 52(51是字符"3",而52是字符"4")

因此,坦白地说,循环这些并尝试删除它们是非常愚蠢的.如果可行,它将几乎从组合框中随机删除两个项目.

您想做什么,您认为这是一个很好的解决方案?

The value of "i" inside the loop will be the value of each individual character in the string form of the integer x: in this case the loop will go twice, with values i == 51 and i == 52 (51 is the character ''3'' and 52 is the character ''4'')

So looping on these and trying to delete them is frankly really, really, stupid. If it worked, it would delete two items almost randomly from the combobox.

What are you trying to do, that you think this is a good solution?


尊敬的Praveen,


如果尝试使用索引,它将无法正常工作.
从列表中删除项目时,索引将自动更改.


comboBox2.Items.Remove(comboBox2.SelectedItem);

此链接可能对您有帮助

http://msdn.microsoft.com/zh-CN/library/system.windows.forms.combobox.selectedindexchanged.aspx
http://www.mycsharpcorner.com/Post.aspx?postID=52 [ ^ ]
Dear Praveen,


if you try with indexes it won''t work correctly.
While removing items from list Indexes will change automatically.


comboBox2.Items.Remove(comboBox2.SelectedItem);

This links may help you

http://msdn.microsoft.com/en-us/library/system.windows.forms.combobox.selectedindexchanged.aspx
http://www.mycsharpcorner.com/Post.aspx?postID=52[^]


这篇关于从组合框中删除选定的项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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