2013-02-27 2 views
0

현재 한 양식에서 다른 양식으로 데이터를 전달해야하는 C++ 프로젝트에서 작업 중입니다. 이 경우 사용자는 사용자 이름과 암호로 CreateAccount Form (Create_Account.h)에 계정을 만듭니다. 이 사용자 이름과 비밀번호는 객체의 비공개 속성으로 저장되며 (user), 로그인 양식 (Login.h)으로 진행됩니다. 그러나 이전 양식으로이 양식에 입력 된 암호를 확인하는 시스템을 얻을 수 없습니다. 객체의 개인 속성 (user)으로 입력 한 사용자 이름과 암호를 확인할 수 있도록이 로그인 양식에 객체 (user)를 전달해야한다는 것을 알았습니다. 그러나 양식에 개체를 전달하는 방법에 대한 구문을 잘 모르겠습니다. (함수를 통해 객체를 전달하는 것에 만 익숙합니다.)클래스/객체/데이터 전달 중입니까? 두 양식 사이

이 언어를 처음 사용하며 실제로 도움을 얻을 수 있습니다.

아래에 작성한 코드가 있습니다.

**//CreateAccount.h** 

#pragma once 
#include "user2.h" 
#include <stdlib.h> 
#include "ExpenseTracker.h" 
#include "User_Login.h" 
#include <string> 
#include <msclr/marshal_cppstd.h> 

namespace EzXpns2 { 

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

