Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add an extension method to the MessageBox

it's possible?

based in another's examples, as LabelExtesios, StringExtensions, etc. I wrote this:

namespace MessageBoxExtensions
{

    public static class MessageBoxExtensionsClass
    {
        public static void Foo()
        {

        }
    }
}

then:

using MessageBoxExtensions;
// ... 

MessageBox.Foo();

gin an error: MessageBox.Foo();

'System.Windows.Forms.MessageBox' does not contain a definition for 'Foo'
like image 671
Jack Avatar asked Dec 31 '11 13:12

Jack


People also ask

How do you declare an extension method?

An extension method must be defined in a top-level static class. An extension method with the same name and signature as an instance method will not be called. Extension methods cannot be used to override existing methods. The concept of extension methods cannot be applied to fields, properties or events.

How do you add an extension to a static class?

Extension methods require an instance variable (value) for an object. You can however, write a static wrapper around the ConfigurationManager interface. If you implement the wrapper, you don't need an extension method since you can just add the method directly.

What is MessageBox class explain MessageBox () in detail?

A message box is a prefabricated modal dialog box that displays a text message to a user. You show a message box by calling the static Show method of the MessageBox class. The text message that is displayed is the string argument that you pass to Show.

What method do you call to display a message box?

To display a message box, call the static method MessageBox. Show. The title, message, buttons, and icons displayed in the message box are determined by parameters that you pass to this method.


1 Answers

Description

You cant do this because System.Windows.Forms.MessageBox is NOT an instance of MessageBox. MessageBox.Show() is a static method.

You cant create an instance of MessageBox because this class has no public constructor.

Update

But you can create your own class in the System.Windows.Forms namespace and use the MessageBox in this method like this

Sample

namespace System.Windows.Forms
{
    public static class MyCustomMessageBox
    {
        public static void Foo()
        {
            MessageBox.Show("MyText");
        }
    }
}

MyCustomMessageBox.Foo();
like image 70
dknaack Avatar answered Oct 13 '22 21:10

dknaack