2015-06-17 77 views
0

如何检查是否在ListBox中存在在C#中的数组如何检查是否在C#ListBox中存在一个数组

我有两个列表框从数据库填充,我可以从一个列表框移动数组项到另一个列表框在asp.net使用C#

有没有办法检查一个数组是否存在或有一个值?

我有这行代码,但是当我没有从第一ListBox中输出填充第二个列表框值是:

System.Collections.ArrayList 
System.Collections.ArrayList 


    ArrayList arraylist2 = new ArrayList(); 
    ArrayList arraylist4 = new ArrayList(); 

    protected void Button1_Click(object sender, EventArgs e) 
    { 
     if (arraylist2.ToString() != "" && arraylist4.ToString() != "") 
     { 
      Response.Write(arraylist2.ToString() + "<br />" + arraylist4.ToString() + "<br />"); 
     } 
     else 
     { 
      Response.Write("not array values"); 
     } 
    } 

你能帮助我吗?

预先感谢您。

编辑

我以下代码:

protected void Button1_Click(object sender, EventArgs e) 
{ 
    if (arraylist2.Count > 0 && arraylist4.Count > 0) 
    { 
     Response.Write(arraylist2.ToString() + "<br />" + arraylist4.ToString() + "<br />"); 
    } 
    else 
    { 
     Response.Write("not array values"); 
    } 
} 

编辑#1

if (arraylist2.Count > 0 && arraylist4.Count > 0) 
{ 
    int b = arraylist2.Count; 
    for (int i = 0; i < b; i++) 
    { 
     Response.Write(arraylist2[i] + ", " + arraylist4[i]); 
    } 

} 
else 
{ 
    Response.Write("not array values"); 
} 

编辑#2

arraylist2.Add(ListBox2.ToString()); 
arraylist4.Add(ListBox4.ToString()); 

b = arraylist2.Count; 
c = arraylist4.Count; 

if (b > 1 && c > 1) 
{    
    for (i = 0; i < b; i++) 
    { 
     Response.Write(arraylist2[i].ToString() + ", " + arraylist4[i].ToString()); 
    } 
} 
else 
{ 
    Response.Write("not array values"); 
} 

编辑#3

arraylist2.Add(ListBox2.Text.ToString()); 
arraylist4.Add(ListBox4.Text.ToString()); 

b = arraylist2.Count; 
c = arraylist4.Count; 

if (b > 0 && c > 0) 
{   
    for (i = 0; i < b; i++) 
    { 
     Response.Write(arraylist2[i].ToString() + ", " + arraylist4[i].ToString()); 
    } 
} 
+0

看你的编辑#2你的错号码检查。如果在两个数组列表中都有1个项目,那么您应该检查b> 0和c> 0。 – sr28

+0

此外,更重要的是,您需要迭代列表框中的项目以将它们添加到数组列表中。你不能仅仅像这样将列表框添加到数组列表中。 – sr28

+0

@ sr28:请参阅**编辑#3 **第一个问题...输出为空... –

回答

0

确保两个名单arraylist2arraylist4在它的东西。由于您在if中使用&&,所以两个条件都必须返回true

下面的代码为我工作 -

ArrayList arraylist2 = new ArrayList(); 
    ArrayList arraylist4 = new ArrayList(); 

    private void button1_Click(object sender, EventArgs e) 
    { 
     arraylist2.Add("SomeVal"); 
     arraylist4.Add("SomeVal"); 
     if (arraylist2.Count > 0 && arraylist4.Count > 0) 
     { 
      MessageBox.Show(arraylist2.ToString() + "<br />" + arraylist4.ToString() + "<br />"); 
     } 
     else 
     { 
      MessageBox.Show("not array values"); 
     } 
    } 

使用

arraylist2.AddRange(ListBox2.Items); 
arraylist4.AddRange(ListBox4.Items); 

,而不是

arraylist2.Add(ListBox2.Text.ToString()); 
arraylist4.Add(ListBox4.Text.ToString()); 
+0

在我的情况是什么“SomeVal”? –

+0

你想要的值在列表中...... – Rohit

+0

请参阅**编辑#2 **第一个问题...输出不会改变... –

0

不知道我完全理解这个问题是在这里什么,所以专注于您的评论“有没有什么办法来检查,如果数组存在或具有价值?”您可以使用ArrayList.Count属性来确定数组列表是否实际包含任何内容。所以:

if(arraylist2.Count > 0 && arraylist4.Count > 0) 
{ 
    //your code 
} 

如果你想在阵列中显示的每个项目中列出您可能会通过这些循环是这样的:

for(int i = 0; i < arraylist2.Count; i++) 
{ 
    Response.Write(arraylist2[i].ToString() + ", " + arraylist4[i].ToString()) //or whatever you want to write. 
} 

这是假设项目的数量在每个ArrayList中的相同。如果没有,那么得到这两个列表的计数,并简单地遍历第二个列表。

编辑

按我的意见,你可能会更好过改变你的ArrayList到列表。所以,你可以有这样的事情:

List<string> list2 = new List<string>(); 
list2.Add(//your values); 

这将允许您使用Count属性就像ArrayList的,但你应该能够简单地通过它的索引(无toString()方法需要)显示每个项目。

EDIT 2

好吧,在看到您的编辑#2和编辑#3你还没有加入从列表框项目到的ArrayList正常。您需要添加它们是这样的:

for (int j = 0; j < ListBox2.Items.Count; j++) 
{ 
    if (!arraylist2.Contains(ListBox2.Items[j])) 
    { 
     arraylist2.Add(ListBox2.Items[j]); 
    } 
} 
+0

您可以使用System.Linq中定义的Count或Any扩展方法。 – Sandeep

+0

@AntonioMailtraq - 已更新。对不起,我在想Array,而不是ArrayList。现在更新使用ArrayList.Count(请参阅https://msdn.microsoft.com/en-us/library/system.collections.arraylist.count(v=vs.110).aspx) – sr28

+0

好的,请参阅**编辑* *在我的第一个问题。我总是在输出**“不是数组值**”** –

1

转换arraylist2.ToString()犯规太大的意义。如果你想有一个列表转换为字符串,你可以做这样的事情:

ArrayList arrayList = new ArrayList() { 1, 2, 3 }; 
StringBuilder sb = new StringBuilder(); 

foreach (object o in arrayList) 
{ 
    sb.Append(o); 
} 
string s = sb.ToString(); 
+0

我的问题是检查** arrayList计数** ...不工作......我总是**“不是数组值”** –

+0

也许arraylistist2.Count或arraylist4.Count是0?你如何填写这份名单? – Gane

+0

不是... arraylist2.Count是一个和arraylist4.Count是一个...但输出不是数组值... –

相关问题