Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Getting returns from a messagebox

Getting returns from a messagebox

Scheduled Pinned Locked Moved C#
question
13 Posts 7 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • B Offline
    B Offline
    Blekk
    wrote on last edited by
    #1

    Ok, in my text editor, when a user clicks new when the document hasn't been saved, it prompts the message: MessageBox.Show("This document has not been saved, would you like to save this document?", "Save", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1); But how do I now get returns from this function, so I can use an if statement such as: if(WHATERVER GOES HERE == OK){Messagebox.Show("User clicked OK"); Thanks, Any reply is appreciated.

    D B 2 Replies Last reply
    0
    • B Blekk

      Ok, in my text editor, when a user clicks new when the document hasn't been saved, it prompts the message: MessageBox.Show("This document has not been saved, would you like to save this document?", "Save", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1); But how do I now get returns from this function, so I can use an if statement such as: if(WHATERVER GOES HERE == OK){Messagebox.Show("User clicked OK"); Thanks, Any reply is appreciated.

      D Offline
      D Offline
      Dan Neely
      wrote on last edited by
      #2

      MEssageBox.Show returns a DialogResult value. That's the return value you want.

      -- Rules of thumb should not be taken for the whole hand.

      B 1 Reply Last reply
      0
      • D Dan Neely

        MEssageBox.Show returns a DialogResult value. That's the return value you want.

        -- Rules of thumb should not be taken for the whole hand.

        B Offline
        B Offline
        Blekk
        wrote on last edited by
        #3

        So if I used: if(MessageBox.Show == DialogResult.OK) { MessageBox.Show("User selected OK"); } that should work? Hmm, how does the compiler know that MessageBox.Show in the if statement is the same as the one with the buttons? Do I have to do something like: MessageBox myBox = new MessageBox(); myBox.Show(ALL THE CODE HERE); if(myBox == DialogResult.OK) { CODE HERE } Should that work?

        P J J S 4 Replies Last reply
        0
        • B Blekk

          So if I used: if(MessageBox.Show == DialogResult.OK) { MessageBox.Show("User selected OK"); } that should work? Hmm, how does the compiler know that MessageBox.Show in the if statement is the same as the one with the buttons? Do I have to do something like: MessageBox myBox = new MessageBox(); myBox.Show(ALL THE CODE HERE); if(myBox == DialogResult.OK) { CODE HERE } Should that work?

          P Offline
          P Offline
          Pradeep C
          wrote on last edited by
          #4

          You can do something like this. if (MessageBox.Show("Press Ok or Cancel", "Test", MessageBoxButtons.OKCancel) == DialogResult.OK) { MessageBox.Show("You pressed Ok"); } else { MessageBox.Show("You pressed Cancel"); } You cannot use new on MessageBox because its a static class (all functions are static and the constructor is private). Also since the MessageBox.Show function does not return unless the user press a button, there is no confusion for the compiler.

          --- "Drawing on my superior command of language I said nothing."

          B 1 Reply Last reply
          0
          • B Blekk

            So if I used: if(MessageBox.Show == DialogResult.OK) { MessageBox.Show("User selected OK"); } that should work? Hmm, how does the compiler know that MessageBox.Show in the if statement is the same as the one with the buttons? Do I have to do something like: MessageBox myBox = new MessageBox(); myBox.Show(ALL THE CODE HERE); if(myBox == DialogResult.OK) { CODE HERE } Should that work?

            J Offline
            J Offline
            Judah Gabriel Himango
            wrote on last edited by
            #5

            DialogResult result = MessageBox.Show("do something?", "title here", MessageBoxButtons.YesNo);
            if(result == DialogResult.Yes)
            {
            // He clicked yes.
            }
            else
            {
            // he clicked no
            }

            Tech, life, family, faith: Give me a visit. I'm currently blogging about: God-as-Judge, God-as-Forgiver The apostle Paul, modernly speaking: Epistles of Paul Judah Himango

            B 1 Reply Last reply
            0
            • B Blekk

              So if I used: if(MessageBox.Show == DialogResult.OK) { MessageBox.Show("User selected OK"); } that should work? Hmm, how does the compiler know that MessageBox.Show in the if statement is the same as the one with the buttons? Do I have to do something like: MessageBox myBox = new MessageBox(); myBox.Show(ALL THE CODE HERE); if(myBox == DialogResult.OK) { CODE HERE } Should that work?

              J Offline
              J Offline
              John Arlen1
              wrote on last edited by
              #6

              When all else fails, study the documentation. http://msdn2.microsoft.com/en-us/library/0x49kd7z.aspx[^] Their sample code answers your question.

              1 Reply Last reply
              0
              • P Pradeep C

                You can do something like this. if (MessageBox.Show("Press Ok or Cancel", "Test", MessageBoxButtons.OKCancel) == DialogResult.OK) { MessageBox.Show("You pressed Ok"); } else { MessageBox.Show("You pressed Cancel"); } You cannot use new on MessageBox because its a static class (all functions are static and the constructor is private). Also since the MessageBox.Show function does not return unless the user press a button, there is no confusion for the compiler.

                --- "Drawing on my superior command of language I said nothing."

                B Offline
                B Offline
                Blekk
                wrote on last edited by
                #7

                Thanks, I'll try that after I finish my ICT Essay :P And yeah, I realised you cannot use MessageBox as a normal class, using new etc. Out of interest, why is this the case?

                P L 2 Replies Last reply
                0
                • J Judah Gabriel Himango

                  DialogResult result = MessageBox.Show("do something?", "title here", MessageBoxButtons.YesNo);
                  if(result == DialogResult.Yes)
                  {
                  // He clicked yes.
                  }
                  else
                  {
                  // he clicked no
                  }

                  Tech, life, family, faith: Give me a visit. I'm currently blogging about: God-as-Judge, God-as-Forgiver The apostle Paul, modernly speaking: Epistles of Paul Judah Himango

                  B Offline
                  B Offline
                  Blekk
                  wrote on last edited by
                  #8

                  Thankyou also, I will use both methods and see which I find the easiest to understand etc. Thanks for all the help.

                  1 Reply Last reply
                  0
                  • B Blekk

                    So if I used: if(MessageBox.Show == DialogResult.OK) { MessageBox.Show("User selected OK"); } that should work? Hmm, how does the compiler know that MessageBox.Show in the if statement is the same as the one with the buttons? Do I have to do something like: MessageBox myBox = new MessageBox(); myBox.Show(ALL THE CODE HERE); if(myBox == DialogResult.OK) { CODE HERE } Should that work?

                    S Offline
                    S Offline
                    shopi30
                    wrote on last edited by
                    #9

                    Hi Blekk. Exists two way for using. 1) if( MessageBox.Show(Handle, msg, caption, MessageBoxButtons.RetryCancel, MessageBoxIcon.Question) == DialogResult.Retry) {     //Do something... } 2) DialogResult p = MessageBox.Show(Handle, msg, caption, MessageBoxButtons.RetryCancel, MessageBoxIcon.Question); if( p == DialogResult.Retry ) {     //Do something... }

                    SINCERELY. ANTHONY ACUÑA PREFERED PHRASE: SOMEBODY TELL ME WHY IS MORE REAL WHEN I DREAM THAT I AM WAKE?

                    1 Reply Last reply
                    0
                    • B Blekk

                      Thanks, I'll try that after I finish my ICT Essay :P And yeah, I realised you cannot use MessageBox as a normal class, using new etc. Out of interest, why is this the case?

                      P Offline
                      P Offline
                      Pradeep C
                      wrote on last edited by
                      #10

                      The way I think about it is like this: We create normal classes (with constructor) because we might want to have multiple instances of that class at the same time. There is no need to create multiple instances of the MessageBox class because the only thing you use a MessageBox is to show it to the user and wait for a input. The MessageBox is a modal dialog and hence the application will have to sit and wait till user clicks a button. Once the user clicks the button the only information you will need is which button the user clicked (if there is more than one button). So the entire purpose for which MessageBox class exists is finished when we call the Show method and check its return value. Hence the natural way is to make it a static class so that its very easy for use (means write less code). User never needs to create an instance.

                      --- "Drawing on my superior command of language I said nothing."

                      1 Reply Last reply
                      0
                      • B Blekk

                        Ok, in my text editor, when a user clicks new when the document hasn't been saved, it prompts the message: MessageBox.Show("This document has not been saved, would you like to save this document?", "Save", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1); But how do I now get returns from this function, so I can use an if statement such as: if(WHATERVER GOES HERE == OK){Messagebox.Show("User clicked OK"); Thanks, Any reply is appreciated.

                        B Offline
                        B Offline
                        Blekk
                        wrote on last edited by
                        #11

                        Thanks all for your help, I've never known anybody respond so quickly on any forum ever, with so much useful information too. Thanks all for your help.

                        1 Reply Last reply
                        0
                        • B Blekk

                          Thanks, I'll try that after I finish my ICT Essay :P And yeah, I realised you cannot use MessageBox as a normal class, using new etc. Out of interest, why is this the case?

                          L Offline
                          L Offline
                          Lost User
                          wrote on last edited by
                          #12

                          Blekk wrote:

                          And yeah, I realised you cannot use MessageBox as a normal class, using new etc.

                          That's because MessageBox a) doesn't have any constructors b) the Show method is declared static, which means that you don't need an instance of the class and call it directly like Class.Method hope this helps, regards

                          B 1 Reply Last reply
                          0
                          • L Lost User

                            Blekk wrote:

                            And yeah, I realised you cannot use MessageBox as a normal class, using new etc.

                            That's because MessageBox a) doesn't have any constructors b) the Show method is declared static, which means that you don't need an instance of the class and call it directly like Class.Method hope this helps, regards

                            B Offline
                            B Offline
                            Blekk
                            wrote on last edited by
                            #13

                            Thanks Pradeep C and Greeeg, I now understand why you don't have to create an instance and you have both also made the whole concept of the C# language a little more clear. There are still many things I don't really know at this stage, but probably should know, meh, I would rather find it out this way than reading through endless chapters of a book and then forgetting what it was at the end of the book. :laugh: Thanks.

                            1 Reply Last reply
                            0
                            Reply
                            • Reply as topic
                            Log in to reply
                            • Oldest to Newest
                            • Newest to Oldest
                            • Most Votes


                            • Login

                            • Don't have an account? Register

                            • Login or register to search.
                            • First post
                              Last post
                            0
                            • Categories
                            • Recent
                            • Tags
                            • Popular
                            • World
                            • Users
                            • Groups