Thread: Yesno

  1. #1
    Registered User
    Join Date
    Jan 2005
    Posts
    183

    Yesno

    Hi all.

    First off, I'm a total newbie to windows programming, and have a few questions.

    1. I have a YES/NO messagebox, and I want to know what code is required to determine which button the user pressed. I tried this in a while loop :


    Code:
    MessageBox(NULL, " Some random text ", "   Random text", MB_YESNO | MB_ICONEXCLAMATION);
    
    if ( IDYES )
    {
        cout<<"Blah Blah Blah.";
        MessageBox(NULL, " Some random text ", "   Random text", MB_YESNO | MB_ICONEXCLAMATION);
    
    }
    But which ever button the user pressed, the message pops up again. Is this the correct syntax ? I can post the rest of the loop if you want.

    2. Forgot what 2 was, but It'll come back to me.

    Please help !
    I'm confused.

  2. #2
    Registered User
    Join Date
    Dec 2003
    Posts
    167
    You have to save the return value of the MessageBox function.

    Code:
     
    retval = MessageBox(...); 
    
    if( retval == IDYES)
    {
    	...
    }
    silk.odyssey

  3. #3
    int x = *((int *) NULL); Cactus_Hugger's Avatar
    Join Date
    Jul 2003
    Location
    Banks of the River Styx
    Posts
    902
    if(IDYES) is like saying if(0) or if(1) - you'll always get the same answer, and the stuff inside the if() will either always or never execute. (A good compiler, with some warnings turned on, will note this. I believe gcc does it with -Wall)

    What you need to do is store the result of MessageBox() somewhere, in a variable. Then see if that variable is equal to IDYES. Example:
    Code:
    int mb_result = 0;
    
    mb_result = MessageBox(HWND_DESKTOP, "Text", "Text", MB_YESNO);
    if(mb_result == IDYES)
    {
      // User clicked Yes.
    }
    else
    {
      // User clicked no.
    }
    long time; /* know C? */
    Unprecedented performance: Nothing ever ran this slow before.
    Any sufficiently advanced bug is indistinguishable from a feature.
    Real Programmers confuse Halloween and Christmas, because dec 25 == oct 31.
    The best way to accelerate an IBM is at 9.8 m/s/s.
    recursion (re - cur' - zhun) n. 1. (see recursion)

  4. #4
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    Or, to hide temp variables:
    Code:
    switch(MessageBox(...))
    {
      case IDYES:
      {
        break;
      }
    
      case IDNO:
      {
        break;
      }
    
      case IDCANCEL:
      {
        break;
      }
    }
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  5. #5
    Registered User
    Join Date
    Jan 2005
    Posts
    847
    or simply
    Code:
    if (MessageBox(...)==IDYES)
    {
       ...
    }
    else
    {
       ...
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Display Lists (OpenGL)
    By Shamino in forum Game Programming
    Replies: 11
    Last Post: 06-05-2005, 12:11 PM
  2. Case: Problem...
    By Jst in forum C Programming
    Replies: 7
    Last Post: 04-24-2005, 07:07 AM
  3. Can somebody help me with this (easy)
    By Kurupt in forum C Programming
    Replies: 3
    Last Post: 01-16-2004, 04:54 AM
  4. Easy Question
    By Bryan in forum C++ Programming
    Replies: 15
    Last Post: 10-17-2002, 04:25 PM
  5. problem with output
    By Garfield in forum C Programming
    Replies: 2
    Last Post: 11-18-2001, 08:34 PM