    /// <summary> 
    /// Summary for Create_Account 
    /// </summary> 
    public ref class Create_Account : public System::Windows::Forms::Form 
    { 
    public: 
     Create_Account(void) 
     { 
      InitializeComponent(); 
      // 
      //TODO: Add the constructor code here 
      // 
     } 

    protected: 
     /// <summary> 
     /// Clean up any resources being used. 
     /// </summary> 
     ~Create_Account() 
     { 
      if (components) 
      { 
       delete components; 
      } 
     } 
    private: System::Windows::Forms::Button^ btnCreateNewAccount; 
    protected: 

    protected: 
    private: System::Windows::Forms::TextBox^ txtBxPassword; 
    private: System::Windows::Forms::Label^ lblPassword; 
    private: System::Windows::Forms::TextBox^ txtBxUsername; 
    private: System::Windows::Forms::Label^ lblUsername; 
    private: System::Windows::Forms::TextBox^ txtBxReenterPassword; 

    private: System::Windows::Forms::Label^ label1; 
    //private: System::Windows::Forms:User_Login^ userlogin; 
    //private User_Login^ UserLogin; 


    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->btnCreateNewAccount = (gcnew System::Windows::Forms::Button()); 
      this->txtBxPassword = (gcnew System::Windows::Forms::TextBox()); 
      this->lblPassword = (gcnew System::Windows::Forms::Label()); 
      this->txtBxUsername = (gcnew System::Windows::Forms::TextBox()); 
      this->lblUsername = (gcnew System::Windows::Forms::Label()); 
      this->txtBxReenterPassword = (gcnew System::Windows::Forms::TextBox()); 
      this->label1 = (gcnew System::Windows::Forms::Label()); 
      this->SuspendLayout(); 
      // 
      // btnCreateNewAccount 
      // 
      this->btnCreateNewAccount->Location = System::Drawing::Point(147, 147); 
      this->btnCreateNewAccount->Name = L"btnCreateNewAccount"; 
      this->btnCreateNewAccount->Size = System::Drawing::Size(126, 23); 
      this->btnCreateNewAccount->TabIndex = 3; 
      this->btnCreateNewAccount->Text = L"Create New Account"; 
      this->btnCreateNewAccount->UseVisualStyleBackColor = true; 
      this->btnCreateNewAccount->Click += gcnew System::EventHandler(this, &Create_Account::btnCreateNewAccount_Click); 
      // 
      // txtBxPassword 
      // 
      this->txtBxPassword->Location = System::Drawing::Point(147, 68); 
      this->txtBxPassword->Name = L"txtBxPassword"; 
      this->txtBxPassword->Size = System::Drawing::Size(126, 20); 
      this->txtBxPassword->TabIndex = 1; 
      this->txtBxPassword->UseSystemPasswordChar = true; 
      // 
      // lblPassword 
      // 
      this->lblPassword->AutoSize = true; 
      this->lblPassword->Location = System::Drawing::Point(26, 73); 
      this->lblPassword->Name = L"lblPassword"; 
      this->lblPassword->Size = System::Drawing::Size(56, 13); 
      this->lblPassword->TabIndex = 12; 
      this->lblPassword->Text = L"Password:"; 
      // 
      // txtBxUsername 
      // 
      this->txtBxUsername->Location = System::Drawing::Point(149, 27); 
      this->txtBxUsername->Name = L"txtBxUsername"; 
      this->txtBxUsername->Size = System::Drawing::Size(126, 20); 
      this->txtBxUsername->TabIndex = 0; 
      // 
      // lblUsername 
      // 
      this->lblUsername->AutoSize = true; 
      this->lblUsername->Location = System::Drawing::Point(24, 29); 
      this->lblUsername->Name = L"lblUsername"; 
      this->lblUsername->Size = System::Drawing::Size(86, 13); 
      this->lblUsername->TabIndex = 10; 
      this->lblUsername->Text = L"Enter Username:"; 
      // 
      // txtBxReenterPassword 
      // 
      this->txtBxReenterPassword->Location = System::Drawing::Point(147, 100); 
      this->txtBxReenterPassword->Name = L"txtBxReenterPassword"; 
      this->txtBxReenterPassword->Size = System::Drawing::Size(126, 20); 
      this->txtBxReenterPassword->TabIndex = 2; 
      this->txtBxReenterPassword->UseSystemPasswordChar = true; 
      // 
      // label1 
      // 
      this->label1->AutoSize = true; 
      this->label1->Location = System::Drawing::Point(26, 105); 
      this->label1->Name = L"label1"; 
      this->label1->Size = System::Drawing::Size(100, 13); 
      this->label1->TabIndex = 15; 
      this->label1->Text = L"Re-enter Password:"; 
      // 
      // Create_Account 
      // 
      this->AutoScaleDimensions = System::Drawing::SizeF(6, 13); 
      this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font; 
      this->ClientSize = System::Drawing::Size(309, 188); 
      this->Controls->Add(this->txtBxReenterPassword); 
      this->Controls->Add(this->label1); 
      this->Controls->Add(this->btnCreateNewAccount); 
      this->Controls->Add(this->txtBxPassword); 
      this->Controls->Add(this->lblPassword); 
      this->Controls->Add(this->txtBxUsername); 
      this->Controls->Add(this->lblUsername); 
      this->Name = L"Create_Account"; 
      this->Text = L"Welcome to EzXpns -Create Account"; 
      this->ResumeLayout(false); 
      this->PerformLayout(); 

     } 
#pragma endregion 

private: System::Void btnCreateNewAccount_Click(System::Object^ sender, System::EventArgs^ e) { 

       ExpenseTracker *myTracker = new ExpenseTracker(); 
       string strUsername, strPassword, strReenterPassword; 
       bool checkPassword = false; 

       System::String^ str1 = txtBxUsername->Text; 
       strUsername = marshal_as<string>(str1); 
       //convert system string to string 

       System::String^ str2 = txtBxPassword->Text;  
       strPassword = marshal_as<string>(str2); 

       System::String^ str3 = txtBxReenterPassword->Text; 
       strReenterPassword = marshal_as<string>(str3); 

       if (strPassword == strReenterPassword) 
        checkPassword = true; 

       if (checkPassword) 
       { 
        User *newUser = new User(strUsername, strReenterPassword); 
        (*myTracker).addUser(newUser); 
        this->Hide(); 
        User_Login^ testDialog = gcnew User_Login; 
        testDialog->Show(); 
       } 
       else 
        MessageBox::Show("Please re-enter your password.\n\nThe password you entered do not match.\nPlease try again.", 
     "Create User Failure", MessageBoxButtons::OK, MessageBoxIcon::Warning); 
     } 
}; 
} 

//UserLogin.h 
#pragma once 
#include "user2.h" 
#include <stdlib.h> 
#include "ExpenseTracker.h" 
#include "Create_Account.h" 
#include "Login.h" 
#include <string> 
#include <msclr/marshal_cppstd.h> 

namespace EzXpns2 { 

    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 msclr::interop; 

