2016-11-15 10 views
0

私はC++/CLIプロジェクトに取り組んでいます。私のプロジェクトでは2つのフォームがあり、1つはメインページ、もう1つは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コンストラクターで1回、connectToolStripMenuItem_Click()関数でLoginクラスの* 2つのインスタンスを作成します。本当ののはどちらですか?後者のみを表示します。だから、あなたが購読する必要があるイベントです。 –

答えて

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

@Eoaneh、あなたはLucas Trzesniewskiのヒントに従ったコードを修正してください。 – LmTinyToon

関連する問題