none
DialogResult

    Question

  • I am using C# and am trying to prompt the user for a Yes/No answer.  This is what I've done:

    DialogResult dlgResult;
    
    dlgResult = MessageBox.Show("My Question", "My title", MessageBoxButton.YesNo);
    
    if (dlgResult == DialogResult.Yes)
    {
    }

    I get error "error CS0118: 'System.Windows.Window.DialogResult' is a 'property' but is used like a 'type'" with the first line, and it also complains about DialogResult.Yes.  Is there some other DialogResult class I am supposed to be using?
    Monday, March 05, 2012 8:27 PM

Answers

  • One final guess on my part: It sounds like you are missing the reference to System.Windows.Forms

    In the solution explorer, right click on your "References" folder->Add Reference...

    In the .Net tab search through the unholy amount of references to find "System.Windows.Forms"

    Select and hit OK.

    This should work whether or not you're working in a Form, Dll, Console App...Anything!


    • Edited by Idea Hat Monday, March 05, 2012 9:45 PM
    • Marked as answer by msnhelpsme Monday, March 05, 2012 10:24 PM
    Monday, March 05, 2012 9:44 PM
  • Hello msnhelpsme,

    This error is seen at compile time using Visual Studio 2008.

    make sure you have set above references the System.Windows.Forms Using statement within the class.

    Regards.


    • Marked as answer by msnhelpsme Monday, March 05, 2012 10:24 PM
    Monday, March 05, 2012 9:57 PM

