public void SetCheckValue(CheckBox cb)
{
if (cb.Checked)
{
MyItemlist obj1 = new MyItemlist();
obj1.Text = cb.Text;
obj1.Value = cb.Name.Remove(0, 2);
listBox1.Items.Add(obj1);
}
}
如果我没选中怎么把刚刚传的那个值清空啊?新手,大家帮帮忙.
{
if (cb.Checked)
{
MyItemlist obj1 = new MyItemlist();
obj1.Text = cb.Text;
obj1.Value = cb.Name.Remove(0, 2);
listBox1.Items.Add(obj1);
}
}
如果我没选中怎么把刚刚传的那个值清空啊?新手,大家帮帮忙.
在调用你写的这个方法的开始就调用初始化方法
其实我觉得应该写在事件里 比如说checkedchanged事件里
{
if (cb.Checked)
{
MyItemlist obj1 = new MyItemlist();
obj1.Text = cb.Text;
obj1.Value = cb.Name.Remove(0, 2);
listBox1.Items.Add(obj1);
}
else
{
//在这里移除你添加的值
}
}
{
MyItemlist obj1 = new MyItemlist();
obj1.Text = cb.Text;
obj1.Value = cb.Name.Remove(0, 2);
if (cb.Checked)
{
listBox1.Items.Add(obj1);
}
else
{
foreach (var item in listBox1.Items)
{
MyItemlist obj = item as MyItemlist;
if (obj.Text==obj1.Text&&obj.Value==obj1.Value)
{
listBox1.Items.Remove(obj);
break;
}
}
}
}
我实在想不出更好的移除方法。如果listBox中添加的是obj1的Text或者Value直接
listBox1.Items.Remove(obj.Text);或者listBox1.Items.Remove(obj.Value);就可以,可是你添加的是个对象,我只有这个方法了……