Visual Studio 2005
Windows Forms Application
GUI


Form1:entering name, then log-in
Form2: main interface

I want to show the log-in name on Form2 at a certain place. So every time a different person uses the program, it shows the log-in name on the interface.

How to pass the input of a textbox in Form1 onto Form2?

Dani AI

Generated

Three lightweight, practical options for a VS2005 WinForms C++ project (modal login is simplest). already outlined the “modal dialog + expose the textbox” approach and ’s note about dialog classes is relevant only if this is native MFC rather than WinForms. The patterns below avoid globals and keep responsibilities clear.

Constructor injection (clean, keeps Form2 immutable at creation)

// Form2 constructor (Form2.h)
public ref class Form2 : public System::Windows::Forms::Form {
public:
    Form2(System::String^ loginName) {
        InitializeComponent();
        labelLoggedInAs->Text = loginName;
    }
};

Then, after the login dialog returns a positive result, pass the string into Form2’s constructor and run the main form.

Event/callback (best for modeless login or when login can happen while main UI exists)

// In LoginForm.h
public:
    delegate void LoginEventHandler(System::String^ user);
    event LoginEventHandler^ LoginSucceeded;

void btnOk_Click(Object^, EventArgs^) {
    if (Authenticate()) {
        if (LoginSucceeded != nullptr) LoginSucceeded(textBoxUser->Text);
        this->Close();
    }
}

// In Form2 where the login form is shown
login->LoginSucceeded += gcnew LoginForm::LoginEventHandler(this, &Form2::OnLogin);

OnLogin simply updates the UI (remember to Invoke if login runs on a background thread).

Quick practical notes and troubleshooting

  • Make the login button set DialogResult = OK (or set it in code) so ShowDialog() returns predictably.
  • Copy the username into a local variable immediately after ShowDialog returns (some patterns dispose the dialog in closing handlers).
  • Use String::IsNullOrEmpty or Trim() (VS2005 uses IsNullOrEmpty) to validate input.
  • Avoid storing passwords in plain Strings; treat credentials securely.
  • If controls aren’t updating, confirm code runs on the UI thread and use Control::Invoke when needed.

Recommended Answers

All 2 Replies

Assuming that your login form is a modal dialog, you can associate
- a class to that dialog (if I remember correctly, this is done when you add the dialog)
- member variables of this clas to the edit boxes in that dialog.
When user has entered the data and clicks Ok, in OnOk() you can use DDX to (think the function name was UpdateData() ) get the values from edit boxes to associated variables.
Also I assume that to show the dialog box you use loginDialogObj.doModal() from your main form. So you already have the values (loginDialogObj.userName, loginDialogObj.xxx).

> How to pass the input of a textbox in Form1 onto Form2?
I guess it depends on how you open the forms. If Form2 is the main form and controls Form1, you can use a property to get the login name:

// Put this in Form1
property String^ LoginName {
  String^ get() { return textBoxName->Text; }
}

// This says whether the login failed or succeeded
property bool Authenticated {
  bool get() { return _isAuthenticated; }
}

Edward usually does something like this in the Form2 load event:

void Form2_Load(Object^ sender, EventArgs^ e)
{
  Form1^ login = gcnew Form1();

  if (login->ShowDialog() != ::DialogResult::OK
    || !login->Authenticated) {

    Application->Exit();
  }
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.