未调用事件方法

event method's not called

我正在开发一个 C++/CLI 项目,我是 it.In 我项目的新手 我有 2 种形式,一种是主页,另一种是登录页。

我想要一个事件,当它在登录页面中成功登录时,开始从服务器获取一些 jpeg 图片。

我已经为它定义了委托、事件和方法,如下所示。它可以完全编译 运行s,但是当事件被触发时,它不会 运行 方法。

我找不到答案。我想知道你是否可以帮助我?

MainPage.h:

#pragma once
#include "IPPort.h"
#include "Login.h"
#include "stdafx.h"
#include "Utils.h"


namespace UIv10 {

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

public ref class MainPage : public System::Windows::Forms::Form
{
public:
    MainPage(void)
    {
        InitializeComponent();
        Login ^ loginpage = gcnew Login();
        loginpage->JPEGgenerate += gcnew UIv10::Login::JPEGgeneratorEventHandler(this, &MainPage::OnJPEGgenerate);
    }

protected:
    /// <summary>
    /// Clean up any resources being used.
    /// </summary>
    ~MainPage()
    {
        if (components)
        {
            delete components;
        }
    }
private: System::Windows::Forms::MenuStrip^  menuStrip1;
private: System::Windows::Forms::ToolStripMenuItem^  fileToolStripMenuItem;
private: System::Windows::Forms::ToolStripMenuItem^  connectToolStripMenuItem;
private: System::Windows::Forms::ToolStrip^  toolStrip1;
private: System::Windows::Forms::ToolStripButton^  toolStripButton1;
private: System::Windows::Forms::PictureBox^  pictureBox1;

private:
    /// <summary>
    /// Required designer variable.
    /// </summary>
    System::ComponentModel::Container ^components;

#pragma region Windows Form Designer generated code
    /// <summary>
    /// Required method for Designer support - do not modify
    /// the contents of this method with the code editor.
    /// </summary>
    void InitializeComponent(void)
    {
        System::ComponentModel::ComponentResourceManager^  resources = (gcnew System::ComponentModel::ComponentResourceManager(MainPage::typeid));
        this->menuStrip1 = (gcnew System::Windows::Forms::MenuStrip());
        this->fileToolStripMenuItem = (gcnew System::Windows::Forms::ToolStripMenuItem());
        this->connectToolStripMenuItem = (gcnew System::Windows::Forms::ToolStripMenuItem());
        this->toolStrip1 = (gcnew System::Windows::Forms::ToolStrip());
        this->toolStripButton1 = (gcnew System::Windows::Forms::ToolStripButton());
        this->pictureBox1 = (gcnew System::Windows::Forms::PictureBox());
        this->menuStrip1->SuspendLayout();
        this->toolStrip1->SuspendLayout();
        (cli::safe_cast<System::ComponentModel::ISupportInitialize^>(this->pictureBox1))->BeginInit();
        this->SuspendLayout();

        // 
        // menuStrip1
        // 
        this->menuStrip1->Items->AddRange(gcnew cli::array< System::Windows::Forms::ToolStripItem^  >(1) { this->fileToolStripMenuItem });
        resources->ApplyResources(this->menuStrip1, L"menuStrip1");
        this->menuStrip1->Name = L"menuStrip1";
        // 
        // fileToolStripMenuItem
        // 
        this->fileToolStripMenuItem->DropDownItems->AddRange(gcnew cli::array< System::Windows::Forms::ToolStripItem^  >(1) { this->connectToolStripMenuItem });
        this->fileToolStripMenuItem->Name = L"fileToolStripMenuItem";
        resources->ApplyResources(this->fileToolStripMenuItem, L"fileToolStripMenuItem");
        // 
        // connectToolStripMenuItem
        // 
        this->connectToolStripMenuItem->Name = L"connectToolStripMenuItem";
        resources->ApplyResources(this->connectToolStripMenuItem, L"connectToolStripMenuItem");
        this->connectToolStripMenuItem->Click += gcnew System::EventHandler(this, &MainPage::connectToolStripMenuItem_Click);
        // 
        // toolStrip1
        // 
        this->toolStrip1->Items->AddRange(gcnew cli::array< System::Windows::Forms::ToolStripItem^  >(1) { this->toolStripButton1 });
        resources->ApplyResources(this->toolStrip1, L"toolStrip1");
        this->toolStrip1->Name = L"toolStrip1";
        // 
        // toolStripButton1
        // 
        resources->ApplyResources(this->toolStripButton1, L"toolStripButton1");
        this->toolStripButton1->Name = L"toolStripButton1";
        this->toolStripButton1->Click += gcnew System::EventHandler(this, &MainPage::toolStripButton1_Click);
        // 
        // pictureBox1
        // 
        resources->ApplyResources(this->pictureBox1, L"pictureBox1");
        this->pictureBox1->Name = L"pictureBox1";
        this->pictureBox1->TabStop = false;
        // 
        // MainPage
        // 
        resources->ApplyResources(this, L"$this");
        this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
        this->Controls->Add(this->pictureBox1);
        this->Controls->Add(this->toolStrip1);
        this->Controls->Add(this->menuStrip1);
        this->MainMenuStrip = this->menuStrip1;
        this->Name = L"MainPage";
        this->WindowState = System::Windows::Forms::FormWindowState::Maximized;
        this->menuStrip1->ResumeLayout(false);
        this->menuStrip1->PerformLayout();
        this->toolStrip1->ResumeLayout(false);
        this->toolStrip1->PerformLayout();
        (cli::safe_cast<System::ComponentModel::ISupportInitialize^>(this->pictureBox1))->EndInit();
        this->ResumeLayout(false);
        this->PerformLayout();

    }
#pragma endregion

private: System::Void toolStripButton1_Click(System::Object^  sender, System::EventArgs^  e) {
             IPPort ^ ipportpage = gcnew IPPort();
             ipportpage->Show();
}
private: System::Void connectToolStripMenuItem_Click(System::Object^  sender, System::EventArgs^  e) {
         /*IPPort ^ ipportpage = gcnew IPPort();
         ipportpage->Show();*/
         Login^ L = gcnew Login();
         L->Show();
}

     void OnJPEGgenerate(System::Object^ sender,System::EventArgs^ e);
};
}


