Tuesday 9 September 2014

Create Customize MessageBox in WPF

hey there ...

Most of the time we get the requirement to design our own message box as the message box by default is not looking cool.. so here is the simple code to create your own customize message box.....


Microsoft message box



your Custom code







XAML code...



Window x:Class="WpfDigital.message"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="message" Height="200" Width="480" ResizeMode="NoResize" WindowStartupLocation="CenterScreen" AllowsTransparency="True"  ShowInTaskbar="True" WindowStyle="None"  Background="Transparent">
    <Border ClipToBounds="True"  BorderBrush="#FF000000" Background="White" BorderThickness="1" CornerRadius="8">
        <Canvas Margin="-3,-25,-4,-10">
            <Canvas.Background>
            <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
                <GradientStop Color="Black" Offset="0"/>
                <GradientStop Color="White" Offset="1"/>
            </LinearGradientBrush>
        </Canvas.Background>
            <Button x:Name="btnOk" Content="Ok" HorizontalAlignment="Left" VerticalAlignment="Top" Width="96" Height="29" Canvas.Left="241" Canvas.Top="167" Click="btnOk_Click">
                <Button.Background>
                    <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
                        <GradientStop Color="Black" Offset="0"/>
                        <GradientStop Color="White" Offset="1"/>
                    </LinearGradientBrush>
                </Button.Background>
            </Button>
            <Button x:Name="btnCancel" Content="Cancel" HorizontalAlignment="Left" VerticalAlignment="Top" Width="96" Height="29" Canvas.Left="354" Canvas.Top="168" Click="btnCancel_Click">
                <Button.Background>
                    <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
                        <GradientStop Color="Black" Offset="0"/>
                        <GradientStop Color="White" Offset="1"/>
                    </LinearGradientBrush>
                </Button.Background>
            </Button>
            <Label x:Name="lblTitle" Content="Message" HorizontalAlignment="Left" VerticalAlignment="Top" Height="29" Width="73" Canvas.Top="33" Canvas.Left="27">
                <Label.Background>
                    <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
                        <GradientStop Color="Black" Offset="0"/>
                        <GradientStop Color="White" Offset="1"/>
                    </LinearGradientBrush>
                </Label.Background>
            </Label>
            <Label x:Name="lblMessage" Content="Message" HorizontalAlignment="Left" VerticalAlignment="Top" Height="81" Width="402" Canvas.Left="29" Canvas.Top="73" BorderBrush="{DynamicResource {x:Static SystemColors.GrayTextBrushKey}}">
                <Label.Background>
                    <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
                        <GradientStop Color="Black" Offset="0"/>
                        <GradientStop Color="White" Offset="1"/>
                    </LinearGradientBrush>
                </Label.Background>
            </Label>
        </Canvas>
    </Border>

</Window>

XAML.cs code for the same is here...



using System.Windows;
namespace WpfDigital
{
    /// <summary>
    /// Interaction logic for message.xaml
    /// </summary>
    public partial class message : Window
    {
        public message()
        {
            InitializeComponent();
        }

        static message newmessagebox;
        static string Button_id;
        public static string ShowBoxOk(string Title,string txtmessage )
        {
            newmessagebox = new message();
            newmessagebox.lblTitle.Content = Title;
            newmessagebox.lblMessage.Content = txtmessage;
            newmessagebox.btnOk.Visibility=Visibility.Visible;
            newmessagebox.btnCancel.Visibility = Visibility.Hidden;
            newmessagebox.ShowDialog();
            return Button_id;
        }
        public static string ShowBoxOkCancel(string Title,string txtmessage)
        {
            newmessagebox = new message();
            newmessagebox.lblTitle.Content = Title;
            newmessagebox.lblMessage.Content = txtmessage;
            newmessagebox.btnOk.Visibility = Visibility.Visible;
            newmessagebox.btnCancel.Visibility = Visibility.Visible;
            newmessagebox.ShowDialog();
            return Button_id;
        }
       
        private void btnOk_Click(object sender, RoutedEventArgs e)
        {
            Button_id = "1";
            newmessagebox.Hide();
        }

        private void btnCancel_Click(object sender, RoutedEventArgs e)
        {
            Button_id = "2";
            newmessagebox.Hide();
        }
    }
}



Call it like that...

private void btn1_Click(object sender, RoutedEventArgs e)
        {
            string s = message.ShowBoxOkCancel("CONFIRM","HI OK OR CANCEL", );
            if (s == "1")
            {
                message.ShowBoxOk("Alert","HI OK");
            }
            else if (s == "2")
            {
                message.ShowBoxOk("Alert","HI Cancel" );
            }

        }


thats it you have done a great job....

No comments:

Post a Comment