C#判断ListBox中是否存在数组
How to check if an array exists in ListBox in C#
如何在 C# 中检查列表框中是否存在数组
我有两个从数据库中填充的 ListBox,我可以将数组项从 一个列表框 移动到 另一个列表框 在 asp.net 中使用 C#
有什么方法可以检查数组是否存在或是否有值?
我有这行代码,但是当我没有从 第一个 ListBox 填充的 second 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());
}
}
不确定我是否完全理解这里的问题所在,因此关注您的评论 "Is there any way to check if a array exists or has a value?" 您可以使用 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.
}
这假定每个数组列表中的项目数相同。如果不是,则获取两个列表的计数并简单地循环遍历第二个列表。
编辑
根据我的评论,您最好将数组列表更改为列表。所以你可以有这样的东西:
List<string> list2 = new List<string>();
list2.Add(//your values);
这将允许您像使用 arraylist 一样使用计数 属性,但您应该能够通过使用它的索引(不需要 ToString())来简单地显示每个项目。
编辑 2
好的,在看过您的 Edit#2 和 Edit#3 之后,您仍然没有正确地将列表框中的项目添加到数组列表中。您需要像这样添加它们:
for (int j = 0; j < ListBox2.Items.Count; j++)
{
if (!arraylist2.Contains(ListBox2.Items[j]))
{
arraylist2.Add(ListBox2.Items[j]);
}
}
转换 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();
确保列表 arraylist2
和 arraylist4
都有内容。由于您在 if
中使用 &&
,因此两个条件都必须 return 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());
如何在 C# 中检查列表框中是否存在数组
我有两个从数据库中填充的 ListBox,我可以将数组项从 一个列表框 移动到 另一个列表框 在 asp.net 中使用 C#
有什么方法可以检查数组是否存在或是否有值?
我有这行代码,但是当我没有从 第一个 ListBox 填充的 second 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());
}
}
不确定我是否完全理解这里的问题所在,因此关注您的评论 "Is there any way to check if a array exists or has a value?" 您可以使用 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.
}
这假定每个数组列表中的项目数相同。如果不是,则获取两个列表的计数并简单地循环遍历第二个列表。
编辑
根据我的评论,您最好将数组列表更改为列表。所以你可以有这样的东西:
List<string> list2 = new List<string>();
list2.Add(//your values);
这将允许您像使用 arraylist 一样使用计数 属性,但您应该能够通过使用它的索引(不需要 ToString())来简单地显示每个项目。
编辑 2
好的,在看过您的 Edit#2 和 Edit#3 之后,您仍然没有正确地将列表框中的项目添加到数组列表中。您需要像这样添加它们:
for (int j = 0; j < ListBox2.Items.Count; j++)
{
if (!arraylist2.Contains(ListBox2.Items[j]))
{
arraylist2.Add(ListBox2.Items[j]);
}
}
转换 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();
确保列表 arraylist2
和 arraylist4
都有内容。由于您在 if
中使用 &&
,因此两个条件都必须 return 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());