    /// <summary> 
    /// Summary for User_Login 
    /// </summary> 
    public ref class User_Login : public System::Windows::Forms::Form 
    { 
    public: 
     User_Login(void)//ExpenseTracker *myPtrTracker) 
     { 
      InitializeComponent(); 
      // 
      //TODO: Add the constructor code here 
      // 
      //CreateAccountForm = CreateAcct; 
      //ExpenseTracker *myTracker = myPtrTracker; 
     } 

    protected: 
     /// <summary> 
     /// Clean up any resources being used. 
     /// </summary> 
     ~User_Login() 
     { 
      if (components) 
      { 
       delete components; 
      } 
     } 
    private: System::Windows::Forms::Button^ btnLogin; 
    protected: 
    private: System::Windows::Forms::TextBox^ txtBxPassword; 
    private: System::Windows::Forms::Label^ lblPassword; 
    private: System::Windows::Forms::TextBox^ txtBxUsername; 
    private: System::Windows::Forms::Label^ lblUsername; 
    private: System::Windows::Forms::Form^ CreateAccountForm; 


    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->btnLogin = (gcnew System::Windows::Forms::Button()); 
      this->txtBxPassword = (gcnew System::Windows::Forms::TextBox()); 
      this->lblPassword = (gcnew System::Windows::Forms::Label()); 
      this->txtBxUsername = (gcnew System::Windows::Forms::TextBox()); 
      this->lblUsername = (gcnew System::Windows::Forms::Label()); 
      this->SuspendLayout(); 
      // 
      // btnLogin 
      // 
      this->btnLogin->Location = System::Drawing::Point(80, 110); 
      this->btnLogin->Name = L"btnLogin"; 
      this->btnLogin->Size = System::Drawing::Size(126, 23); 
      this->btnLogin->TabIndex = 9; 
      this->btnLogin->Text = L"Login"; 
      this->btnLogin->UseVisualStyleBackColor = true; 
      this->btnLogin->Click += gcnew System::EventHandler(this, &User_Login::btnLogin_Click); 
      // 
      // txtBxPassword 
      // 
      this->txtBxPassword->Location = System::Drawing::Point(80, 65); 
      this->txtBxPassword->Name = L"txtBxPassword"; 
      this->txtBxPassword->Size = System::Drawing::Size(126, 20); 
      this->txtBxPassword->TabIndex = 8; 
      this->txtBxPassword->UseSystemPasswordChar = true; 
      this->txtBxPassword->TextChanged += gcnew System::EventHandler(this, &User_Login::txtBxPassword_TextChanged); 
      // 
      // lblPassword 
      // 
      this->lblPassword->AutoSize = true; 
      this->lblPassword->Location = System::Drawing::Point(20, 68); 
      this->lblPassword->Name = L"lblPassword"; 
      this->lblPassword->Size = System::Drawing::Size(56, 13); 
      this->lblPassword->TabIndex = 7; 
      this->lblPassword->Text = L"Password:"; 
      this->lblPassword->Click += gcnew System::EventHandler(this, &User_Login::lblPassword_Click); 
      // 
      // txtBxUsername 
      // 
      this->txtBxUsername->Location = System::Drawing::Point(82, 24); 
      this->txtBxUsername->Name = L"txtBxUsername"; 
      this->txtBxUsername->Size = System::Drawing::Size(126, 20); 
      this->txtBxUsername->TabIndex = 6; 
      this->txtBxUsername->TextChanged += gcnew System::EventHandler(this, &User_Login::txtBxUsername_TextChanged); 
      // 
      // lblUsername 
      // 
      this->lblUsername->AutoSize = true; 
      this->lblUsername->Location = System::Drawing::Point(18, 24); 
      this->lblUsername->Name = L"lblUsername"; 
      this->lblUsername->Size = System::Drawing::Size(58, 13); 
      this->lblUsername->TabIndex = 5; 
      this->lblUsername->Text = L"Username:"; 
      this->lblUsername->Click += gcnew System::EventHandler(this, &User_Login::lblUsername_Click); 
      // 
      // User_Login 
      // 
      this->AutoScaleDimensions = System::Drawing::SizeF(6, 13); 
      this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font; 
      this->ClientSize = System::Drawing::Size(231, 155); 
      this->Controls->Add(this->btnLogin); 
      this->Controls->Add(this->txtBxPassword); 
      this->Controls->Add(this->lblPassword); 
      this->Controls->Add(this->txtBxUsername); 
      this->Controls->Add(this->lblUsername); 
      this->Name = L"User_Login"; 
      this->Text = L"Welcome to EzXpns"; 
      this->Load += gcnew System::EventHandler(this, &User_Login::User_Login_Load); 
      this->ResumeLayout(false); 
      this->PerformLayout(); 

     } 
#pragma endregion 
    private: System::Void btnLogin_Click(System::Object^ sender, System::EventArgs^ e) { 

       //User myUser; 
       //ExpenseTracker myTracker; 

       System::String^ str1 = txtBxUsername->Text; 
       string strUsername = marshal_as<string>(str1); 
       //convert system string to string 

       System::String^ str2 = txtBxPassword->Text;  
       string strPassword = marshal_as<string>(str2); 
       bool success = false; 
       success = (*myTracker).login(strUsername, strPassword); 
       if (success) 
       { 
         this->Hide(); 
         Login^ testDialog = gcnew Login; 
         testDialog->Show(); 
       } 
       else 
       { 
         MessageBox::Show("Please re-enter your password.\n\nThe password you entered is incorrect.\nPlease try again (make sure your caps lock is off).", 
     "Login Failure", MessageBoxButtons::OK, MessageBoxIcon::Warning); 
       } 
      } 
private: System::Void lblUsername_Click(System::Object^ sender, System::EventArgs^ e) { 
      } 
private: System::Void lblPassword_Click(System::Object^ sender, System::EventArgs^ e) { 
      } 
private: System::Void txtBxPassword_TextChanged(System::Object^ sender, System::EventArgs^ e) { 
      } 
private: System::Void txtBxUsername_TextChanged(System::Object^ sender, System::EventArgs^ e) { 
      } 
private: System::Void User_Login_Load(System::Object^ sender, System::EventArgs^ e) { 

     } 
}; 
} 

