I'm doing an assignment for class and we're suppose to write a program that pops up a window with YES/NO/CANCEL buttons and when the user pushes a button another window pops up displaying what button they clicked.

I wrote up the code and it does work, but it takes several clicks of the button for it to finally display the 2nd box. Any reason for this??

#include <windows.h>

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPreInstance, LPSTR lpCmdLine, int nSHowCmd)
{
	MessageBox(NULL, "This is my dialog box", "Dialog Box",MB_YESNOCANCEL | MB_ICONEXCLAMATION);
	if (MessageBox(NULL, "This is my dialog box", "Dialog Box", MB_YESNOCANCEL | MB_ICONEXCLAMATION) == IDYES)
	{
		MessageBox(NULL, "User Clicked Yes", "Robert Week 1", MB_OK | MB_ICONEXCLAMATION);
	}
	if (MessageBox(NULL, "This is my dialog box", "Dialog Box", MB_YESNOCANCEL | MB_ICONEXCLAMATION) == IDNO)
	{
		MessageBox(NULL, "User Clicked No", "Robert  Week 1", MB_OK | MB_ICONQUESTION);
	}
}

Recommended Answers

All 2 Replies

The Below Code might Help just remember MessageBox Returns the Value just store that Value and then Process that response rather than calling the message box again and again.

int nResponse = MessageBox(NULL, TEXT("Hello, World"), TEXT("Say Yes No Cancel"), MB_YESNOCANCEL);

	if (nResponse == IDYES) {
		MessageBox(NULL, TEXT("Hey You Pressed Yes!"), TEXT("Wow! Got It"), MB_OK |MB_ICONEXCLAMATION);
	}
	/*else if(...) {
	}
	else if (...) {
	}*/

One more thing why if (....) and if (....) two times, if you notice your code, you'll find one more thing
you check the first condition then again you check for some other condition.
why is that so, logically and as per the requirement, you just need to show the form & store it response & then process that response rather than calling the MessageBox API again and again.


Hope this might help

The Below Code might Help just remember MessageBox Returns the Value just store that Value and then Process that response rather than calling the message box again and again.

int nResponse = MessageBox(NULL, TEXT("Hello, World"), TEXT("Say Yes No Cancel"), MB_YESNOCANCEL);

	if (nResponse == IDYES) {
		MessageBox(NULL, TEXT("Hey You Pressed Yes!"), TEXT("Wow! Got It"), MB_OK |MB_ICONEXCLAMATION);
	}
	/*else if(...) {
	}
	else if (...) {
	}*/

One more thing why if (....) and if (....) two times, if you notice your code, you'll find one more thing
you check the first condition then again you check for some other condition.
why is that so, logically and as per the requirement, you just need to show the form & store it response & then process that response rather than calling the MessageBox API again and again.


Hope this might help

Thanks a lot. That helped out perfectly. This is my first week using C++ in a win32 sense so I'm trying to get out of console thought and move on. Good thing is after my next assignment I get to start dealing with DirectX so that'll be interesting.

Thanks for yor help, much appreciated.

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.