<StackPanel Height="23" HorizontalAlignment="Right" VerticalAlignment="Bottom" Orientation="Horizontal">
<Button x:Name="btnYes" Content="Cancel" Click="CancelButton_Click" Width="75" Height="23" />
<Button x:Name="btnNo" Content="OK" Click="OKButton_Click" Width="75" Height="23" />
<Button x:Name="btnCancel" Width="75" Content="Button"/>
</StackPanel>
<TextBlock x:Name="txtMsg" Text="TextBlock" TextWrapping="Wrap" Margin="155,51,8,87"/>
<Image x:Name="imgIcon" HorizontalAlignment="Left" Width="100" Margin="8,66,0,99"/>
Now in the code delete all the default things.Now my requirement was to show four types of information : question,warning,error message and general information. Also I had to show relative icons in the left hand side of the box too. So in the code I added the followings:
public enum MessageBoxButtons
{
Ok, YesNo, YesNoCancel, OkCancel
}
public enum MessageBoxIcon
{
Question, Information, Error, None, Warning
}
So now user is having the option as per the requirement, he/she can select the message box buttons and icons to. You can modify it as per as your requirement.
We have created our message box. One last thing we have to know that which button is clicked. I tried using implementing similar to the default message box show method, but in this method the application didn't wait for the user to close the dialog in order to complete its work. In order to overcome this issue, I decided to handle the close event of the messagebox control so that we can know the clicked button.
I declared a delegate function with one parameter of type MessageBoxResult in order to know which button was clicked.
public delegate void MessageBoxClosedDelegate(MessageBoxResult result);
public event MessageBoxClosedDelegate OnMessageBoxClosed;
public MessageBoxResult Result { get; set; }
private void MessageBoxChildWindow_Closed(object sender, EventArgs e)
{
if (OnMessageBoxClosed != null)
OnMessageBoxClosed(this.Result);
}
All set. I am not going to explain the rest of the codes. You can download the zip file and go through it. Trust me, It will be easy to understand.Now build the project and you will be having the dll.
Using the Message Box user control in project:
First you need to add the dll and also the System.Windows.Control dll to your project. Then create an instance of the MyMessageBox class and in the constructor decide which type of message box you want. Then call the default show method. And on the close event, decide what to do when a particular button is clicked.
MyMessageBox newBox = new MyMessageBox("Error!!", "An unexpected error occured", MessageBoxButtons.Ok, MessageBoxIcon.Error);
newBox.OnMessageBoxClosed += new MyMessageBox.MessageBoxClosedDelegate(newBox_OnMessageBoxClosed);
newBox.Show();
That's it.Enjoy!!


Zorayr KhalapyanPosted Jan 22, 2011, 11:56 PM EST
For already implemented message boxes (info, error, confirm, text input, combo input, etc) check out http://silverlightmsgbox.codeplex.com/. It's pretty easy to utilize and extend. Have fun!
Apu GhoshPosted Dec 28, 2010, 12:47 AM EST
THANKS A LOT ...................... BRO. good job