简体   繁体   English

为什么按钮的事件处理程序没有触发?

[英]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)). 我希望运行的一些代码显然不是,所以我把一堆MessageBox.Show()s放在处理程序中,即使在最开始,但它们都没有显示(注意:我无法单步执行代码;我必须这样做(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 UPDATE

Based on Mike C's idea, I added a MessageBox to the button_close handler. 根据Mike C的想法,我在button_close处理程序中添加了一个MessageBox。 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; 在这种情况下,其他代码不会阻止Close_Click(最终)触发; 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 更新2

Oh my lanta/say it ain't so, Joe! 哦,我的lanta /说不是这样,乔! 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. 我正在看到的消息,它取代了按钮事件处理程序中的所有内容,发生在processBarcode()调用的方法中,该方法由processBarcode1()调用,该方法由barcodeScanner_BarcodeScan1()调用,由barcodeScanner_BarcodeScan调用( ),这是在frmEntry的重载构造函数中设置的。 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. 问题可能是按钮的Click事件未正确订阅。 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. 在表单的Designer文件中,就是这样。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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