문제

At the end of a drag&drop operation, I'm showing a form using ShowDialog.
Problem: When the form is closed, my main form is pushed behind any other application windows.

Code:

private void ctrl_DragDrop(object sender, DragEventArgs e) {
    // ...
    if (e.Effect == DragDropEffects.Move) {
    string name = e.Data.GetData(DataFormats.Text).ToString();
    viewHelperForm.ShowDialog(view.TopLevelControl);
    // ...
}

Question: What can I do that the main form stays on top?

도움이 되었습니까?

해결책

Your ShowDialog() call is blocking the DragDrop event. That is very, very bad, it gums up the drag source and make it go catatonic and unresponsive to Windows messages. This has all kinds of side-effects, like your window going catatonic as well or not getting reactivated since the D+D operation isn't completed yet.

Avoid this by only displaying the dialog after the D+D operation is completed. Elegantly done by taking advantage of the Winforms plumbing that allows posting a message to the message queue and get it processed later. Like this:

    private void ctl_DragDrop(object sender, DragEventArgs e) {
        //...
        this.BeginInvoke(new Action(() => {
            viewHelperForm.ShowDialog(view.TopLevelControl);
        }));
    }
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top