Customized Message Box In Windows Phone Using C#

Introduction

Hello, Friend. Today I am explaining how to customize the default Message Box given in Windows Phone. And also how to use the result returned by pressing the keys of the Message Box in your application. It's simple and very handy for you to implement it in your application.

PROCEDURES

Step 1

Create a New Windows Phone project. Name it whatever you want (I named it "try_MsgBox").

Now add a Button Control to your "MainPage.xaml" by dragging it from the Tools Window. Also add a TextBlock Control from the Tools Window that will show your action after pressing the MessageBox Button.

(I named the Button Control "DisplayMsg" and the TextBlock control "ResultTextBlock" and its text property to "myResult", you can name them as you wish.)

Step 2

Now go to the Button Event that you added to your MainPage.xaml.

You can do so by just double-clicking on the Button Control in your Main Page, the IDE will automatically generate an event, or you can just do this in the "MainPge.xaml" by just typing "Click" in the "<Button/>" and it will ask you to create a new event handler; double-click on it. the IDE will then generate it for you.



Step 3

Now its time to write some code for the Button Event. (In my program it is called "DisplayMsg_Click(object sender, RoutedEventArgs e)".)

//Button Event
private void DisplayMsg_Click(object sender, RoutedEventArgs e)
{

//Creating Instance of MessageBoxResult Class named ‘result’(you can however name it as per //your convention)

MessageBoxResult result= MessageBox.Show("This my Customised Message Box", "Customised Message", MessageBoxButton.OKCancel);

//Checking the Condition for the Button pressed in Message Box. If it is OK then the TextBlock //will display the text as “I have pressed OK button” otherwise it will show “I have pressed //Cancel Button”.(These are for example you can do whatever you required in this ‘if-else’ //clause).

if (result == MessageBoxResult.OK)
{
ResultTextBlock.Text = "I have Pressed \"OK\" Button";
}

else
{
ResultTextBlock.Text = "I have Pressed \"Cancel\" Button ";
}

}

Step 4

Now compile and run it. When you press the button it will pop up a Message Box showing the text and with the two Buttons “OK” and “Cancel”.

Whenever you press the “OK” button the TextBlock will show “You have Pressed “OK” Button ” otherwise it will Show “You have Pressed “Cancel” Button”.



CODE DESCRIPTION

The first line of the code in the Button Event is:

MessageBoxResult result= MessageBox.Show("This my Customised Message Box", "Customised Message", MessageBoxButton.OKCancel);


Here actually we are storing the result returned from the MessageBox.Show() method in our "result" variable of type MessageBoxResult. So that we can use it in our program later.

Now we need to describe the MessageBox.Show() method. It is taking three parameters, two of which are String type and the third one is actually the option to show the Buttons in the Message Box.

The second parameter is for the title of the Message Box (in our program the title is "Customised Message") and the first one is for the description you wanted to show in your Message Box. (This is actually the description you want to show the user in the Message Box).

The third one as I told is the Button type in the Message Box to be shown. (It has two options for the Windows Phone Application, “OK” and “OKCancel”, in our example we used “OKCancel”).

Now the rest of the code is:

if (result == MessageBoxResult.OK)
{
ResultTextBlock.Text = "I have Pressed \"OK\" Button";
}

else
{
ResultTextBlock.Text = "I have Pressed \"Cancel\" Button ";
}

}


Here we are using an else clause to implement the result returned by the MessageBox.Show() method.

It will return the result and be tored in whatever the variable is that we have used (in this example we have used "result" as the variable).

Now it will check the result with the MessageBoxResult.OK property; that is actually checking that we pressed the "OK" button. If so then it will execute the if block otherwise the else block will be executed.

That’s all for this article.

Thank You.


Similar Articles