![]() The first is the message the second is the title. The second overloaded method of the ShowBox() method takes two parameters. This value, as mentioned earlier, is either 1, 2, or null if the X button is clicked. This means that when your message box is displayed, the user will not be able to interact with the form that created the message box until the user makes a choice-either OK, Cancel, or the X Close button.įinally, you use the return statement to return a value back to the calling method. After setting the message, you need to show the message box you use the ShowDialog() method to show the message box as a dialog box. Now that an object of MyMessageBox Class is created, you can set the label of the message box. Previously, you defined an ojbect ‘newMessageBox’ from your MyMessageBox class now, in the ShowBox() method, you use the new keyword to instaniate the object. It takes one parameter this parametter is the message to be displayed on your message box. The preceding is the first of the two overloaded ShowBox() methods. Public static string ShowBox(string txtMessage) Following is the code for the ShowBox() method. Thats is, you can determine which button was clicked by the user (OK/Cancel). This means that not only can you use the method to set the message on our message box but you also can get a result back. This method also will be used as an Accessor. This overloaded method will take one or two parameters. For your custom message box, you will create a ShowBox() method that will be an overloaded method. The Windows C# MessageBox Class has a Show() method. Both should be declared just under the public partial class: You also need to define an object of MyMessageBox it is also defined as static. ![]() The variable is declared as type string and is a static variable. This variable will be used to determine which button was clicked. ![]() If the user clicks the OK button, this variable will be set to 1 if the user clicks the Cancel button, the variable will be set to 2. First, you need to declare a variable called Button_id this variable will hold a number-either 1 or 2. The code for the custom message box is quite simple. Use the following table to set the properties for label1. Now, use the following table to set the properties for button2. Use the following table to set the properties for button1. Use the following table to set the properties for the form. Create a new Windows Application titled MyMessageBox. You’re now going to create a custom message box. I will also improve on the message box by adding a timer feature. In this article, I will explain how to create a simple OK/Cancel message box. There are also times when you might develop an application with a great user interface but it is ruined by the dull Windows message box.įortunately, it is very easy for C# developers to develop a custom message box of their own to incorporate it into their applications. For example, suppose you wanted to display a lot of text and therefore wanted a message box that was sizable or even scrollable by the user. For many applications, the message box will work fine however, there are times when the message box is limited. The Windows message box is a sufficient means to display messages to the user.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |