Software & Finance





Visual Studio.NET WPF - Implementing Modal Dialogs





I have implemented two modal dialogs in this sample application called POP3 and SMTP settings dialog. From the main window page, you will have an option to invoke dialogs and get the values updated in the main window.

Create a WPF application from VS2010 wizard and then add two window classes called SMTP and POP3.

images/wpfmd1.jpg

 

images/wpfmd2.jpg


Click here to get the complete VS2010 project and executable


Here is the source code given below:

// MainWindow.xaml

 

<Window x:Class="SWFModalDialog.MainWindow"

        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

        Title="Email Configuration - softwareandfinance.com" Height="330" Width="433"

        Background="AliceBlue">

 

    <Grid Margin="25,15,10,10" Background="AliceBlue">

        <Grid.RowDefinitions>

            <RowDefinition Height="35" />

            <RowDefinition Height="35" />

            <RowDefinition Height="35" />

            <RowDefinition Height="35" />

            <RowDefinition Height="35" />

            <RowDefinition Height="35" />

            <RowDefinition Height="15" />

            <RowDefinition Height="35" />

            <RowDefinition Height="*" />

        </Grid.RowDefinitions>

        <Grid.ColumnDefinitions>

            <ColumnDefinition Width="150"/>

            <ColumnDefinition Width="200"/>

            <ColumnDefinition Width="*"/>

        </Grid.ColumnDefinitions>

 

            <Label Name="l1" Content="Display Name:" VerticalAlignment="Center" Width="120" Grid.Row="0" Grid.Column="0"></Label>

            <TextBox Name="t1" Text="Kathir" Height="25" Width="200" Grid.Row="0" Grid.Column="1"></TextBox>

 

            <Label Name="l2" Content="Email Address:" VerticalAlignment="Center" Width="120" Grid.Row="1" Grid.Column="0"></Label>

        <TextBox Name="t2" Text="kathir@softwareandfinance.com" Height="25" Width="200" Grid.Row="1" Grid.Column="1"></TextBox>

 

        <Label Name="l3" Content="SMTP Server:" VerticalAlignment="Center" Width="120" Grid.Row="2" Grid.Column="0"></Label>

        <TextBox Name="t3" IsEnabled="False" Text="smtp.softwareandfinance.com" Height="25" Width="200" Grid.Row="2" Grid.Column="1" ></TextBox>

 

        <Label Name="l4" Content="SMTP Port:" VerticalAlignment="Center" Width="120" Grid.Row="3" Grid.Column="0"></Label>

        <TextBox Name="t4" IsEnabled="False" Text="25" Height="25" Width="200" Grid.Row="3" Grid.Column="1" ></TextBox>

 

        <Label Name="l5" Content="POP3 Server:" VerticalAlignment="Center" Width="120" Grid.Row="4" Grid.Column="0"></Label>

        <TextBox Name="t5" IsEnabled="False" Text="pop.softwareandfinance.com" Height="25" Width="200" Grid.Row="4" Grid.Column="1" ></TextBox>

 

        <Label Name="l6" Content="POP3 Port:" VerticalAlignment="Center" Width="120" Grid.Row="5" Grid.Column="0"></Label>

        <TextBox Name="t6" IsEnabled="False" Text="110" Height="25" Width="200" Grid.Row="5" Grid.Column="1" ></TextBox>

 

 

 

        <Button Name="SMTP" Content="SMTP Settings" Width="100" Height="25" Grid.Row="7" Grid.Column="0" Click="SMTP_Click"></Button>

        <Button Name="POP3" Content="POP3 Settings" Width="100" Height="25" Grid.Row="7" Grid.Column="1" Click="POP_Click"></Button>

 

    </Grid>

</Window>

 

 

// MainWindow.xaml.cs

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Windows;

using System.Windows.Controls;

using System.Windows.Data;

using System.Windows.Documents;

using System.Windows.Input;

using System.Windows.Media;

using System.Windows.Media.Imaging;

using System.Windows.Navigation;

using System.Windows.Shapes;

 

namespace SWFModalDialog

{

    /// <summary>

    /// Interaction logic for MainWindow.xaml

    /// </summary>

    public partial class MainWindow : Window

    {

        public MainWindow()

        {

            InitializeComponent();

        }

 

        private void SMTP_Click(object sender, RoutedEventArgs e)

        {

            SWFModalDialog.SMTP dlg = new SMTP();

            dlg.t1.Text = this.t3.Text;

            dlg.t2.Text = this.t4.Text;

            dlg.ShowDialog();

            if (dlg.retVal == 0)

            {

                this.t3.Text = dlg.t1.Text;

                this.t4.Text = dlg.t2.Text;

            }

        }

 

        private void POP_Click(object sender, RoutedEventArgs e)

        {

            SWFModalDialog.POP3 dlg = new SWFModalDialog.POP3();

            dlg.t1.Text = this.t5.Text;

            dlg.t2.Text = this.t6.Text;

            dlg.ShowDialog();

            if (dlg.retVal == 0)

            {

                this.t5.Text = dlg.t1.Text;

                this.t6.Text = dlg.t2.Text;

            }

        }

    }

}

 

 

