如何在上层类中设置较低级radiobutton的事件处理程序?

时间:2012-04-12 18:37:40

标签: winforms visual-studio-2010 c++-cli

我创建了一个用户控制组子类。有两个radiobuttons。我需要为它们创建事件处理程序。我有两个方向要去。一种是在子类中创建事件处理程序,并让事件处理程序更改子类中的常量。我将使用一个函数来检查上层的子类常量。另一个是在上层类中为子类单选按钮创建事件处理程序。以下是方法1的代码。注意我已注释掉两行(它们是事件处理程序创建),因为它们是错误的,因为它们会产生错误请帮助

    using namespace System;
using namespace System::ComponentModel;
using namespace System::Collections;
using namespace System::Windows::Forms;
using namespace System::Data;
using namespace System::Drawing;
using namespace System::Collections;
using namespace System::Collections::Generic;

#include <stdio.h>
#include <stdlib.h>
#using <mscorlib.dll>

            public ref class temp_groupbox: public GroupBox
            {
            public: temp_groupbox(int a, int b, String ^groupboxname){

                          // Create and initialize a GroupBox and two RadioButton controls.
                            //GroupBox^ groupBox1 = gcnew GroupBox;
                            RadioButton^ radioButton1 = gcnew RadioButton;
                            RadioButton^ radioButton2 = gcnew RadioButton;

                          // Add the RadioButtons to the GroupBox.
                            this->Controls->Add( radioButton1 );
                            this->Controls->Add( radioButton2 );

                            this->Location = System::Drawing::Point(a, b);
                            this->Size = System::Drawing::Size(500, 100);
                            this->TabIndex = 0;
                            this->TabStop = false;

                            radioButton1->Name = L"radioButton1";
                            radioButton2->Name = L"radioButton2";


                            radioButton1->Size = System::Drawing::Size(85, 17);
                            radioButton2->Size = System::Drawing::Size(85, 17);

                            radioButton1->Location = System::Drawing::Point(30, 40);
                            radioButton2->Location = System::Drawing::Point(30, 90);

                            radioButton1->TabIndex = 0;
                            radioButton1->TabStop = true;

                            radioButton2->TabIndex = 1;
                            radioButton2->TabStop = true;

                            radioButton1->UseVisualStyleBackColor = true;
                            radioButton1->CheckedChanged += gcnew System::EventHandler(this, radioButton1_CheckedChanged);
                            radioButton2->UseVisualStyleBackColor = true;
                            radioButton2->CheckedChanged += gcnew System::EventHandler(this, radioButton2_CheckedChanged);

                            this->SuspendLayout();
                            this->ResumeLayout(false);
                            this->PerformLayout();
                }


            public: RadioButton^ radioButton1;
            public: RadioButton^ radioButton2;
            public: int anss;

            void radioButton1_CheckedChanged(Object^ sender, EventArgs^ e)
            {
                anss = 1;
            }

            void radioButton2_CheckedChanged(Object^ sender, EventArgs^ e)
            {
                anss = 2;
            }

            };// end ref class

enter image description here

1 个答案:

答案 0 :(得分:1)

您是否定义了radioButton1_CheckedChangedradioButton2_CheckedChanged方法?

void radioButton1_CheckedChanged(Object^ sender, EventArgs^ e)
{
}

void radioButton2_CheckedChanged(Object^ sender, EventArgs^ e)
{
}

如果这些方法已经存在,请列出您收到的错误消息。如果我们不知道什么是错的,很难弄清楚如何解决问题。


您需要指定要为其创建委托的方法的完整类名,并使用&获取其地址。

gcnew System::EventHandler(this, &temp_groupbox::radioButton1_CheckedChanged)
相关问题