25. Custom message box i
Note: While reading this page, bear in mind that I'm no computer expert and that the text below may be partly inaccurate. If you find errors or have proposals for improvements, please send me a message and help make this a better page for the benefit of future visitors. To the left, there are links to more C# tutorials.
To complete this tutorial, follow these instructions:
1. Open Visual C# 2010 Express.
2. Click on New Project in the File menu.
3. Choose Windows Forms Application if that option isn't already chosen and click on OK.
4. Point at Toolbox in the left margin and click on the Auto Hide icon at the top of the window (to keep the window to stay open and easier to work with).
5. Drag a Button to Form 1.
6. Double-click on button1 and write or paste this code where the caret is:
Form2.showBox("Hello", "Hello world!", "OK");
7. Click on Add Windows Form in the Project menu.
8. Choose Windows Form if that option isn't already chosen and click on OK.
9. In the Properties pane, change FormBorderStyle to FixedToolWindow.
10. In the Properties pane, change ShowInTaskbar to False.
11. In the Properties pane, change StartPosition to CenterScreen.
12. In the Properties pane, change Text to no text.
13. Drag a Label to the top of Form 2.
14. Drag a Button to the bottom of Form 2.
15. Double-click on button1 (on Form2) and write or paste this code where the caret is:
16. Write or paste this code right above
private void button1_Click(object sender, EventArgs e):
public static Form2 mb;
public static void showBox(string title, string msg, string btn)
mb = new Form2();
mb.Text = title;
mb.label1.Text = msg;
mb.button1.Text = btn;
17. Press F5 to start debugging the program.
18. Click on button1. A MessageBox with the title
Hello, the message
Hello world! and a button with the text
OK should appear in the center of the screen.
19. Click on OK to close the custom message box.
Comments on the first code snippet
When button1 on Form1 is clicked, the method showBox is called and the custom message box appears.
Hello is the title of the custom message box.
Hello world! is the text of the custom message box.
OK is the text of the button on the custom message box.
Comments on the second code snippet
When button1 on Form2 (the OK button) is clicked, Form2 (the custom message box) is closed.
Comments on the third code snippet
Line 1: Create a new instance of Form2.
Line 3-10: A method to set title, message and button text of a custom message box.
Would you like to comment on this page or some other page? Use the contact form. Write the title of the page you want to comment on and your comment in the field Message.