void UIv10::MainPage::OnJPEGgenerate(System::Object^ sender,System::EventArgs^ e)
{
    MessageBoxA(NULL, "Failed to Login", "Attention", MB_OK);
    throw gcnew System::NotImplementedException();
}

Login.h:

#include <msclr/marshal.h>
#include <Windows.h>

#ifndef Login_h
#define Login_h

bool FLogin(char use[128], char pass[64]);

#pragma once

namespace UIv10 {

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



    public ref class Login : public System::Windows::Forms::Form
    {

    public: delegate void JPEGgeneratorEventHandler(System::Object^ sender, EventArgs^ e);
    public: event JPEGgeneratorEventHandler^ JPEGgenerate; 
    public:
        Login(void)
        {
            InitializeComponent();
            //
            //TODO: Add the constructor code here
            //
        }

    protected:
        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        ~Login()
        {
            if (components)
            {
                delete components;
            }
        }
    private: System::Windows::Forms::Button^  btnCancel2;
    private: System::Windows::Forms::Button^  btnLogin;
    private: System::Windows::Forms::TextBox^  txtbPassword;
    private: System::Windows::Forms::TextBox^  txtbUserName;
    private: System::Windows::Forms::Label^  lblPassword;
    private: System::Windows::Forms::Label^  lblUserName;


    private:
        /// <summary>
        /// Required designer variable.
        /// </summary>
        System::ComponentModel::Container ^components;

#pragma region Windows Form Designer generated code
        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        void InitializeComponent(void)
        {
            this->btnCancel2 = (gcnew System::Windows::Forms::Button());
            this->btnLogin = (gcnew System::Windows::Forms::Button());
            this->txtbPassword = (gcnew System::Windows::Forms::TextBox());
            this->txtbUserName = (gcnew System::Windows::Forms::TextBox());
            this->lblPassword = (gcnew System::Windows::Forms::Label());
            this->lblUserName = (gcnew System::Windows::Forms::Label());
            this->SuspendLayout();
            // 
            // btnCancel2
            // 
            this->btnCancel2->Location = System::Drawing::Point(158, 174);
            this->btnCancel2->Name = L"btnCancel2";
            this->btnCancel2->Size = System::Drawing::Size(71, 23);
            this->btnCancel2->TabIndex = 11;
            this->btnCancel2->Text = L"Cancel";
            this->btnCancel2->UseVisualStyleBackColor = true;
            // 
            // btnLogin
            // 
            this->btnLogin->Location = System::Drawing::Point(61, 174);
            this->btnLogin->Name = L"btnLogin";
            this->btnLogin->Size = System::Drawing::Size(71, 23);
            this->btnLogin->TabIndex = 10;
            this->btnLogin->Text = L"Login";
            this->btnLogin->UseVisualStyleBackColor = true;
            this->btnLogin->Click += gcnew System::EventHandler(this, &Login::btnLogin_Click);
            // 
            // txtbPassword
            // 
            this->txtbPassword->Location = System::Drawing::Point(112, 109);
            this->txtbPassword->Name = L"txtbPassword";
            this->txtbPassword->PasswordChar = '*';
            this->txtbPassword->Size = System::Drawing::Size(126, 20);
            this->txtbPassword->TabIndex = 9;
            this->txtbPassword->Text = L"roseek";
            // 
            // txtbUserName
            // 
            this->txtbUserName->Location = System::Drawing::Point(112, 65);
            this->txtbUserName->Name = L"txtbUserName";
            this->txtbUserName->Size = System::Drawing::Size(126, 20);
            this->txtbUserName->TabIndex = 8;
            this->txtbUserName->Text = L"roseek";
            // 
            // lblPassword
            // 
            this->lblPassword->Location = System::Drawing::Point(47, 117);
            this->lblPassword->Name = L"lblPassword";
            this->lblPassword->Size = System::Drawing::Size(59, 12);
            this->lblPassword->TabIndex = 7;
            this->lblPassword->Text = L"Password";
            // 
            // lblUserName
            // 
            this->lblUserName->AutoSize = true;
            this->lblUserName->Location = System::Drawing::Point(47, 73);
            this->lblUserName->Name = L"lblUserName";
            this->lblUserName->Size = System::Drawing::Size(60, 13);
            this->lblUserName->TabIndex = 6;
            this->lblUserName->Text = L"User Name";
            // 
            // Login
            // 
            this->AutoScaleDimensions = System::Drawing::SizeF(6, 13);
            this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
            this->ClientSize = System::Drawing::Size(284, 262);
            this->Controls->Add(this->btnCancel2);
            this->Controls->Add(this->btnLogin);
            this->Controls->Add(this->txtbPassword);
            this->Controls->Add(this->txtbUserName);
            this->Controls->Add(this->lblPassword);
            this->Controls->Add(this->lblUserName);
            this->Name = L"Login";
            this->Text = L"Login";
            this->ResumeLayout(false);
            this->PerformLayout();

        }
#pragma endregion

    private: System::Void btnLogin_Click(System::Object^  sender, System::EventArgs^  e) {
                 String ^ username;
                 String ^ password;

                 username = this->txtbUserName->Text;
                 password = this->txtbPassword->Text;

                 using namespace System::Runtime::InteropServices;
                 IntPtr Cusername = Marshal::StringToHGlobalAnsi(username);
                 char* UserName = static_cast<char*>(Cusername.ToPointer());
                 IntPtr Cpassword = Marshal::StringToHGlobalAnsi(password);
                 char* Password = static_cast<char*>(Cpassword.ToPointer());

                 bool Lret = FLogin(UserName, Password);
                 if (Lret == TRUE)
                 {
                     JPEGgenerate(this,e);
                     this->Close();
                 }
                 else
                 {
                     JPEGgenerate(this, e);
                     MessageBoxA(NULL, "Failed to Login", "Attention", MB_OK);
                     this->Close();
                 }
    }
};
}
#endif

对不起,代码很长,我只是不知道问题出在哪里。

实际问题是当 JPEGgenerate(this,e); 运行它应该 运行 在 JPEG 上生成。但事实并非如此。

这段代码对我来说似乎是错误的:

MainPage(void)
{
    InitializeComponent();
    Login ^ loginpage = gcnew Login();
    loginpage->JPEGgenerate += gcnew UIv10::Login::JPEGgeneratorEventHandler(this, &MainPage::OnJPEGgenerate);
}

据我了解,您应该保留对此类对象的引用,否则它将被删除(并且不会调用适当的处理程序)

其他代码(不包含事件处理程序的注册)

 Login^ L = gcnew Login();
 L->Show();