Search code examples
c#winformseventsmessagebox

Calling event skip message box


This is a case of weird behaviour.
I'm calling a TreeView_Drag_Drop event (I don't think the type of event is relevant but whatever). Inside my event I call 3 MessageBox in a row. Like this :

MessageBox.Show("A");
MessageBox.Show("B");
MessageBox.Show("C");

The output will be B and C. If I put a breakpoint on MessageBox.Show("A"); it will stop there but won't pop the MessageBox.
If I replace my code for this :

Console.WriteLine("A");
MessageBox.Show("B");
MessageBox.Show("C");

The ouput will be this A (in the console), B,C.

My last try is to make a dummy MessageBox to see if the compiler only rules out the first box he sees. So I replaced my code for this :

MessageBox.Show("Kill this box");
MessageBox.Show("A");
MessageBox.Show("B");
MessageBox.Show("C");

The output is A,B, C.

Is there a simple explanation for this without any further code ? (Anyway that's the only code in the event) because I just can't explain this behaviour.


Update with another test

I tried putting them in a foreach loop.

foreach ( // logic )
{
  MessageBox.Show("A");
  MessageBox.Show("B");
  MessageBox.Show("C");
}

For my first iteration, the output will be B, C.
But for all the other iterations, the output will be A,B,C. Like it already ruled out one MessageBox so it's okay to go through.


Code Update

This code works perfectly on a prototype. But the exact same code doesn't in my program.
Please note that for multiple language support and stuff the forms are called by reflexion. (Don't think that's relevant at all but anyway).

    private void Form1_Load(object sender, EventArgs e)
    {
        treeView1.Nodes.Add("Test");
        this.treeView1.ItemDrag += new ItemDragEventHandler(this.treeView_ItemDrag);
        this.treeView2.ItemDrag += new ItemDragEventHandler(this.treeView_ItemDrag);
        this.treeView1.DragEnter += new DragEventHandler(this.treeView_DragEnter);
        this.treeView2.DragEnter += new DragEventHandler(this.treeView_DragEnter);
        this.treeView1.DragDrop += new DragEventHandler(this.treeView_DragDrop);
        this.treeView2.DragDrop += new DragEventHandler(this.treeView_DragDrop);
    }

    private void treeView_DragEnter(object sender,
        System.Windows.Forms.DragEventArgs e)
    {
        e.Effect = DragDropEffects.Move;
    }

    private void treeView_ItemDrag(object sender,
        System.Windows.Forms.ItemDragEventArgs e)
    {
        DoDragDrop(e.Item, DragDropEffects.Move);
    }

    private void treeView_DragDrop(object sender, System.Windows.Forms.DragEventArgs e)
    {

        MessageBox.Show("A");
        MessageBox.Show("B");
        MessageBox.Show("C");

    }

I know it works. The problem is it doesn't in my program and it's Exactly the same code. I'm looking for any possible reasons why it would behave differently in my solution but I can't find anything.


Solution

  • DragAndDrop event

    Any exception raised in them is swallowed without any diagnostic. The probable philosophy behind that is that they are likely to fail because they handle data that is produced by another program. And that a buggy program that produces bad data like that should not be allowed to crash yours.

    If you need to debug your code then use Debug + Exceptions, tick the Thrown checkbox for CLR exceptions. The debugger will now stop when the exception is thrown.

    If you want the user to know about any exceptions then you'll need to use try/catch to catch the exception before it is swallowed. With the slight risk that any bugs in another program become yours to explain.

    I found this on a similar question. Finally.. this behaviour is so hard to debug.