Okay, so almost the exact same code works in one of my c++/cli projects, but not the other. Both have the variables set as public, so they should be free to change. But only the first one does, and the second one does nothing.
Here's the button click code for the first.

1:

private: System::Void helium_Click(System::Object^  sender, System::EventArgs^  e) {
			   Form2^ form = gcnew Form2();
            form->Show();
			form->atomname->Text = "Helium";
			form->comionic->Text = "N/A";
			form->molmass->Text = "4.00 G/Mol";
			form->atomicnumber->Text = "2";
			form->color->Text = "Colorless";
			form->diatomic->Text = "Yes";
			form->pronounce->Text = "HEE-lee-am";
			form->mpoint->Text = "-272.20 °C";
			form->bpoint->Text = "-268.93 °C";
			form->tpoint->Text = "No Triple Point";
			form->state->Text = "Gas";
			form->adinfo->Text = "Helium naturally occurs as He2. It is a colorless, odorless gas. Helium has no"
				" common compounds, as any compounds made with Helium are extremely unstable at room temperature."
				" Uses include coolant in superconducting magnets, and since it is lighter than air, it is used"
				" to lift airships and balloons, it is also a vital component in rocket fuel.";
				
		 }

And the one that doesn't work.


2:

Info^ form = gcnew Info();
form->ShowDialog();
form->name->Text = "Calculator";
form->infobox->Text = "Test";

It just doesn't change the text at all. Any help? I can't see what I did wrong.

Dani AI

Generated

Short answer: the dialog instance is being shown before you set its fields. As noted, set values on the form instance before displaying a modal dialog — or initialize the form with the data so there’s no risk of updating the wrong object or doing it after the dialog blocks.

A couple of safer patterns that avoid the “set after ShowDialog” pitfall:

Constructor-initializer (put the data into the form at construction time)

public ref class Info : public System::Windows::Forms::Form {
public:
    Info(System::String^ title, System::String^ text) {
        InitializeComponent();
        this->name->Text = title;
        this->infobox->Text = text;
    }
};

Call with:

auto dlg = gcnew Info("Calculator", "Test");
dlg->ShowDialog();
delete dlg;    // calls Dispose

Property-wrapper pattern (keeps fields encapsulated rather than exposing controls)

public:
    property System::String^ DialogTitle {
        void set(System::String^ v) { name->Text = v; }
    }

Use it like:

auto dlg = gcnew Info();
dlg->DialogTitle = "Calculator";
dlg->DialogText  = "Test";
dlg->ShowDialog();
delete dlg;

Quick troubleshooting checklist

  • Verify you aren’t creating two different Info instances (one shown, one modified).
  • Ensure controls are accessible (better: expose properties instead of public controls).
  • Use the debugger to inspect the form instance before ShowDialog to confirm the values were set.
  • Dispose/delete the form after use to avoid leaks.

These approaches keep UI initialization deterministic and avoid timing/order bugs that often confuse newcomers.

Recommended Answers

All 2 Replies

Your operations are out of order. Create a dialog object, initialize it, then show it:

Info^ form = nullptr;

try {
    form = gcnew Info();

    form->name->Text = "Calculator";
    form->infobox->Text = "Test";

    form->ShowDialog();
} finally {
    if (form != nullptr)
        form.Dispose();
}

This process makes the most sense from a code perspective, but it's especially important with ShowDialog(). From MSDN's page on Form.ShowDialog():

When this method is called, the code following it is not executed until after the dialog box is closed.

Your operations are out of order. Create a dialog object, initialize it, then show it:

Info^ form = nullptr;

try {
    form = gcnew Info();

    form->name->Text = "Calculator";
    form->infobox->Text = "Test";

    form->ShowDialog();
} finally {
    if (form != nullptr)
        form.Dispose();
}

This process makes the most sense from a code perspective, but it's especially important with ShowDialog(). From MSDN's page on Form.ShowDialog():

Thanks Narue. Didn't quite pick up on this. But I completely understand what you mean.

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.