Click here to Skip to main content
15,943,477 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I want to display messagebox with buttons whose name can be passed as parameter and return a value to the calling function respective to the button clicked
e.g. I want two buttons to be displayed as Ok and Cancel, when user clicks Ok, I want to return 0 and 1 for cancel to the calling function.
In another case, instead of Ok-Cancel, I want to pass Yes,No,Cancel as button names and return value as 0,1,2 respectively.
Is this possible in Window Forms application?

What I have tried:

I have created another form, added buttons to the form as Ok/Cancel/Yes/No/Retry/Ignore each button have its event handler and respective code for that event handler.
But I can't use single button in multiple places as it has single event handler.
Example: Form A calling this custom message box to display Yes and No. Upon Yes email will be sent, upon "No" nothing will happen. In another case, i.e. Form B calling same custom message box to use buttons Yes and No, but this Form B want to insert data into DB upon Yes button click. I.e. Different features will consume these buttons to perform different operations, which is not achieved with what I have tried.
Updated 10-Mar-17 8:13am

"Is this possible in Window Forms application?" - Yes. There are several articles here on CodeProject that may give you some ideas - Search[^]
As to the multiple event handlers - it is the calling module that knows what to do depending on which button is pressed. The custom message box should just tell you *which* button was pressed
Share this answer
Basicly it is like CHill has answered in his Solution.
You can create your own MessageBox - that is not very complicated.
But ... normally the action doesn't comes from the MessageBox. Normally the Buttons on the MessageBox-Form set a DialogResult-Value which is evaluated from that point where the MessageBox is called (for example somewhere from your Form) and after it is called. So normally the MessageBox gives a Feedback like the DialogResult-Enumation (which really are Integer-Values - but defined ones).
Share this answer

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900