2016-11-15 5 views
0

저는 C++/CLI 프로젝트에서 작업 중이며 새로운 프로젝트입니다. 두 가지 폼이 있는데 하나는 메인 페이지이고 다른 하나는 Loginpage입니다.이벤트 메서드가 호출되지 않았습니다

Loginpage에 성공적으로 로그인하면 서버에서 일부 jpeg 사진을 가져 오기 시작하기를 원합니다.

다음과 같이 위임자, 이벤트 및 메서드를 정의했습니다. 컴파일되고 실행되지만 이벤트가 발생하면 메소드가 실행되지 않습니다.

답을 찾을 수 없습니다. 네가 나를 도울 수 있는지 궁금해?

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); 실행하면 OnJPEGgenerate를 실행해야합니다. 그러나 그렇지 않습니다.

+1

코드를 작성했지만 논리적으로 정확하지는 않습니다. MainPage 생성자에서 한 번, ConnectToolStripMenuItem_Click() 함수에서 Login 클래스의 * 인스턴스 두 개를 만듭니다. 진짜야? 당신은 후자를 보여줄뿐입니다. 이것이 구독해야하는 이벤트입니다. –

답변

0

이 코드는 나를 위해 잘못된 것 같다

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

난 당신이 같은 객체에 대한 참조를 유지해야 알 수있는 바와 같이, 그렇지 않으면

다른 부분이 삭제됩니다 (그리고 충당 핸들러가 호출되지 않습니다) (이벤트 핸들러 등록이 포함되지 않음)

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

답변 해 주셔서 감사합니다. 이미 삭제 되었기 때문에 처리기가 호출되지 않는다고 가정합니다. 어떻게 해결할 수 있습니까? – Eoaneh

+0

'MainPage' 생성자의'loginpage' 인스턴스가 사용되지 않았기 때문에 안전하게 제거 할 수 있습니다. 이벤트 핸들러를'connectToolStripMenuItem_Click'의'L' 인스턴스에 추가하십시오 –

+0

@ Eaneey, Lucas Trzesniewski의 힌트를 따라 코드를 수정해야합니다. – LmTinyToon

관련 문제