Home » C# programming » Custom input dialog in C# WPF for numeric input

Custom input dialog in C# WPF for numeric input

In C# and Windows Presentation Foundation there are building dialogs, there is not an input dialog, but creating your own is almost just as easy. In fact, you really just need to create a Window, place the required controls in it and then show it.

In this article, we’ll create a very simple dialog to ask the user a question and then return the answer, while discussing the various good practices that you should follow.

Designing the dialog

For this particular dialog, I just wanted a TextBox for entering the answer, and then the usual Ok and Cancel buttons.

The dialog has a TextBox for input, an OK button and a Cancel button

And here is the XAML code for the dialog:

<Window x:Class=”WpfApplication1.Window1″
Title=”Window1″ Height=”184.182″ Width=”266.277″>
<Grid Margin=”0,0,2,29″>
<TextBox x:Name=”textBox” HorizontalAlignment=”Left” Height=”24″ Margin=”23,40,0,0″ TextWrapping=”Wrap” Text=”0.0″ VerticalAlignment=”Top” Width=”213″ PreviewTextInput=”Preview_e”
<Button x:Name=”button” Content=”OK” HorizontalAlignment=”Left” Height=”32″ Margin=”23,82,0,0″ VerticalAlignment=”Top” Width=”77″ Click=”button_OKClick”/>
<Button x:Name=”button1″ Content=”Cancel” HorizontalAlignment=”Left” Height=”32″ Margin=”158,82,0,0″ VerticalAlignment=”Top” Width=”78″ Click=”button_CancelClick”/>
<Label x:Name=”label” Content=”Label” HorizontalAlignment=”Left” Height=”25″ Margin=”23,10,0,0″ VerticalAlignment=”Top” Width=”213″/>



This is the windows1.xaml.cs code corresponding to the above dialog.

using System;
using System.Collections.Generic;
using System.Linq;

using System.Text;
using System.Threading.Tasks;
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;
using System.Text.RegularExpressions;

namespace WpfApplication1
/// <summary>
/// interation for Window1.xaml
/// </summary>

public partial class Window1 : Window 

public float TotalValue { get; set; }

public Window1(string caption, double dvalue)
this.label.Content = caption;
this.textBox.Text = dvalue.ToString();


private void button_CancelClick(object sender, RoutedEventArgs e)

private void button_OKClick(object sender, RoutedEventArgs e)

TotalValue = float.Parse(textBox.Text);

private void Preview_e(object sender, TextCompositionEventArgs e)
Regex regex = new Regex(“^[.][0-9]+$|^[0-9]*[.]{0,1}[0-9]*$”);
e.Handled = !regex.IsMatch((sender as TextBox).Text.Insert((sender as TextBox).SelectionStart, e.Text));



System.Text.RegularExpressions is added to the code because the Preview_e function uses regular expressions (Regex).

In the example, with the above regex expression you can only insert numbers in the textBox, and not text.

Using the dialog

You can use this code to call the custom input dialog, with a prompt text “insert value”.

The value m_value will be returned.

var W01 = new Window1(“insert value”, m_value);




  1. Pingback: writeessay

Leave a Reply