2017-07-21 6 views
0

最初の項目をクリックして最初のリストボックスをクリックし、リストボックス2に項目を読み込む一連のリストボックスを作成しています。リストボックス2の項目をクリックすると、リストボックス3.リストボックス1のアイテムをクリックすると、リストボックス2のアイテムがロードされますが、リストボックス2のアイテムをクリックしてリストボックス3をロードすると、何もロードされず、エラーも表示されません。リストボックス1で動作するときにこのコードが機能しない理由がわかりませんか?lisbox.SelectedIndexを取得して、複数のリストボックスで選択した複数の項目を扱うにはどうすればよいですか?

 if (listBox1.SelectedIndex == 0 && !listBox2.Items.Contains("Directory50  >")) 
     { 
      if (listBox2.Items.Count >= 5) 
      { 
       for (int i = listBox2.Items.Count - 1; i >= 0; i--) 
       { 
        // do with listBox1.Items[i] 

        listBox2.Items.RemoveAt(i); 
       } 
      } 
      for (int x = 0; x <= 4; x++) 
      { 
       listBox2.Items.Add("Directory5" + x.ToString() + "  >"); 
      } 

      if (listBox2.SelectedIndex == 0) 
      {///I get no response here from listbox2 when mouse clicking the first item in listbox2 
       for (int x = 0; x <= 2; x++) 
       { 
        listBox3.Items.Add("Directory51" + x.ToString() + ".txt"); 
       } 

      } 

     } 

答えて

0
listBox1.SelectedIndex > 0 

OR

listBox1.SelectedIndex > -1 (-1 if first item needs to load something) 

listBox1.SelectedIndex = -1 // means nothing selected 

// means an item selected and first item is with index = 0 
listBox1.SelectedIndex = 0 or 1 or 2 ... etc 
+0

ごめんなさいあなたはlistBox1.SelectedIndexまたはlistBox2.SelectedIndex話していた私はエラーを取得する場所listBox2.SelectedIndexがあるため。 – Lecy277

+0

どちらも同じ問題を抱えており、この情報を考慮してそれに基づいたコードが必要です –

関連する問題