//ExpenseTracker.h 
#ifndef _EXPENSETRACKER_H 
#define _EXPENSETRACKER_H 

#include <string> 
#include <vector> 
//#include <iostream> 
#include "user2.h" 

using namespace std; 

class ExpenseTracker 
{ 
private: 
    vector<User*> allMyUsers; 

public: 
    ExpenseTracker(); //empty constructor 
    void addUser(User*); 
    int findUser(string); 
    bool login(string, string); 
}; 

#endif; 
+0

승인 나는 네임 스페이스에 정적 클래스를 정의하고 인수를 전달하는 데 사용하거나 데이터를 교환하기 위해 참조 또는 개체에 대한 포인터를 허용하는 생성자를 정의합니다. –

+3

코드가 다소 길기 때문에 많은 사람들이 코드를 적색으로 작성하지는 않습니다. 그러나 여기에있는 것이 표준 C++가 아니라 .NET Framework 용 C++ ish 코드 작성을 지원하는 Microsoft 확장 인 C++/CLI입니다. 태그를 추가하여 더 많은 잠재 고객에게 질문을 전달했습니다. –

답변

0

양식의 데이터를 다른 양식으로 전달하는 방법은 응용 프로그램의 계층 구조에 따라 다릅니다. 첫번째 클래스로부터 데이터를 받아서 두번째 폼으로 전송하는 두 폼을 인스턴스하는 "부모"클래스가 될 것입니다. 생성자 또는 접근 자 함수를 통해 수행 할 수 있지만 실제로 중요하지는 않습니다.

0

일반적으로 Forms (OOP)로 작업 할 때는 양식을 클래스로 생각해야합니다 (예 : 응용 프로그램 작업을 돕기 위해 만드는 양식). 따라서 Forms를 통해 데이터를 전달해야하는이 특별한 경우에는 대개 속성을 만듭니다. 어디에 넣고 사용할지 결정해야합니다.

예를 들어 : 기본 폼에 있고 기본 폼에 사용자 이름과 암호를 반환하도록 로그인 폼을 호출하면 로그인 양식에 Username 및 Passwords라는 두 속성을 만듭니다. 문자열, 어쩌면). 또한 공개 문자열을 만들어 외부 (Main form)에서 직접 액세스 할 수 있지만 OOP 관점에서는 Username 및 Password를 Login 클래스의 2 데이터 속성으로 생각할 수 있으므로 좋지 않습니다.

마지막으로 공개 속성을 사용하면 로그인 양식에서이 정보를 쉽게 검색 할 수 있습니다. 나는 그것이 당신의 필요에 적용되어야한다고 생각합니다.

public ref class Login { 
    private string username, password; 
    public string Username { get { return username; } } 
    public string Password { get { return password; } } 

    public void Show { 
     //Do your stuff and then user clicks OK 
     username = ...//soemthing like Textbox1.Text 
     password = ...//same here 
    } 
} 
public ref class Main{ 

    public void method { 
     Login frmLogin = new Login(); 
     frmLogin.Show(); 
     MessageBox.Show("User: " + frmLogin.Username + " and Password: " + frmLogin.Password); 
    } 
} 

내가 테스트하거나 (양식 및 ShowDialog를() 물건의 상속 클래스) 작동하지만 그게 아주 기본적인 생각이다 뭔가를 작성하지 않은, 당신은 그것을 얻을해야합니다.