Public Group

#### Archived

This topic is now archived and is closed to further replies.

# Die MessageBox!

This topic is 5231 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I know how to call up a message box, it''s easy. But how do you code them to close when a user hits one of their buttons, for example a message box pops up saying "Howdy" and displaying the buttons "ok" and "cancle". How do you make the message box disappear when they hit the buttons? Actual examples would be apprieciatted.

##### Share on other sites
If you are talking about a system messagebox like this one:
MessageBox(hwnd,"Push the yes button","Test",MB_YESNO);

Then it should terminate when you click on either button, yes or no.

If you are creating a messagebox yourself then you will need to call the function "EndDialog(hwnd,0);" with hwnd as the handle to your dialogbox after the appropriate case statement.

##### Share on other sites
So the entire sequence of code including the creation of the message box and the message handling structure would look like this, then?:

int Result MessageBox(
hWnd, "Push the yes button" , "Test", MB_YESNO);

switch(Result)
{
case IDYES: EndDialog(hWnd, 0);

else {

//do something else

}

}

The one thing that I think I might have wrong is what goes in the parentheses after the "switch" statement, if that''s wrong let me know.

Many thanks.

##### Share on other sites
Since you are calling the system MessageBox you would not need the end dialog function since it is built in. The return value is of course the button but the dialog should terminate before the result is given.

The switch statement you''ve given has a few problems with it, first being there is no break after the case statement and I don''t think "else" will work in a switch statment. For else use default instead.

Try running this:
	int result=MessageBox(hwnd,"Click either","Test",MB_YESNO);	switch(result)	{	      case IDYES:	      {	           MessageBox(hwnd,"You''ve click yes","Result",MB_OK);	      }	      break;	      case IDNO:	      {	           MessageBox(hwnd,"You''ve click no","Result",MB_OK);	      }	      break;	      default:	      {	           MessageBox(hwnd,"I''m not suppose to show","Result",MB_OK);	      }	}

##### Share on other sites
I appreciatte the help. I have succesfully compiled your example and run it. I have also gotten nested dialog boxes to work meaning you get even more MessageBoxes depending on what you buttons you choose from the preceding Message Box. Thanks again.

• 17
• 10
• 19
• 14
• 19