在我的程序中,我用了一个RadioButtonList和一个DropDownList往Listbox中添加ITEM,然后根据这些ITEM做查询,然后还可以更改和删除ITEM,现在添加查询没问题,但是就是删除某个ITEM会出问题.具体问题是,我用上面2个控件添加ITEM的时候,要是同时出现VALUE一样的ITEM的话,我要是想删除后面的ITEM的话会发现删除的都是第一个同样VALUE的ITEM,举个例子:我现在添加了4个ITEM:A(VALUE),0(这个0是序号是第一个)、B,1、A,2、C,3现在我选择了序号2的A点删除结果确删除了序号0的A,结果变成了B,0、A,1、C,2。
我把某段添加代码还有删除代码贴出来,大家帮我分析下是什么原因。
RadioButtonList的添加代码如下,news_connection是RadioButtonList,sear_body是Listbox。 private void Button6_Click(object sender, System.EventArgs e)
{
string tmp_text="";
string tmp_value="";
tmp_text=this.news_connection.SelectedItem.Text.ToString()+" ";
tmp_value=this.news_connection.SelectedItem.Value.ToString()+" ";
this.sear_body.Items.Add(tmp_text);
this.sear_body.Items[this.sear_body.Items.Count-1].Value=tmp_value;
} 删除代码如下:
private void Button2_Click(object sender, System.EventArgs e)
{
if(this.sear_body.SelectedIndex!=-1)
{
for(inti=0;i<this.sear_body.Items.Count;i++)
{
if(this.sear_body.Items[i].Selected)
{
this.sear_body.Items.RemoveAt(i);
i--; }
} }
else
{
this.sear_body.Items.Clear();
}
}
我把某段添加代码还有删除代码贴出来,大家帮我分析下是什么原因。
RadioButtonList的添加代码如下,news_connection是RadioButtonList,sear_body是Listbox。 private void Button6_Click(object sender, System.EventArgs e)
{
string tmp_text="";
string tmp_value="";
tmp_text=this.news_connection.SelectedItem.Text.ToString()+" ";
tmp_value=this.news_connection.SelectedItem.Value.ToString()+" ";
this.sear_body.Items.Add(tmp_text);
this.sear_body.Items[this.sear_body.Items.Count-1].Value=tmp_value;
} 删除代码如下:
private void Button2_Click(object sender, System.EventArgs e)
{
if(this.sear_body.SelectedIndex!=-1)
{
for(inti=0;i<this.sear_body.Items.Count;i++)
{
if(this.sear_body.Items[i].Selected)
{
this.sear_body.Items.RemoveAt(i);
i--; }
} }
else
{
this.sear_body.Items.Clear();
}
}
private void Button2_Click(object sender, System.EventArgs e)
{
if(this.sear_body.SelectedIndex!=-1)
{
if(this.sear_body.Items[i].Selected)
{
this.sear_body.Items.RemoveAt(3);
}
} }
else
{
this.sear_body.Items.Clear();
}
}
我修改后做了测试,删除完全正常,并不会因为前后2个ITEM的VALUE一样而错删除前面的ITEM,然后我又把原来程序段的循环语句用其他来写,比如WHILE什么的,甚至我从序号0开始用IF来判断
然后IF ELSE继续判断1,2,3,4,这样都会出现一开始的错删除同样VALUE的ITEM的情况.我就感觉似乎是listbox控件自己对循环的响应机制的问题,有哪个高手知道吗?能详细说下listbox控件吗?
to LZ:对于html下的标记,select有value和selectedIndex, 假如两个option的value相同,select的value只返回这个value,而selectedindex就只返回第一个等于此value的index,所以,在删除指定的value的时候,就删除第一个等于这个value的option。
而你后面的试验,是你删除指定的index,这个select是不会搞错的,所以,你指定删除哪个,它就会删除哪个的。
<select>
<option value="1">1</option>
.....
....
<option value="n">n</option>
</select>