How to use MessageBox in class library?
Here is my code
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace MessageBoxes
{
class ShowInfo
{
MessageBox.Show("test");
}
}
i can load MessageBox but can't have show property, MessageBox.Show("test"); <-- fail
You should NOT use a Windows forms MessageBox inside a class library. What if you use this library in an ASP.NET application. The MessageBox will be shown in Webserver. And your webserver will be waiting (hung) untill someone responds to that MessageBox in webserver.
An ideal design would be that you either return the message as string and deal with that string in caller specific way or throw an exception if thats what you want.
If you still want then here is your code corrected
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace MessageBoxes
{
class ShowInfo
{
public void ShowMessage(string msg)
{
MessageBox.Show(msg);
}
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With