2010-06-04 35 views
13

Hey. Tôi có mã sau điền vào hộp danh sách của tôiListBox và Datasource - ngăn không cho mục đầu tiên được chọn

UsersListBox.DataSource = GrpList; 

Tuy nhiên, sau khi hộp được điền, mục đầu tiên trong danh sách được chọn theo mặc định và sự kiện "thay đổi chỉ mục đã chọn". Làm cách nào để ngăn không cho mục này được chọn ngay sau khi hộp danh sách được điền hoặc làm cách nào để ngăn sự kiện đó kích hoạt?

Cảm ơn

Trả lời

20

Để giữ cho sự kiện từ bắn, đây là hai lựa chọn Tôi đã sử dụng trong quá khứ:

  1. Unregister xử lý sự kiện trong khi thiết DataSource.

    UsersListBox.SelectedIndexChanged -= UsersListBox_SelectedIndexChanged; 
    UsersListBox.DataSource = GrpList; 
    UsersListBox.SelectedIndex = -1; // This optional line keeps the first item from being selected. 
    UsersListBox.SelectedIndexChanged += UsersListBox_SelectedIndexChanged; 
    
  2. Tạo cờ boolean để bỏ qua sự kiện.

    private bool ignoreSelectedIndexChanged; 
    private void UsersListBox_SelectedIndexChanged(object sender, EventArgs e) 
    { 
        if (ignoreSelectedIndexChanged) return; 
        ... 
    } 
    ... 
    ignoreSelectedIndexChanged = true; 
    UsersListBox.DataSource = GrpList; 
    UsersListBox.SelectedIndex = -1; // This optional line keeps the first item from being selected. 
    ignoreSelectedIndexChanged = false; 
    
-4

Có lẽ trong DataSourceChanged bạn có thể kiểm tra trạng thái của SelectedIndex, nếu bạn may mắn bạn có thể sau đó chỉ cần buộc SelectedIndex = -1.

+0

Say gì? Nếu may mắn của bạn? Và tôi thậm chí không thể hiểu câu trả lời của bạn là gì. –

-2

Nếu bạn chỉ muốn xóa giá trị được lựa chọn, bạn có thể sử dụng ClearSelected sau khi bạn thiết lập các DataSource. Nhưng nếu bạn không muốn sự kiện cháy, thì bạn sẽ phải sử dụng một trong những phương pháp của Joseph.

+0

Đây không phải là câu trả lời phù hợp. –

1

Vâng, có vẻ như yếu tố đầu tiên được tự động chọn sau khi ListBox.DataSource được đặt. Các giải pháp khác là tốt, nhưng chúng không giải quyết được vấn đề. Đây là cách tôi đã khắc phục sự cố:

// Get the current selection mode 
SelectionMode selectionMode = yourListBox.SelectionMode; 

// Set the selection mode to none 
yourListBox.SelectionMode = SelectionMode.None; 

// Set a new DataSource 
yourListBox.DataSource = yourList; 

// Set back the original selection mode 
yourListBox.SelectionMode = selectionMode; 
+0

Giải pháp này hoạt động tốt và đơn giản hơn các giải pháp khác –

1

tôi bằng cách sử dụng sau, dường như làm việc cho tôi:

List<myClass> selectedItemsList = dataFromSomewhere 

//Check if the selectedItemsList and listBox both contain items 
if ((selectedItemsList.Count > 0) && (listBox.Items.Count > 0)) 
{ 
    //If selectedItemsList does not contain the selected item at 
    //index 0 of the listBox then deselect it 
    if (!selectedItemsList.Contains(listBox.Items[0] as myClass)) 
    { 
     //Detach the event so it is not called again when changing the selection 
     //otherwise you will get a Stack Overflow Exception 
     listBox.SelectedIndexChanged -= listBox_SelectedIndexChanged; 
     listBox.SetSelected(0, false); 
     listBox.SelectedIndexChanged += listBox_SelectedIndexChanged; 
    } 
} 
0

thiết IsSynchronizedWithCurrentItem="False" và Cũng SelectedIndex=-1 và mọi điều nên làm việc cho bạn

Các vấn đề liên quan