0

現在、あるフォームから別のフォームにデータを渡す必要がある C++ プロジェクトに取り組んでいます。この場合、ユーザーは CreateAccount Form ( Create_Account.h) でユーザー名とパスワードを使用してアカウントを作成します。このユーザー名とパスワードは、オブジェクトのプライベート属性として保存され ( と呼びuserましょう)、ログイン フォーム ( ) に進みますLogin.h。ただし、このフォームに入力されたパスワードを以前のフォームと照合するシステムを取得できません。userobject( ) のプライベート属性で入力されたユーザー名とパスワードを確認できるように、object( ) をこのログイン フォームに渡す必要があることがわかりました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;
4

2 に答える 2

0

通常、フォーム (OOP) を扱うときは、フォームをクラス (アプリケーション タスクを支援するために作成するクラスなど) と考える必要があります。したがって、フォームを介してデータを渡す必要があるこの特定のケースでは、通常、プロパティを作成します。どこに置くか、どのように使用するかを決めるだけです。

例: メイン フォームでログイン フォームを呼び出し、ユーザー名とパスワードをメイン フォームに返す必要がある場合、ログイン フォームに Username と Passwords という 2 つのプロパティを作成します (おそらく文字列として)。 . パブリック文字列を作成して外部から (メイン フォームから) 直接アクセスできるようにすることもできますが、これは OOP の観点からはあまり良くありません。ユーザー名とパスワードは 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);
    }
}

私は動作するもの (Forms および ShowDialog() から継承されたクラス) をテストまたは作成しませんでしたが、それは非常に基本的な考え方です。それを理解する必要があります。

于 2013-02-27T10:20:06.280 に答える
0

あるフォームから別のフォームにデータを渡す方法は、アプリケーションの階層によって異なります。最初のフォームからデータを受け取り、2 番目のフォームに送信するフォームの両方をインスタンス化するのは、「親」クラスです。コンストラクターまたはアクセサー関数を介して実行できますが、実際には問題ではありません。

于 2013-02-27T08:12:42.417 に答える