[英]How to use Messagebox in class library c#?
如何在類庫中使用MessageBox?
這是我的代碼
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");
}
}
我可以加載MessageBox但不能具有show屬性,MessageBox.Show(“ test”); <-失敗
你不應該使用一個Windows窗體MessageBox的 類庫里面。 如果在ASP.NET應用程序中使用此庫,該怎么辦。 MessageBox將顯示在Web服務器中。 並且您的Web服務器將一直等待(掛起),直到有人響應Web服務器中的該MessageBox。
理想的設計是,您要么以字符串形式返回消息,然后以調用者特定的方式處理該字符串,要么就這樣拋出異常。
如果您仍然想要,那么這里的代碼已更正
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);
}
}
}
您可以通過任何方法來調用消息框。
此代碼完全不能編譯。
你應該寫
namespace MessageBoxes
{
class ShowInfo
{
public void ShowUserMessage(string messageText)
{
MessageBox.Show(messageText);
}
}
}
然后實例化ShowInfo類型的對象后調用它
ShowInfo info = new ShowInfo();
info.ShowUserMessage("This is a Test");
此問題的其他答案:
創建類庫項目之后。
右鍵單擊您的項目,然后單擊Add
> New Item
> Windows form
通過添加參考System.Windows.Forms.dll
確保您在主窗體中實際使用了該類。
class ShowInfo
{
public static void show()
{
System.Windows.Forms.MessageBox.Show("test");
}
}
...
public Form1()
{
InitializeComponent();
ShowInfo.show();
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.