2015-06-17 82 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是一個...但輸出不是數組值... –

相關問題