-1

I am trying to add a confirmation messagebox in C# code and the examples I have found and I have added the below example but I keep getting error message "The name 'MessageBox' does not exist in the current context" I am pretty new to C# and need help as I need a confirmation message for the user of the page.

I have tried adding the Using system.windows.form to see if that resolves the messagebox issue but so far no luck

 const string message = "Are you sure that you would like to close the form?";
        const string caption = "Form Closing";
        var result = MessageBox.Show(message, caption,
                                     MessageBoxButtons.YesNo,
                                     MessageBoxIcon.Question);

        // If the no button was pressed ...
        if (result == DialogResult.No)
        {
            // cancel the closure of the form.
            e.Cancel = true;
        }

Based on a compare I will perform, I will need this messagebox to prompt and ask the user if they are sure they want to continue or not and if not it is to return to the page, if so then the new code executes.

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
Todd L
  • 1
  • 1
  • 1
    Note that [tag:C] is not [tag:C#]. Please make sure that you use the correct tags in the future. – Thomas Jager Aug 07 '19 at 14:56
  • Also tell us what API you are using; ASP.NET, WinForm, WPF, UWP. Note the answers you have got so far all assume WinForms. – Dour High Arch Aug 07 '19 at 15:47
  • Honestly i dont know where to look to tell you if i am using asp.net, winform, WPF or UWP, can you tell me where to look? i am so frustrated that every example i look at shows that i need to set up the code as i have it but i can't get it to work. – Todd L Aug 07 '19 at 17:42

2 Answers2

0

In the Solution Explorer: Right Click your Project -> Add -> Reference... -> Assemblies -> In the top right is a searchbar, enter "Forms" -> System.Windows.Forms -> OK

lionthefox
  • 369
  • 1
  • 2
  • 16
  • Assuming you use Visual Studio and that it's a Windows Forms Project and not WPF. – lionthefox Aug 07 '19 at 15:13
  • I have added the reference to System.Windows.Forms. Getting this error when i execute page. ASPX page. The type or namespace name 'DialogResult' does not exist in the namespace 'System.Windows.Forms' – Todd L Aug 07 '19 at 16:43
0

You are missing adding references to the System.Windows.Forms dll. Please add the references and using statement.

  • Post exact error from IDE if this doesn't solves your error. – Sanjay Kumar Aug 07 '19 at 15:15
  • The WPF equivalent would be the System.Windows.MessageBox. Based on your project type select the correct using namespace. – Sanjay Kumar Aug 07 '19 at 15:17
  • I have added the reference to System.Windows.Forms. Getting this error when i execute page. ASPX page. The type or namespace name 'DialogResult' does not exist in the namespace 'System.Windows.Forms' – – Todd L Aug 07 '19 at 17:02
  • Honestly i dont know where to look to tell you if i am using asp.net, winform, WPF or UWP, can you tell me where to look? i am so frustrated that every example i look at shows that i need to set up the code as i have it but i can't get it to work. – Todd L Aug 07 '19 at 17:42
  • @ToddL did you create the project yourself? You choose the type when creating it. – lionthefox Aug 08 '19 at 10:58
  • @ToddL If it's an ASPX page, you are using ASP.NET and not WinForms. So your code can't work. Look into this: https://stackoverflow.com/questions/9720143/asp-net-web-application-message-box – lionthefox Aug 08 '19 at 12:27