0

我目前正在从事一个 C++ 项目,该项目需要我将数据从一种形式传递到另一种形式。Create_Account.h在这种情况下,用户将使用用户名和密码在 CreateAccount Form ( ) 上创建一个帐户。这个用户名和密码将被存储为一个对象的私有属性(让我们称之为user),他将进入一个登录表单(Login.h)。但是,我无法让系统将在此表单上输入的密码与之前的密码进行验证。我已经知道我必须将 object( user) 传递给这个登录表单,以便我可以使用 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

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

于 2013-02-27T08:12:42.417 回答
0

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

例如:如果我在主窗体中并且将调用登录窗体,因此它必须将用户名和密码返回到主窗体,那么我将在登录窗体中创建两个属性,称为用户名和密码(可能是字符串) . 您还可以创建公共字符串,以便您可以直接从外部(从主窗体)访问,但从 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() 的继承类),但这是非常基本的想法,你应该明白。

于 2013-02-27T10:20:06.280 回答