// SMTP.xaml

<Window x:Class="SWFModalDialog.SMTP"

        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

        Title="SMTP Server Settings" Height="190" Width="420" Background="AliceBlue"

        >

 

    <Grid Margin="15,15,10,10" Background="AliceBlue">

        <Grid.RowDefinitions>

            <RowDefinition Height="35" />

            <RowDefinition Height="35" />

            <RowDefinition Height="15" />

            <RowDefinition Height="35" />

            <RowDefinition Height="*" />

        </Grid.RowDefinitions>

        <Grid.ColumnDefinitions>

            <ColumnDefinition Width="150"/>

            <ColumnDefinition Width="200"/>

            <ColumnDefinition Width="*"/>

        </Grid.ColumnDefinitions>

 

        <Label Name="l1" Content="SMTP Host Name:" VerticalAlignment="Center" Width="120" Grid.Row="0" Grid.Column="0"></Label>

        <TextBox Name="t1" Text="" Height="25" Width="200" Grid.Row="0" Grid.Column="1"></TextBox>

 

        <Label Name="l2" Content="SMTP Server Port:" VerticalAlignment="Center" Width="120" Grid.Row="1" Grid.Column="0"></Label>

        <TextBox Name="t2" Text="25" Height="25" Width="200" Grid.Row="1" Grid.Column="1"></TextBox>

 

        <Button Name="OK" Width="100" Height="25" Content="OK" Grid.Row="3" Grid.Column="0" Click="OK_Click"></Button>

        <Button Name="Cancel" Width="100" Height="25" Content="Cancel" Grid.Row="3" Grid.Column="1" Click="Cancel_Click"></Button>

 

    </Grid>

</Window>

 

 

 

// SMTP.xaml.cs

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Windows;

using System.Windows.Controls;

using System.Windows.Data;

using System.Windows.Documents;

using System.Windows.Input;

using System.Windows.Media;

using System.Windows.Media.Imaging;

using System.Windows.Shapes;

 

namespace SWFModalDialog

{

    /// <summary>

    /// Interaction logic for SMTP.xaml

    /// </summary>

    public partial class SMTP : Window

    {

        public SMTP()

        {

            InitializeComponent();

        }

 

        public int retVal = -1;

 

        private void OK_Click(object sender, RoutedEventArgs e)

        {

            retVal = 0;

            this.Close();

        }

 

        private void Cancel_Click(object sender, RoutedEventArgs e)

        {

            retVal = 1;

            this.Close();

        }

    }

}

 

 

 

// POP3.xaml

<Window x:Class="SWFModalDialog.POP3"

        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

        Title="POP3 Server Settings" Height="190" Width="420" Background="AliceBlue"

        >

 

    <Grid Margin="15,15,10,10" Background="AliceBlue">

        <Grid.RowDefinitions>

            <RowDefinition Height="35" />

            <RowDefinition Height="35" />

            <RowDefinition Height="15" />

            <RowDefinition Height="35" />

            <RowDefinition Height="*" />

        </Grid.RowDefinitions>

        <Grid.ColumnDefinitions>

            <ColumnDefinition Width="150"/>

            <ColumnDefinition Width="200"/>

            <ColumnDefinition Width="*"/>

        </Grid.ColumnDefinitions>

 

        <Label Name="l1" Content="POP3 Host Name:" VerticalAlignment="Center" Width="120" Grid.Row="0" Grid.Column="0"></Label>

        <TextBox Name="t1" Text="" Height="25" Width="200" Grid.Row="0" Grid.Column="1"></TextBox>

       

 

        <Label Name="l2" Content="POP3 Server Port:" VerticalAlignment="Center" Width="120" Grid.Row="1" Grid.Column="0"></Label>

        <TextBox Name="t2" Text="110" Height="25" Width="200" Grid.Row="1" Grid.Column="1"></TextBox>

 

        <Button Name="OK" Width="100" Height="25" Content="OK" Grid.Row="3" Grid.Column="0" Click="OK_Click"></Button>

        <Button Name="Cancel" Width="100" Height="25" Content="Cancel" Grid.Row="3" Grid.Column="1" Click="Cancel_Click"></Button>

       

    </Grid>

</Window>

 

 

 

// POP3.xaml.cs

 

 

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Windows;

using System.Windows.Controls;

using System.Windows.Data;

using System.Windows.Documents;

using System.Windows.Input;

using System.Windows.Media;

using System.Windows.Media.Imaging;

using System.Windows.Shapes;

 

namespace SWFModalDialog

{

    /// <summary>

    /// Interaction logic for POP3.xaml

    /// </summary>

    public partial class POP3 : Window

    {

        public POP3()

        {

            InitializeComponent();

        }

 

        public int retVal = -1;

 

        private void OK_Click(object sender, RoutedEventArgs e)

        {

            retVal = 0;

            this.Close();

        }

 

        private void Cancel_Click(object sender, RoutedEventArgs e)

        {

            retVal = 1;

            this.Close();

        }

    }

}