2011-09-02 29 views
1

第四次嘗試應用此修復程序後詢問此問題。我的問題是我有2個列表框,其中一個擁有ID,另一個擁有Cell ID。 ID列表框有10個項目(用於測試),單元格框有5個ID。我試圖處理listId和的ListCell同時創造更新的鏈接...Listbox對於不通過列表框循環的語句

例如MakeReq將創建txtWebUpdate.Text listID的ListCell & IRE = 1,在這

 store.domain.com/101/sec01&ire=1 
     store.domain.com/102/sec02&ire=1 
     store.domain.com/103/sec03&ire=1 
     store.domain.com/104/sec04&ire=1 
     store.domain.com/105/sec05&ire=1 
     store.domain.com/106/sec01&ire=1 <- notice how listCell starts over it 
continues to loop applying sections until the ListID is complete. 

這是我一直在使用的代碼。我的代碼發生了什麼事情,它只是選擇第一個項目。它完成後不會進入下一個項目。

 if (listId.Items.Count != 0 && listCell.Items.Count != 0) 
     { 
      for (int a = 0; a < listId.Items.Count; a++) 
      { 
       for (int b = 0; b < listCell.Items.Count; b++) 
       { 
        lblID.Text = listId.Items[a].ToString(); 
        MakeReq(txtWebUpdate.Text + listId.Items[a].ToString() + 
         "&ire=1", listCell.Items[b].ToString()); 
        //System.Threading.Thread.Sleep(5); 
       } 
      } 
     } 

如果可以幫忙,請只評論。我幾乎肯定問題是for語句以及它如何選擇列表項,但我可能是錯的。

編輯更多信息:經過測試@duffp的示例/建議下面的問題肯定與我的For語句有關。發生了什麼是它的計數,我在ListCell中有5個條目,然後輸出一個(相同的)ListID 5次,但與不同的ListCell。有人可以幫我按照我上面的錯誤寫下來嗎?

+0

您的列表框更新是否觸發回發,讓您走出循環? –

+0

@kcsavage是的。我用vb6做這樣的事情,但是我會在每個過程之後使用Remove屬性去取出這個項目。所以它總是抓住第一個項目。但在這種情況下,我正在處理兩個ListBox,其中一個listCell不能從其中刪除項目,因爲它需要連續循環,爲每個ListID – acctman

回答

0

在MakeReq方法調用中可能有些事要做。我只是想下面的代碼:

private void Form1_Load(object sender, EventArgs e) 
    { 

     if (listId.Items.Count != 0 && listCell.Items.Count != 0) 
     { 
      for (int a = 0; a < listId.Items.Count; a++) 
      { 
       for (int b = 0; b < listCell.Items.Count; b++) 
       { 
        lblID.Text = listId.Items[a].ToString(); 
        MakeReq(listId.Items[a].ToString(), listCell.Items[b].ToString()); 

       } 
      } 
     }    
    } 
    public void MakeReq(string listId, string cellId) 
    { 
     Console.WriteLine("store.domain.com/" + listId + "sec01&ire=" + cellId); 
    } 

它給輸出:

store.domain.com/ID_10sec01&ire=Cell_1 
store.domain.com/ID_10sec01&ire=Cell_2 
store.domain.com/ID_10sec01&ire=Cell_3 
store.domain.com/ID_10sec01&ire=Cell_4 
store.domain.com/ID_10sec01&ire=Cell_5 
store.domain.com/ID_10sec01&ire=Cell_6 
store.domain.com/ID_10sec01&ire=Cell_7 
store.domain.com/ID_10sec01&ire=Cell_8 
store.domain.com/ID_10sec01&ire=Cell_9 
store.domain.com/ID_10sec01&ire=Cell_10 

這不正是你想要的嗎?

+0

store.domain.com/ID_01 sec 01&ire = 1 store提供SecID。 domain.com/ListID + ListCell ... ex:/ 01sec01&ire = 1一旦ListCell達到其結尾,它將從/ 11sec01&ire = 1開始。所有listCell正在做的是在一個循環中應用部分,而ListID處理一次從上到下直通 – acctman

+0

你有ID_10這些ID總是不同的。看看我的輸出示例up top store.domain.com/101/sec01&ire=1 101是ListID sec01是listCell ...並注意當ListID到達106時會發生什麼,ListCell重新開始,爲什麼因爲ListCell只有5個條目。因此即使必須重新開始,直到listID框完成,它仍會繼續應用secIDs(listCell)。 – acctman

0

我想在MakeReq中可能會有一些副作用,那就是你沒有向我們展示。怎麼樣加入一些調試語句到你的代碼,就像這樣:

if (listId.Items.Count != 0 && listCell.Items.Count != 0) 
    { 
     for (int a = 0; a < listId.Items.Count; a++) 
     { 
      int listCellCount = listCell.Items.Count; 
      for (int b = 0; b < listCell.Items.Count; b++) 
      { 
       lblID.Text = listId.Items[a].ToString(); 
       MakeReq(txtWebUpdate.Text + listId.Items[a].ToString() + 
        "&ire=1", listCell.Items[b].ToString()); 
       //System.Threading.Thread.Sleep(5); 
       Debug.Assert(listCellCount == listCell.Items.Count); 
      } 
     } 
    } 

斷言(listCellCount == listCell.Items.Count)應始終是真實的,如果沒有什麼變化listCell.Items。

+0

我用@duffp在他的例子中使用了console.write。我還有另一個我沒有注意到的問題。在console.write輸出期間不是MakeReq(),我得到了每個進程打印出10次相同的ListID。所以我的For語句都是錯誤的。我之所以說它不是MakeReq是因爲它沒有做任何事情,除了握住字符串之外。如果我使用標籤,我會得到相同的結果不輸出正確的信息。 – acctman