MessageBoxButton exception WPF, VB.net project

59 views Asked by At
I am using the following code in a Button_Click event:
        If OwordDoc IsNot Nothing Then
            System.Windows.MessageBox.Show("A document is already open.", MessageBoxButton.OK, MessageBoxImage.Information)
            Return
        End IF

I am getting the following runtime error no matter which MessageBoxButton enumeration I use.

(InvalidEnumArgumentException: 'The value of argument 'button' (64) is invalid for Enum type 'MessageBoxButton'.)

I'm not using any other MessageBox elsewhere. Although this is a first time wpf project, this seems so fundamental, what am I doing wrong?

I have checked through previous StackOverflow Q/A and feel I am almost married to Microsoft Copilot!

1

There are 1 answers

2
jmcilhinney On BEST ANSWER

If you had read the relevant documentation, as you should have before posting a question, then you'd know that there is no overload of MessageBox.Show with three parameters of those types. If you'd simply paid attention to Intellisense when typing the code then you'd know too. You need to provide two Strings - the title and the message - followed by a MessageBoxButton and then a MessageBoxImage. In your code, the MessageBoxButton value is being converted to a String and the MessageBoxImage value is being converted to a MessageBoxButton value. As the error message clearly states, that conversion is not valid.

This is a perfect example of why you should ALWAYS have Option Strict On. If you had done then implicit conversions like that would not be allowed and your code would not compile. Turn Option Strict On in the project properties and fix any late binding or implicit conversion errors that result. You should also turn it On in the VB options, so it will be On by default for all future projects.