Replacement for yes/no/cancel MessageBox (C#)

北慕城南 提交于 2019-12-04 09:55:26

Check out Dissecting the MessageBox on CodeProject. The project is a bit dated, but it's pretty much exactly what you're looking for and it shouldn't take much to update it.

Depending on your target platform, a task dialog may be a good way of doing this. There is a .NET wrapper for task dialogs in the Windows API Code Pack. However these are provided only in Windows Vista and above, not in XP or 2003.

Frankly, it is not that difficult to create such a Messagebox yourself, we have such a thing working in the current app we are developing.

What you need is a FlowLayout for the buttons that will auto-align any buttons you create. Our API then has something like (params Tuple<string,DialogResult>[] buttons)

Tuple is a helper class that contains two values. The string is the Text of the button, the Dialogresult is the one our messagebox returns when the button with said text is clicked.

I agree with Frank. It wouldn't be too difficult to create your own generic form that handles this for you. Without getting into code, the form should do the following

1) Have a property to set the message you want to show to the user.

2) Have a method for adding buttons, with 2 arguments, one for the button text, and one for the dialog result

3) When the form is displayed, it should be in modal dialog mode so that the rest of the application is inactive while until one of the options is clicked.

So, to create a Save As/Don't Save/Cancel, you would add 3 buttons in step 2, all with the appropriate button text and dialog result.

Using Flow layout, you should be able to get it to display properly regardless of the size of the message, or the number of buttons.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!