如何编写列表框的扩展方法 [英] How to write extention method for listbox
本文介绍了如何编写列表框的扩展方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
你好朋友,
在我的代码中,我必须从列表框中删除所选项目。
我已经编写了代码,我必须在需要时重写。
我可以扩展此代码,例如
我可以致电
Listbox.removeSelected();
删除列表框中的选定项目?
任何建议??
提前致谢
解决方案
试试这个:
public static class myExtensions
{
public static RemoveSelectedItems( this ListBox listBox1)
{
for ( int i = listBox1.SelectedIndices.Count-1; i > = 0 ; i--)
{
listBox1.Items.RemoveAt(listBox1.SelectedIndices [i]);
}
}
}
看看这里:在ASP.NET 3.0和3.5中使用扩展方法和泛型 [ ^ ]
我不建议为ListBox
编写扩展方法。此类不是密封,因此您只需使用所需方法创建派生类并使用它。
-SA
Hello friends,
In my code, i have to remove the selected item from the listbox.
I have writeen the code, which i have to rewrite whenever needed.
Can i extend this code, such as
I can call
Listbox.removeSelected();
to remove selected items in the listbox?
any suggestion??
Thanks in advance
解决方案
Try this:
public static class myExtensions { public static RemoveSelectedItems(this ListBox listBox1) { for (int i = listBox1.SelectedIndices.Count-1; i >= 0; i--) { listBox1.Items.RemoveAt(listBox1.SelectedIndices[i]); } } }
Have a look here: Using Extension methods in ASP.NET 3.0 and 3.5 with generics[^]
I would not advise to write an extension method forListBox
. This class is not sealed, so you can simply create a derived class with required method(s) and use it.
—SA
这篇关于如何编写列表框的扩展方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文