2009-06-01 84 views
0

我有一個列表框,其中有像A B C D E
我也有兩個按鈕上移下移與它。
我已經在屬性窗口(F4)中將它們的屬性設置爲false。如何在Listbox中使用C#啓用按鈕來查找選定項目的特定位置?

當用戶選擇B或下面的所有項目時,我的上移按鈕應該啓用。它應該被禁用爲A項目

以同樣的方式我的下移按鈕應該啓用時,用戶選擇D或上面的所有項目。它應該被禁用E. E.

你能否給我提供這裏寫的代碼的正確部分。

感謝....

回答

0

我在我的應用程序中做類似的事情。它還處理多個項目的選擇,並檢查所選的多個項目是否連續。

下面的代碼:

private bool SelectionIsContiguous(ListBox lb) 
{ 
    for (int i = 0; i < lb.SelectedIndices.Count - 1; i++) 
     if (lb.SelectedIndices[i] < lb.SelectedIndices[i + 1] - 1) 
      return false; 

    return true; 
} 

private void SetMoveButtonStates() 
{ 
    if (this.listBox.SelectedIndices.Count > 0) 
    { 
     if (this.listBox.SelectedIndices.Count > 1 && !SelectionIsContiguous(this.listBox)) 
     { 
      this.btnMoveUp.Enabled = false; 
      this.btnMoveDown.Enabled = false; 
      return; 
     } 

     int firstSelectedIndex = this.listBox.SelectedIndices[0]; 
     this.btnMoveUp.Enabled = firstSelectedIndex == 0 ? false : true; 

     int lastIndex = this.listBox.Items.Count - 1; 
     int lastSelectedIndex = this.listBox.SelectedIndices[this.listBox.SelectedIndices.Count - 1]; 
     this.btnMoveDown.Enabled = lastSelectedIndex == lastIndex ? false : true; 
    } 
} 
+0

嗨Rashmi, 感謝您的回答一件事我要問。我想從我的列表框中選擇一個文件例如給出above.den我想啓用我的按鈕,因爲我已經寫了這個if(m_lbOPFfiles.SelectedIndex == 0) { m_BtnValidateInput.Enabled = true; } 現在選擇一個只有我的按鈕是啓用,而我想要選擇任何文件,如B C D E它shud被啓用。我已經在SelectIndex更改Clik事件寫了上述部分。 你能幫助沃茨錯誤嗎? – 2009-06-01 12:53:02

0

處理ListBox的SelectedIndexChanged事件。如果SelectedIndex大於0,則啓用「向上移動」。如果是較小的比count - 1,啓用「下移」

0

這是我在listBox_SelectedIndexChanged使用代碼:

this.moveUp.Enabled = this.listBox.SelectedIndex > 0; 
this.moveDown.Enabled = this.listBox.SelectedIndex > -1 && listBox.SelectedIndex < listBox.Items.Count - 1; 

其實它是在一種稱爲從那裏進行代碼級的時候在對話框的初始化也被稱爲。

相關問題