2012-11-01 2 views
2

특정 이벤트가 호출되는 순서를 변경할 수 있습니까? 예를 들어 ComboBox가 있고 선택 항목이 변경되면 TextChanged 이벤트가 호출되기 전에 SelectedIndexChanged 이벤트가 호출되기를 바랍니다. 솔직히 말하면, 새 항목을 선택했기 때문에 TextChanged 이벤트가 호출되었는지 알 수 없으므로 SelectedIndexChanged 이벤트 전에 TextChanged 이벤트를 호출하는 것은 매우 어리 석다는 것이 좋습니다.이벤트 순서를 변경할 수 있습니까?

도움을 주시면 감사하겠습니다.

// from http://referencesource.microsoft.com 
if (IsHandleCreated) { 
    OnTextChanged(EventArgs.Empty); 
} 

OnSelectedItemChanged(EventArgs.Empty); 
OnSelectedIndexChanged(EventArgs.Empty); 

각 이벤트에 대한 핸들러가있는 경우 그들을 당신이 을 가질 수있다 특정 순서로 실행해야합니다 : 그것은 하드 제어 부호로 부호화 것 -

+0

짧은 대답은 아니오입니다. .NET 컨트롤에서 발생한 이벤트의 순서는 변경할 수 없습니다. 하지만 어쩌면 너는 필요하지 않을거야. 당신의 유스 케이스는 무엇입니까? 즉,'TextChanged' 내에서'SelectedIndexChanged'가 실행되었는지를 알 필요가 있기 때문에 무엇을하려고합니까? – ean5533

+0

하지만 본문이 바뀌 었습니다 ... 그래서 그것이 불려진 것입니다. 전혀 바보 같이 보이지 않습니다. –

+0

다른 항목을 선택했기 때문에 텍스트가 변경되었습니다. _ 그것이 이해할 수 없기 때문에 그것을 잘못된 순서로 말하는 것은 어리 석다. – ygoe

답변

3

아니, 당신은 순서를 변경할 수 없습니다 TextChanged 이벤트는 SelectedIndexChanged 이벤트가 발생했음을 나타내는 지표를 찾은 다음 SelectedIndexChanged 처리기에서 TextChanged 처리기를 호출하거나 SelectedIndexChanged 처리기로 처리합니다.

에 따라 달라집니다. 왜 그들이에 따라 특정 순서로 실행해야합니다.

0

할 수있는 것이 있지만 해결책이 좋지 않습니다. & 미래에 응용 프로그램을 유지 관리 할 사람을 혼동하고 자신이 수행 한 것을 잊어 버렸을 때 반드시 혼동하게됩니다. 그러나 여기 어쨌든입니다 :

아이디어는 이벤트가

// two fields to keep the previous values of Text and SelectedIndex 
private string _oldText = string.Empty; 
private int _oldIndex = -2; 
. 
// somewhere in your code where you subscribe to the events 
this.ComboBox1.SelectedIndexChanged += 
new System.EventHandler(ComboBox1_SelectedIndexChanged_AND_TextChanged); 
this.ComboBox1.TextChanged+= 
new System.EventHandler(ComboBox1_SelectedIndexChanged_AND_TextChanged); 
. 
. 

/// <summary> 
/// Shared event handler for SelectedIndexChanged and TextChanged events. 
/// In case both index and text change at the same time, index change 
/// will be handled first. 
/// </summary> 
private void ComboBox1_SelectedIndexChanged_AND_TextChanged(object sender, 
     System.EventArgs e) 
{ 

    ComboBox comboBox = (ComboBox) sender; 

    // in your case, this will execute on TextChanged but 
    // it will actually handle the selected index change 
    if(_oldIndex != comboBox.SelectedIndex) 
    { 
     // do what you need to do here ... 

     // set the current index to this index 
     // so this code doesn't exeute again 
     oldIndex = comboBox.SelectedIndex; 
    } 
    // this will execute on SelecteIndexChanged but 
    // it will actually handle the TextChanged event 
    else if(_oldText != comboBox.Test) 
    { 
     // do what you need to ... 

     // set the current text to old text 
     // so this code doesn't exeute again 
     _oldText = comboBox.Text;  
    } 

} 
따라 처리하는 방법을 주문할 수 있도록 같은 기능은 모두 이벤트를 처리 인덱스의 이전 값의 & 텍스트를 추적하는 것입니다

이 코드는 이벤트가 별도로 실행될 때 작동합니다. 텍스트 만 변경하거나 인덱스 만 변경하십시오.

0
if (SelectedIndex == -1) // only the text was changed. 
관련 문제