ListBox

jiayingjie 2008-06-04 11:18:15
大家好!现在有个这样的问题,请指教!谢谢!
有3个ListBox和1个按钮,前2个显示的是2个表的字段,点击按钮在第3个ListBox中显示的就是在前2个表中选中的2个字段,还有个限制,前2个表中的字段在第3个ListBox中只能显示1次,也就是只能有1个对应关系,这些功能目前都实现了,存在的问题是填加完1个对应后,正常情况下再填加第2个对应的时候,不知道代码应该怎么写,目前代码如下:
int lbint = ListBox3.Items.Count;
string str1=ListBox1.SelectedValue.ToString();
string str2=ListBox2.SelectedItem.ToString();
if (lbint == 0)
{
ListItem item=new ListItem(str1+"—"+str2,str1+";"+str2);
ListBox3.Items.Add(item);
}
else
{
foreach(ListItem item in ListBox3.Items)
{
string[] field = item.Value.Split(';');
if (str1==field[0])
{
Response.Write("<script>alert('请重新匹配!')</script>");
}
else if(str2==field[1])
{
Response.Write("<script>alert('请重新匹配!')</script>");
}
}
}
...全文
58 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
jiayingjie 2008-06-04
  • 打赏
  • 举报
回复
非常感谢!
Zine_Alone 2008-06-04
  • 打赏
  • 举报
回复
逻辑改下,先校验再添加就没问题了撒,代码没测试过,大概意思如下.

int lbint = ListBox3.Items.Count;
string [] str=new string[]{ListBox1.SelectedValue.ToString(),ListBox2.SelectedItem.ToString()}
string str1=
string str2=
if (IsUnique(str))
{
ListItem item=new ListItem(str1+"—"+str2,str1+";"+str2);
ListBox3.Items.Add(item);
}
else
{

}

private bool IsUnique(string[] str)
{
bool bl=true;
foreach(ListItem item in ListBox3.Items)
{
string[] field = item.Value.Split(';');
if (str1==field[0])
{
bl=false;
break;
}
else if(str2==field[1])
{
bl=false;
break;
}
}
return bl;
}

111,125

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Creator Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