如何删除列表框中的所有项目? [英] How to remove all ListBox items?
本文介绍了如何删除列表框中的所有项目?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我创建了两个单选按钮(体重,身高)。我会做两个类别之间切换。但它们共享相同的列表框控制器(listBox1中和listBox2)。
有没有清除所有列表框项目简单的好方法吗?我没有为列表框找到的removeAll()。我不喜欢我的复杂的多线条风格,我张贴在这里。
私人无效Weight_Click(对象发件人,RoutedEventArgs E)
{
//的单选按钮Weith和高度
//清除所有项目第一
listBox1.Items.Remove(脚)之间进行切换;
listBox1.Items.Remove(英寸);
listBox1.Items.Remove(米);
listBox2.Items.Remove(脚);
listBox2.Items.Remove(英寸);
listBox2.Items.Remove(米);
//添加源单位物品listBox1中
listBox1.Items.Add(公斤);
listBox1.Items.Add(咚咚);
//将目标单位物品listBox2
listBox2.Items.Add(公斤);
listBox2.Items.Add(咚咚);
}
私人无效Height_Click(对象发件人,RoutedEventArgs E)
{
//的单选按钮Weith和高度
/间切换/清除所有项目第一
listBox1.Items.Remove(公斤);
listBox1.Items.Remove(咚咚);
listBox2.Items.Remove(公斤);
listBox2.Items.Remove(咚咚);
//添加源单位物品listBox1中
listBox1.Items.Add(脚);
listBox1.Items.Add(英寸);
listBox1.Items.Add(米);
//将目标单位物品listBox2
listBox2.Items.Add(脚);
listBox2.Items.Add(英寸);
listBox2.Items.Add(米);
}
解决方案
是不一样的在WinForm和网络表单的方式
listBox1.Items.Clear()?;
I created two RadioButton (Weight and Height). I will do switch between the two categories. But the they share the same ListBox Controllers (listBox1 and listBox2).
Is there any good method to clear all the ListBox items simpler? I didn't find the removeAll() for ListBox. I don't like my complex multi-lines style which I posted here.
private void Weight_Click(object sender, RoutedEventArgs e)
{
// switch between the radioButton "Weith" and "Height"
// Clear all the items first
listBox1.Items.Remove("foot");
listBox1.Items.Remove("inch");
listBox1.Items.Remove("meter");
listBox2.Items.Remove("foot");
listBox2.Items.Remove("inch");
listBox2.Items.Remove("meter");
// Add source units items for listBox1
listBox1.Items.Add("kilogram");
listBox1.Items.Add("pound");
// Add target units items for listBox2
listBox2.Items.Add("kilogram");
listBox2.Items.Add("pound");
}
private void Height_Click(object sender, RoutedEventArgs e)
{
// switch between the radioButton "Weith" and "Height"
// Clear all the items first
listBox1.Items.Remove("kilogram");
listBox1.Items.Remove("pound");
listBox2.Items.Remove("kilogram");
listBox2.Items.Remove("pound");
// Add source units items for listBox1
listBox1.Items.Add("foot");
listBox1.Items.Add("inch");
listBox1.Items.Add("meter");
// Add target units items for listBox2
listBox2.Items.Add("foot");
listBox2.Items.Add("inch");
listBox2.Items.Add("meter");
}
解决方案
isn't the same as the Winform and Webform way?
listBox1.Items.Clear();
这篇关于如何删除列表框中的所有项目?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文