传递类/对象/数据?在两种形式之间 我目前正在研究一个C++项目,它要求我把数据从一个表单传递给另一个表单。在这种情况下,用户将使用用户名和密码在CreateCount表单(create_account.h)上创建一个帐户。此用户名和密码将存储为对象的私有属性(称之为user),他将进入登录表单(Login.h)。但是,我无法让系统将此表单上输入的密码与之前的密码进行验证。我想我必须将对象(user)传递到此登录表单,以便验证使用对象的私有属性(user)输入的用户名和密码。但是,我不太清楚如何将对象传递到表单的语法。(我只熟悉通过函数传递对象)

传递类/对象/数据?在两种形式之间 我目前正在研究一个C++项目,它要求我把数据从一个表单传递给另一个表单。在这种情况下,用户将使用用户名和密码在CreateCount表单(create_account.h)上创建一个帐户。此用户名和密码将存储为对象的私有属性(称之为user),他将进入登录表单(Login.h)。但是,我无法让系统将此表单上输入的密码与之前的密码进行验证。我想我必须将对象(user)传递到此登录表单,以便验证使用对象的私有属性(user)输入的用户名和密码。但是,我不太清楚如何将对象传递到表单的语法。(我只熟悉通过函数传递对象),c++,.net,c++-cli,C++,.net,C++ Cli,我对这门语言不熟悉,非常感谢您的帮助 下面是我写的代码 **//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 {

我对这门语言不熟悉,非常感谢您的帮助

下面是我写的代码

**//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;
**//CreateAccount.h**
#布拉格语一次
#包括“user2.h”
#包括
#包括“ExpenseTracker.h”
#包括“User\u Login.h”
#包括
#包括
名称空间EzXpns2{
使用名称空间系统;
使用名称空间System::ComponentModel;
使用名称空间系统::集合;
使用命名空间System::Windows::Forms;
使用名称空间System::Data;
使用名称空间系统::绘图;
/// 
///创建帐户的摘要
/// 
public ref类Create_帐户:public System::Windows::Forms::Form
{
公众:
创建账户(作废)
{
初始化组件();
//
//TODO:在此处添加构造函数代码
//
}
受保护的:
/// 
///清理所有正在使用的资源。
/// 
~Create_Account()
{
if(组件)
{
删除组件;
}
}
私有:系统::Windows::窗体::按钮^BTN创建新帐户;
受保护的:
受保护的:
private:System::Windows::Forms::TextBox^txtBxPassword;
私有:系统::Windows::窗体::标签^lblPassword;
私有:系统::Windows::窗体::文本框^txtBxUsername;
私有:系统::Windows::窗体::标签^lblUsername;
私有:系统::Windows::窗体::文本框^TXTBX重新输入密码;
私有:系统::Windows::窗体::标签^label1;
//private:System::Windows::Forms:User\u Login^userlogin;
//私人用户登录^UserLogin;
私人:
/// 
///必需的设计器变量。
/// 
系统::组件模型::容器^组件;
#pragma区域Windows窗体设计器生成的代码
/// 
///设计器支持所需的方法-不修改
///此方法的内容与代码编辑器一起使用。
/// 
void初始化组件(void)
{
此->btnCreateNewAccount=(gcnew System::Windows::Forms::Button());
这->txtBxPassword=(gcnewsystem::Windows::Forms::TextBox());
这->lblPassword=(gcnewsystem::Windows::Forms::Label());
这->txtBxUsername=(gcnew System::Windows::Forms::TextBox());
这->lblUsername=(gcnewsystem::Windows::Forms::Label());
这->txtBxReenterPassword=(gcnew System::Windows::Forms::TextBox());
这->标签1=(gcnewsystem::Windows::Forms::Label());
此->SuspendLayout();
// 
//BTN创建新帐户
// 
此->btnCreateNewAccount->位置=系统::绘图::点(147147);
此->btnCreateNewAccount->Name=L“btnCreateNewAccount”;
这->btnCreateNewAccount->大小=系统::绘图::大小(126,23);
此->btnCreateNewAccount->TabIndex=3;
此->btnCreateNewAccount->Text=L“创建新帐户”;
此->btnCreateNewAccount->UseVisualStyleBackColor=true;
this->btnCreateNewAccount->Click+=gcnewsystem::EventHandler(this,&Create\u Account::btnCreateNewAccount\u Click);
// 
//txtBxPassword
// 
这个->txtBxPassword->位置=系统::绘图::点(147,68);
此->txtBxPassword->Name=L“txtBxPassword”;
这->txtBxPassword->尺寸=系统::图纸::尺寸(126,20);
此->txtBxPassword->TabIndex=1;
此->txtBxPassword->UseSystemPasswordChar=true;
// 
//lblPassword
// 
此->lblPassword->自动调整大小=true;
这个->lblPassword->位置=系统::绘图::点(26,73);
此->lblPassword->Name=L“lblPassword”;
这->lblPassword->尺寸=系统::图纸::尺寸(56,13);
此->lblPassword->TabIndex=12;
此->lblPassword->Text=L“密码:”;
// 
//txtBxUsername
// 
此->txtBxUsername->位置=系统::绘图::点(149,27);
此->txtBxUsername->Name=L“txtBxUsername”;
此->txtBxUsername->尺寸=系统::图纸::尺寸(126,20);
此->txtBxUsername->TabIndex=0;
// 
//lblUsername
// 
此->lblUsername->自动调整大小=true;
这->lblUsername->位置=系统::绘图::点(24,29);
此->lblUsername->Name=L“lblUsername”;
这->lblUsername->大小=系统::绘图::大小(86,13);
这->lblUsername->TabIndex=10;
此->lblUsername->Text=L“输入用户名:”;
// 
//TXTBX重新输入密码
// 
此->TXTBX重新输入密码->位置=系统::绘图::点(147100);
此->txtBxReenterPassword->Name=L“txtBxReenterPassword”;
此->TXTBX重新输入密码->大小=系统::绘图::大小(126,20);
此->txtBxReenterPassword->TabIndex=2;
此->TXTBX重新输入密码->使用系统密码C
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);
    }
}