Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use Messagebox in class library c#?

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

like image 796
PotterWare Avatar asked Dec 21 '22 07:12

PotterWare


1 Answers

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);
        }
    }
}
like image 152
Guru Kara Avatar answered Jan 07 '23 04:01

Guru Kara