简体   繁体   中英

Why is the button's event handler not firing?

I have a button with an event handler attached to it; 2-clicking it in the designer takes me to the code. Nowhere is the handler being unhooked/detached.

Some code I expected to run apparently isn't, so I put a bunch of MessageBox.Show()s in the handler, even at the very beginning, but none of them display (Note: I can't step through the code; I have to do it this way (arggghhhh)).

Here's some of the code:

private void btnFind_Click(object sender, System.EventArgs e) // Find and list Records
{
    MessageBox.Show("Made it into btnFind_Click 0"); //TODO: Remove after debugging
    try
    {
        if (barcodeScanner != null)
        {
            // Turn off the listening
            barcodeScanner.BarcodeScan -= new BarcodeScanner.BarcodeScanEventHandler(barcodeScanner_BarcodeScan);
        }
            MessageBox.Show("Made it into btnFind_Click 1"); //TODO: Remove after debugging . . .

What could be preventing this code from being executed?

UPDATE

Based on Mike C's idea, I added a MessageBox to the button_close handler. And when I click it, it does fire, but only after other code runs first ; in this case, that other code doesn't prevent the Close_Click from (eventually) firing; with the Find button, though, it completely preempts it...IOW, I see the message from the Close button at the end when I click it, but I never see any of the messages in the Find button handler when I click it...

UPDATE 2

Oh my lanta/say it ain't so, Joe! What's happening is an event is being kicked off in the form's overloaded constructor, and somehow this event is always fired just then (after clicking the find button). The message I'm seeing, that preempts everything in the button event handler, takes place in a method which is called by processBarcode() which is called by processBarcode1(), which is invoked from barcodeScanner_BarcodeScan1(), which is called by barcodeScanner_BarcodeScan(), which is set up in frmEntry's overloaded constructor. If the previous coder had intended to drive me insane, he couldn't have done much better.

I guess there's a reason there's so much maintenance work "out there" or "out here": because there's so much bad broken code AND because the cats who make such a mess scratch a bunch of sand on it and walk away.

And this code is chock full of "huh?!?@#$%^?!?" moments, where bizarre gyrations are not commented on at all, and yet there is this comment:

// Check connection
checkConnection();

The problem could be that the Click event of the button is not subscribed to properly. If there is no line resembling

this.btnFind.Click += new System.EventHandler(this.btnFind_Click);

in the Designer file of the form, that's it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM