且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

从组合框中删除选定的项目

更新时间:2023-01-13 19:46:40

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[^]