I have a running calculator on VC++ and don't know how to get an exit button to work. (The X on top right works fine, but want an exit button) I have tried OnOK(); this->CloseWindow(); And other things. Still not managing to close the program from the button. Any tutorials or advice you all can give me?

Recommended Answers

All 17 Replies

Is this an MFC program, or what? In Windows Forms CloseWindow() only minimizes the window. You want DestroyWindow().

Yes. It is, my bad.

Maybe you are calling CloseWindow() from the wrong window???

CDialog::CloseWindow();

Calculator_Dialog C;
C.CloseWindow();

Among the things I've tried. Only have that one dialog.

I still don't understand, can you explain?

In your button control OnClick() handler just call OnOk()

I figured it out somehow, Any chance you can tell me how to group the items in a group box so I can disable them from another button.

You can either do it in the forms designer (drag the groupbox control over and place the other items inside of it), or you can do it programmatically

instantiate a new groupbox
instantiate a new control of whatever type
add it to the groupbox
instantiate a new control of whatever type
add it to the groupbox
add the groupbox to the 'Controls' object of the main form.

Try that out and post back if you can't get it to work (or with any other issues).

How would I reference them as one group? ID?

Assuming you added 2 textBoxes to a groupbox programmatically and that those were the only 2 controls there

groupBox2 = gcnew GroupBox();
groupBox2->Controls->Add(textBox3);
groupBox2->Controls->Add(textBox4);
this->Controls->Add(groupBox2);
for each (Object^ ctrl in groupBox2->Controls)
{
    MessageBox::Show((static_cast<TextBox^>(ctrl))->Text);
}

(if you had other controls in it, you'd have to know what order they were in, etc).

They can also be addressed as groupBox2->Controls[0] and groupBox2->Controls[1] Since there's no is/as in C++/CLI like there is in C#, I've found that using the Tag property of the control comes in handy. You can assign a string to it (or any object really) and sort your controls based on that.

private: System::Void button2_Click(System::Object^  sender, System::EventArgs^  e) {
         Application::Exit();  }

How did this topic turn from MFC to Windows Forms? Which one is it anyway?

How did this topic turn from MFC to Windows Forms?

That's on me. I misinterpreted something you said.

I have tried OnOK(); this->CloseWindow()

In Windows Forms CloseWindow()...

as meaning that he was doing Winforms.

Apologies.

At the time I wrote that I didn't know which one it was.

At the time I wrote that I didn't know which one it was.

Yes, I know. I was just giving you the stack trace of my brain fart. :) I'm again sorry for steering things the wrong direction... (means I have to bow out, too, as I know next to nothing about MFC)

void CMySecondAppDlg::OnBnClickedButton2()
{
    SetBackgroundColor(022222);
    this->DestroyWindow();
    // TODO: Add your control notification handler code here
}
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.