All replies

  • Here is a sample, just modify as needed:

    DialogResult dr = new DialogResult();
                        dr = MessageBox.Show("Clicking 'Yes' commits this record to the database." + Environment.NewLine + "This will remove this record from your screen." + Environment.NewLine + "Have you confirmed all data to be correct?", "DataSync", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
                        if (dr == DialogResult.Yes)
                        {
                            SubmitDataToIBM();
                        }
                        else { return; }

    Monday, March 05, 2012 8:41 PM
  • When I use "new" with the constructor, I get this error (twice):

    "error CS0118: 'System.Windows.Window.DialogResult' is a 'property' but is used like a 'type'"

    am I not using the right library?  DialogResult didn't even come up as an option as I was typing.  The closest thing for autocomplete was DialogResultConverter().

    Monday, March 05, 2012 9:04 PM
  • if (System.Windows.Forms.DialogResult.Yes == MessageBox.Show("Do you wanna quit?", "Question", MessageBoxButtons.YesNo, MessageBoxIcon.Question))
    {
          //it quits
    }
    else
    {
         //it does not quit, it stays here
    }


    Mitja


    Monday, March 05, 2012 9:09 PM
  • You have a namespace fight on your hands! Use

    System.Windows.Forms.DialogResult

    Monday, March 05, 2012 9:09 PM
  • What class are you doing this in? DialogResult is apparently also the name of a property in your class hierarchy somewhere. You should change the name of that property to avoid conflicts.

    Check out My Blog. Now updated to actually work!

    Monday, March 05, 2012 9:10 PM
  • You have a namespace fight on your hands! Use

    System.Windows.Forms.DialogResult

    I now do this:

    System.Windows.Forms.DialogResult dlgResult = new System.Windows.Forms.DialogResult();

    and this is the error: "error CS0234: The type or namespace name 'Forms' does not exist in the namespace 'System.Windows' (are you missing an assembly reference?)"
    Monday, March 05, 2012 9:16 PM
  • Are you sure you are in Windows forms? If you are in Console, you can NOT access to this namespace (Windows, and whats rest from it)?

    Mitja

    Monday, March 05, 2012 9:18 PM
  • Hello msnhelpsme,

    dialog result should be enhanced immediately, and can not declare a variable, see example below.

            private void button3_Click(object sender, EventArgs e)
            {
                var dialogresult = MessageBox.Show("your text here",Application.ProductName.ToString(),MessageBoxButtons.YesNoCancel);
                if (dialogresult.Equals(DialogResult.Yes))
                {
                    //your code here
                    MessageBox.Show("you presset yes" );
                }    
            }

    Regards.

    Monday, March 05, 2012 9:18 PM
  • Are you sure you are in Windows forms? If you are in Console, you can NOT access to this namespace (Windows, and whats rest from it)?

    Mitja


    I don't know, how do I tell?  I'm not running from the command line, if that's what you are asking.
    Monday, March 05, 2012 9:19 PM
  • Is this project of yours a Windows Form?

    Mitja

    Monday, March 05, 2012 9:20 PM
  • Hello msnhelpsme,

    Are you sure you are in Windows forms? If you are in Console, you can NOT access to this namespace (Windows, and whats rest from it)?

    Mitja


    I don't know, how do I tell?  I'm not running from the command line, if that's what you are asking.

    when you created the project you choose an application type or WindowsForm Console?

    Regards.


    Monday, March 05, 2012 9:21 PM
  • Does it have a GUI that inherits the windows theme or does it look like you are in command prompt? If it looks like you are in command prompt when you compile, you are using a console app. Also, you could have selected other things like a class library in which case you would have any display when you compile and run because your output would be a dll file if you have added any classes.
    Monday, March 05, 2012 9:27 PM
  • Hello msnhelpsme,

    dialog resultshould beenhancedimmediately,andcannot declarea variable,see example below.

            private void button3_Click(object sender, EventArgs e)
            {
                var dialogresult = MessageBox.Show("your text here",Application.ProductName.ToString(),MessageBoxButtons.YesNoCancel);
                if (dialogresult.Equals(DialogResult.Yes))
                {
                    //your code here
                    MessageBox.Show("you presset yes" );
                }    
            }

    Regards.

    Ok, declaring it as type 'var' and setting it equal to MessageBox.Show gets rid of one error.  Why?  Also, I still have this error with the 'DialogResult.Yes' statement: "error CS1061: 'System.Nullable<bool>' does not contain a definition for 'Yes' and no extension method 'Yes' accepting a first argument of type 'System.Nullable<bool>' could be found (are you missing a using directive or an assembly reference?)"

    Monday, March 05, 2012 9:28 PM
  • Which version of VS do you use? Because these all code snipets should work from 2003 to 2010 versions.

    Mitja

    Monday, March 05, 2012 9:30 PM
  • Hello msnhelpsme,

    when you createdthe projectyou choosean applicationtypeorWindowsFormConsole?

    I didn't create the project, so I don't know.
    Monday, March 05, 2012 9:31 PM
  • Does it have a GUI that inherits the windows theme or does it look like you are in command prompt? If it looks like you are in command prompt when you compile, you are using a console app. Also, you could have selected other things like a class library in which case you would have any display when you compile and run because your output would be a dll file if you have added any classes.

    It builds an exe and when I run it I get a window that appears with buttons that I can click.
    Monday, March 05, 2012 9:32 PM
  • Hello msnhelpsme,

    Ok, declaring it as type 'var' and setting it equal to MessageBox.Show gets rid of one error.  Why?  Also, I still have this error with the 'DialogResult.Yes' statement: "error CS1061: 'System.Nullable<bool>' does not contain a definition for 'Yes' and no extension method 'Yes' accepting a first argument of type 'System.Nullable<bool>' could be found (are you missing a using directive or an assembly reference?)"

    and strange, this example of a version of VisualStudio 2010 is working, the error you get and already in the designer or runtime?

    Regards.


    Monday, March 05, 2012 9:36 PM
  • I think the guessing game has gone on far enough... the best option would be to copy the entire cs of your main form (or whatever form you are having issues with) and paste it into thhe forum so the fine people of the forum will be able to see the bigger picture.
    Monday, March 05, 2012 9:37 PM
  • Hello msnhelpsme,

    and strange, this exampleofaversionofVisualStudio2010is working,the erroryou get andalreadyin thedesigner orruntime?

    Regards.


    This error is seen at compile time using Visual Studio 2008.
    • Edited by msnhelpsme Monday, March 05, 2012 9:39 PM
    Monday, March 05, 2012 9:39 PM
  • One final guess on my part: It sounds like you are missing the reference to System.Windows.Forms

    In the solution explorer, right click on your "References" folder->Add Reference...

    In the .Net tab search through the unholy amount of references to find "System.Windows.Forms"

    Select and hit OK.

    This should work whether or not you're working in a Form, Dll, Console App...Anything!


    • Edited by Idea Hat Monday, March 05, 2012 9:45 PM
    • Marked as answer by msnhelpsme Monday, March 05, 2012 10:24 PM
    Monday, March 05, 2012 9:44 PM
  • Here I'll make it easy for you. You need to use a switch instead of ifs and elses.

    switch (MessageBox.Show("My Question", "My Title", MessageBoxButton.YesNo))
    {
        case DialogResult.Yes:
              //DO SOMETHING
              break;
        case DialogResult.No:
              //DO SOMETHING ELSE
              break;
    }


    Aaron Chapman

    • Proposed as answer by Aaron Chapman Monday, March 05, 2012 9:50 PM
    • Unproposed as answer by msnhelpsme Monday, March 05, 2012 9:55 PM
    • Proposed as answer by Aaron Chapman Monday, March 05, 2012 10:53 PM
    Monday, March 05, 2012 9:49 PM
  • One final guess on my part: It sounds like you are missing the reference to System.Windows.Forms

    In the solution explorer, right click on your "References" folder->Add Reference...

    In the .Net tab search through the unholy amount of references to find "System.Windows.Forms"

    Select and hit OK.

    This should work whether or not you're working in a Form, Dll, Console App...Anything!


    Closer.  This line is now ok:

    System.Windows.Forms.DialogResult dlgResult = new System.Windows.Forms.DialogResult();

    Now this line has an error:

    dlgResult = MessageBox.Show("my msg", "my title", MessageBoxButton.YesNo);

    "error CS0266: Cannot implicitly convert type 'System.Windows.MessageBoxResult' to 'System.Windows.Forms.DialogResult'. An explicit conversion exists (are you missing a cast?)"

    And this line is still in error:

    dlgResult.Equals(DialogResult.Yes)

    "error CS1061: 'System.Nullable<bool>' does not contain a definition for 'Yes' and no extension method 'Yes' accepting a first argument of type 'System.Nullable<bool>' could be found (are you missing a using directive or an assembly reference?)"

    I see the code is using

    using Microsoft.Win32;

    could this be throwing something off?
    Monday, March 05, 2012 9:54 PM
  • Hello msnhelpsme,

    This error is seen at compile time using Visual Studio 2008.

    make sure you have set above references the System.Windows.Forms Using statement within the class.

    Regards.


    • Marked as answer by msnhelpsme Monday, March 05, 2012 10:24 PM
    Monday, March 05, 2012 9:57 PM
  • make sure you have set above references the System.Windows.Forms Using statement within the class.


    I added "using System.Windows.Forms;" to the top of the file, and then I had to manually resolve all conflicts by typing out the entire namespace.  Does this seem right?  The code works like I want it to.
    Monday, March 05, 2012 10:24 PM
  • Hello msnhekpsme,

    make sure you have set above references the System.Windows.Forms Using statement within the class.


    I added "using System.Windows.Forms;" to the top of the file, and then I had to manually resolve all conflicts by typing out the entire namespace.  Does this seem right?  The code works like I want it to.

    If you import the System.Windows.Forms namespace, and does not need to type all the aliases to complete each statement, using the initial reason of education is precisely to avoid typing the entire namespace.

    Regards.


    Monday, March 05, 2012 10:28 PM
  • Yes, but I was getting conflicts with other imports, such as System.Windows.Input and Microsoft.Win32 so that is why I typed the entire path.
    Monday, March 05, 2012 10:38 PM