大家好. 这很好用
Hello to everybody. This works fine
private void btnDelete_Click(object sender, EventArgs e) { for (int i = 0; i < listBox1.SelectedItems.Count; i++) { listBox1.Items.Remove(listBox1.SelectedItems[i].ToString()); i--; } }但这不起作用:
But this doesn''t work:
private void btnDelete_Click(object sender, EventArgs e) { listBox1.Items.Remove(listBox1.SelectedItems); }为什么第二个btnDelete_Click不起作用?我的意思是我用鼠标在listBox1上选择了一行,然后按按钮. .Remove功能不能识别我选择的哪一行?即使我说.Remove(listBox1.SelectedItem),是否必须要有一个selectedTable数组? "SelectedItems"一词不是不言自明的吗?而且由于我用鼠标单击了listBox1上的行,因此程序或IDE无法理解选择了哪一行?为什么我仍必须使用SelectedItems [i]?
Why is the second btnDelete_Click not working? I mean I select a line on my listBox1 with my mouse and then press the button. Doesn''t the .Remove function recognize which line I selected? Even though I say .Remove(listBox1.SelectedItem), is it a must to have and selectedItem array? Isn''t the word SelectedItems self-explanatory? And since I clicked the line on my listBox1 with my mouse, can''t the program or the IDE understand which line is selected? Why do I still have to use SelectedItems[i]?
推荐答案删除"功能一次只能删除一个对象,而"SelectedItems"功能只能删除一次返回所选项目的集合(因为您可能有多个所选项目).您还可以使用以下代码: The "Remove" function lets you only remove one object at a time, but the "SelectedItems" function returns a collection of the selected items (since you may have multiple selected items). You could also use this code: while(listBox1.SelectedItems.Count > 0) { listBox1.Items.Remove(listBox1.SelectedItem); }
第二个btnDelete_Click为什么不起作用: 因为Remove方法期望元素被删除,但是SelectedItems属性返回项目的集合. 我的意思是我用鼠标在listBox1上选择一行,然后按按钮. .Remove功能不能识别我选择的哪一行?即使我说.Remove(listBox1.SelectedItem),是否必须要有一个selectedTable数组? 这个词不是很明显吗? 集合的通用Remove功能与GUI状态(如选定项)有什么关系?如果您想使用删除所选内容"功能,则第一个解决方案将可以正常工作(除非您不需要ToString调用).自我解释是什么意思?对于编译器?您在这里不做魔术-它是程序设计. 由于我用鼠标单击了listBox1上的行,因此程序或IDE无法理解选择了哪一行? 这个问题给了我一个强烈的提示",你是一个初学者……运行时没有IDE,程序"了解选择了哪些行(您可以调用SelectedItems属性来检索它们,做到了,没有."你吗?) 为什么我仍然必须使用SelectedItems [i]? 因为Remove方法要删除该对象(如docu/intellisense所述).使用Index运算符,您可以从集合中获得一个元素-要删除的元素. 结论:我承认RemoveRange方法对于列表框而言将是一个不错的功能.但是您的其他问题来自缺乏对某些基础知识的理解,似乎您对框架没有太多经验-.NET的优点是:它非常统一,如果您将它用于Items-collection,则可以理解适用于所有集合(其中一些具有RemoveRange方法) 如果您仍然不喜欢它.实现您自己的Listbox(派生)或为ListBox.ObjectCollection创建扩展方法.最后,它始终是相同的,对我来说,编程是一件好事:如果您不喜欢它的方式,请根据自己的需要创建一段代码-对于这个问题,您可以明确地做到这一点那-但是我认为您最终会坚持第一个解决方案. Why is the second btnDelete_Click not working: Because the Remove method expects the element to remove but the SelectedItems property returns a collection of items. I mean I select a line on my listBox1 with my mouse and then press the button. Doesn''t the .Remove function recognize which line I selected? Even though I say .Remove(listBox1.SelectedItem), is it a must to have and selectedItem array? Isn''t the word SelectedItems self-explanatory? What has the generic Remove function of a collection to do with a GUI state like selected items? If you want a "Remove selected" function your first solution will work fine (except you don''t need the ToString call). What you mean by self-explainatory? for the compiler? You don''t do magic here - it''s programming. And since I clicked the line on my listBox1 with my mouse, can''t the program or the IDE understand which line is selected? This questions gives me a "strong hint" you are a beginner... There is no IDE at runtime, the "program" understands which lines are selected (you can call the SelectedItems property to retrieve them, you did that, didn''t you?) Why do I still have to use SelectedItems[i]? because the Remove method want''s the object to remove (like the docu/intellisense says). And with the Index operator you get one element out of a collection - the one you want to remove. Conclusion: I admit that a RemoveRange method would be a nice feature for a listbox. But your other questions come from a lack of understanding some basics and it seems you don''t have much experience with the framework - the good thing about .NET: it is very uniform, if you get it for the Items-collection you understand it for all collections (some of them have a RemoveRange method) If you still don''t like it. Implement your own Listbox (derive) or create an extension method for ListBox.ObjectCollection. In the end it''s always the same, and for me a good thing about programming: If you don''t like the way it is, create your own piece of code for your needs - and for this problem you can clearly do that - but I think you will stick to your first solution in the end.
更多推荐
listBox selectedItem问题
发布评论