foreach (string strs in list)
{
if (buttonBar1.SelectedGroup.Items.Count > 0)
{
for (int i = 0; i < buttonBar1.SelectedGroup.Items.Count; i++)
{
if (buttonBar1.SelectedGroup.Items[i].Text == strs)
{
}
else
{
buttonBar1.SelectedGroup.Items.Add(strs);
}
}
}
else
{
buttonBar1.SelectedGroup.Items.Add(strs);
}
}
list是个集合,buttonBar1是个空间,因为方法体会被多次触发,所以需要判断如果控件里面有这个值的,不加进入,没有的话再加。可是怎么弄都是乱得很
{
if (buttonBar1.SelectedGroup.Items.Count > 0)
{
for (int i = 0; i < buttonBar1.SelectedGroup.Items.Count; i++)
{
if (buttonBar1.SelectedGroup.Items[i].Text == strs)
{
}
else
{
buttonBar1.SelectedGroup.Items.Add(strs);
}
}
}
else
{
buttonBar1.SelectedGroup.Items.Add(strs);
}
}
list是个集合,buttonBar1是个空间,因为方法体会被多次触发,所以需要判断如果控件里面有这个值的,不加进入,没有的话再加。可是怎么弄都是乱得很
{
if (buttonBar1.SelectedGroup.Items.Count > 0)
{
bool flag=false;
for (int i = 0; i < buttonBar1.SelectedGroup.Items.Count; i++)
{
if (buttonBar1.SelectedGroup.Items[i].Text == strs)
{
flag=true;
break;
}
}
if(!flag)
buttonBar1.SelectedGroup.Items.Add(strs);
}
else
{
buttonBar1.SelectedGroup.Items.Add(strs);
}
}
if(buttonBar1.SelectedGroup.Items.Count > 0)
{
for (int i = 0; i < buttonBar1.SelectedGroup.Items.Count; i++)
{
if (buttonBar1.SelectedGroup.Items[i].Text != strs)
buttonBar1.SelectedGroup.Items.Add(strs);
}
}
else
{
buttonBar1.SelectedGroup.Items.Add(strs);
}
Sorry,漏了一句话
if(buttonBar1.SelectedGroup.Items.Count > 0)
{
foreach (string strs in list) //补上
{ for (int i = 0; i < buttonBar1.SelectedGroup.Items.Count; i++)
{
if (buttonBar1.SelectedGroup.Items[i].Text != strs)
buttonBar1.SelectedGroup.Items.Add(strs);
}
}
}
else
{
buttonBar1.SelectedGroup.Items.Add(strs);
}
if(buttonBar1.SelectedGroup.Items.Count > 0)
{
foreach (string strs in list) //补上
{ for (int i = 0; i < buttonBar1.SelectedGroup.Items.Count; i++)
{
if (buttonBar1.SelectedGroup.Items[i].Text != strs)
buttonBar1.SelectedGroup.Items.Add(strs);
}
}
}
else
{
foreach (string strs in list) //补上
{
buttonBar1.SelectedGroup.Items.Add(strs);
}
}
foreach (string strs in list)
{
if (buttonBar1.SelectedGroup.Items.Count > 0)
{
bool flag=false;
for (int i = 0; i < buttonBar1.SelectedGroup.Items.Count; i++)
{
if (buttonBar1.SelectedGroup.Items[i].Text == strs)
{
flag=true;
break;
}
}
if(!flag)
buttonBar1.SelectedGroup.Items.Add(strs);
}
else
{
buttonBar1.SelectedGroup.Items.Add(strs);
}
}
using System.Collections.Generic; List<string> list = new List<string>();
for (int i = 0; i < buttonBar1.SelectedGroup.Items.Count; i++)
{
list.Add(buttonBar1.SelectedGroup.Items[i].Text);
}
foreach (string strs in list)
{
if (Array.IndexOf<string>(list, strs) == -1)
{
buttonBar1.SelectedGroup.Items.Add(strs);
list.Add(strs);
}
}
{
bool check=false;
if (buttonBar1.SelectedGroup.Items.Count > 0)
{
for (int i = 0; i < buttonBar1.SelectedGroup.Items.Count; i++)
{
if (buttonBar1.SelectedGroup.Items[i].Text == strs)
{
check=true;
break;
}
}
if(!check)
buttonBar1.SelectedGroup.Items.Add(strs);
}
else
{
buttonBar1.SelectedGroup.Items.Add(strs);
} }
你的这个list跟我上面定义的,应该是两个了对吧,也就是说这个方法需要两个list<string>对吗
List<string> strList = new List<string>();
for (int i = 0; i < buttonBar1.SelectedGroup.Items.Count; i++)
{
strList.Add(buttonBar1.SelectedGroup.Items[i].Text);
}
foreach (string strs in list)
{
if (Array.IndexOf<string>(strList, strs) == -1)
{
buttonBar1.SelectedGroup.Items.Add(strs);
strList.Add(strs);
}
}
foreach (string strs in list)
{
if (Array.IndexOf<string>(list, strs) == -1)
{
buttonBar1.SelectedGroup.Items.Add(strs);
list.Add(strs);
}
}
里面的list是哪个跟那个啊?
List<string> strList = new List<string>();
for (int i = 0; i < buttonBar1.SelectedGroup.Items.Count; i++)
{
strList.Add(buttonBar1.SelectedGroup.Items[i].Text);
}
foreach (string strs in list)
{
if (Array.IndexOf<string>(strList.ToArray(), strs) == -1)
{
buttonBar1.SelectedGroup.Items.Add(strs);
strList.Add(strs);
}
}
List<string> strList = new List<string>();
for (int i = 0; i < buttonBar1.SelectedGroup.Items.Count; i++)
{
strList.Add(buttonBar1.SelectedGroup.Items[i].Text);
}
foreach (string strs in list)
{
if (!strList.Contains(strs))
{
buttonBar1.SelectedGroup.Items.Add(strs);
strList.Add(strs);
}
}