如何在类库c#中使用Messagebox?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15848787/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 18:08:58  来源:igfitidea点击:

How to use Messagebox in class library c#?

c#.netmessagebox

提问by PotterWare

How to use MessageBox in class library?

如何在类库中使用MessageBox?

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

我可以加载 MessageBox 但不能有显示属性 MessageBox.Show("test"); <-- 失败

采纳答案by Guru Kara

You should NOTuse a Windows forms MessageBoxinside 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.

您应使用Windows窗体MessageBox的一个内类库。如果您在 ASP.NET 应用程序中使用此库会怎样。MessageBox 将显示在 Webserver 中。并且您的网络服务器将等待(挂起),直到有人响应网络服务器中的 MessageBox。

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);
        }
    }
}

回答by Steve

You have the call to messagebox outside any method.
This code cannot be compiled at all.

您可以在任何方法之外调用 messagebox。
这段代码根本无法编译。

You should write

你应该写

namespace MessageBoxes
{
    class ShowInfo
    {
        public void ShowUserMessage(string messageText)
        {
             MessageBox.Show(messageText);
        }
    }
}

and then call it after instancing an object of type ShowInfo

然后在实例化 ShowInfo 类型的对象后调用它

ShowInfo info = new ShowInfo();
info.ShowUserMessage("This is a Test");

回答by Nathan Abramov

Make sure you actually use the the class in the main form.

确保您实际使用了主窗体中的类。

class ShowInfo
{
    public static void show()
    {
        System.Windows.Forms.MessageBox.Show("test");
    }
}

...

...

    public Form1()
    {
        InitializeComponent();
        ShowInfo.show();
    }

回答by Vijunav Vastivch

Additional Answer to this Question:

这个问题的补充回答:

After the Class Library Project has been created.

在创建类库项目之后。

Right Click your Project Add> New Item> Windows form

右键单击您的项目Add> New Item>Windows form

it's done by adding reference System.Windows.Forms.dll

它是通过添加引用来完成的 System.Windows.Forms.dll