传递类/对象/数据?两种形式之间

时间:2013-02-27 07:31:57

标签: c++ .net c++-cli

我目前正在开发一个C ++项目,它要求我将数据从一个表单传递到另一个表单。在这种情况下,用户将使用用户名和密码在CreateAccount表单(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;

2 个答案:

答案 0 :(得分:0)

如何将数据从一个表单传递到另一个表单取决于应用程序的层次结构。它将是“父”类,它实例化两个表单,从第一个表单接收数据并将其发送到第二个表单。它可以通过构造函数或访问器函数来完成,它并不重要。

答案 1 :(得分:0)

通常在使用Forms(OOP)时,您应该将表单视为类(就像您创建的用于帮助应用程序任务的类)。因此,在这种特殊情况下,当我必须通过Forms传递数据时,我通常会创建属性。您应该只决定放置它的位置以及如何使用它。

例如:如果我在Main表单中并且将调用Login表单以便它必须将用户名和密码返回到Main表单,那么我将在Login表单中创建两个名为Username和Passwords的属性(作为字符串, 也许)。您也可以创建公共字符串,这样您就可以直接从外部访问(从Main表单),但从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);
    }
}

我没有测试或编写有效的东西(使用Forms和ShowDialog()中的继承类)但这是非常基本的想法,你应该得到它。