c ++ CLI事件处理程序未被触发

时间:2010-12-20 22:50:07

标签: event-handling c++-cli

我刚刚进入c ++ CLI并遇到一个问题,其中一些事件处理程序被调用,而另一些则没有。

这是我的事件处理程序列表:

    private: System::Void Form1_Load(System::Object^  sender, System::EventArgs^  e) {
             }
    private: System::Void exitToolStripMenuItem_Click(System::Object^  sender, System::EventArgs^  e) {
                 Application::Exit();
             }
private: System::Void aboutToolStripMenuItem_Click(System::Object^  sender, System::EventArgs^  e) {
             MessageBox::Show("Paul Madsen\nLab 13",
                 "About lab 13", MessageBoxButtons::OK,
                 MessageBoxIcon::Asterisk);
         }

private: System::Void textBox1_TextChanged(System::Object^  sender, System::EventArgs^  e) 
         {
             try
             {
                double tmp = Double::Parse(textBox1->Text);
             }
             catch(...)
             {
                 textBox1->ResetText();
             }

         }
private: System::Void textBox2_TextChanged(System::Object^  sender, System::EventArgs^  e) 
         {
             try
             {
                double tmp = Double::Parse(textBox2->Text);
             }
             catch(...)
             {
                 textBox2->ResetText();
             }
         }
private: System::Void textBox1_Enter(System::Object^  sender, System::EventArgs^  e)
         {
             MessageBox::Show("Test",
                 "test", MessageBoxButtons::OK,
                 MessageBoxIcon::Asterisk);
         }
private: System::Void button1_Click(System::Object^  sender, System::EventArgs^  e) {
             double tmp = (Double::Parse(textBox1->Text) - 32) * 5/9;
             textBox2->Text = System::Convert::ToString(tmp);

         }
private: System::Void button2_Click(System::Object^  sender, System::EventArgs^  e) {
              double tmp = (Double::Parse(textBox1->Text) - 32) * 5/9;
              textBox1->Text = System::Convert::ToString(tmp);

          }

button1_Click工作正常,但即使它们基本相同,也永远不会触发button2_Click。那是为什么?

1 个答案:

答案 0 :(得分:1)

回顾源代码文件的开头,在InitializeComponent()中。您应该可以毫不费力地看到button1-> Click事件处理程序分配。你看到按钮2 - >点击吗?

删除设计器中的按钮,将其添加回来。双击它。或者只是把它留下来,没有必要让两个按钮做同样的事情。