Display messagebox in dll library

2019-03-02 02:19发布

问题:

I am writing a dll library (for example for checking login details of a user) and I want to pop up a confirmation dialog or a informational dialog in the process, for example

"Are you sure you want to continue? (Yes/No/Cancel)"

for some reason. At present I am need this for an application in Winforms and I want to use same dll for my other webforms applications also. Can anyone guide how to achieve it? Need less to say I don't want to reference System.Windows.Forms or System.Web. Can any one give me some pointers? I am using .net 4.0

I don't want to for Publication/Subscription of events as I am not very familiar with cab architecture.

EDIT

I have a dll for creating database at runtime of an application. So my dll generally checks if the database already exists and is upto date or not, if it is then stop. But if it does not exist or not upto date, then I want to ask user if he want to update it now or afterwards. If user says now, update it and says afterwards continue to application. It is just an example and any relevant information on this is welcome.

Thanks.

回答1:

You really shouldn't. This does not belong in a library, but should be done in the application that uses this library instead.

Where a WinForms application might solve it by showing a MessageBox, a web application might perform the request without asking after a successful POST (since a POST usually shows the intent to modify a resource).

When for example your library is used for logging in, simply throw a AuthenticationException, so the client application (whether it's WinForms, web, or whatever) can catch that and display the appropriate message.


As for your edit:

I have a dll for creating database at runtime of an application ...

Why not expose two methods like IsDatabaseUpToDate() and UpdateDatabase()? Then in the client, you can call the first method. If it returns false, you ask the user (be it with a MessageBox, an HTML form, a JavaScript Alert, a simple Yes button or a blinking tile) whether they want to update the database. If so, you call the UpdateDatabase() method and you're ready to go.



回答2:

To accomplish this you need to right click on the project and select "Add Reference", then select the ".NET" tab and click "System.Windows.Forms". Then this should work inside the .dll:

System.Windows.Forms.MessageBox.Show("Hello World!");

Of course this is a bad idea for the original poster (as covered by CodeCaster).