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″
xmlns=”http://schemas.microsoft.com/winfx/2006/xaml/presentation”
xmlns:x=”http://schemas.microsoft.com/winfx/2006/xaml”
xmlns:d=”http://schemas.microsoft.com/expression/blend/2008″
xmlns:mc=”http://schemas.openxmlformats.org/markup-compatibility/2006″
xmlns:local=”clr-namespace:WpfApplication1″
mc:Ignorable=”d”
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″/>

</Grid>
</Window>

 

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)
{
InitializeComponent();
this.label.Content = caption;
this.textBox.Text = dvalue.ToString();

}

private void button_CancelClick(object sender, RoutedEventArgs e)
{
this.Close();
}

private void button_OKClick(object sender, RoutedEventArgs e)
{

TotalValue = float.Parse(textBox.Text);
this.Close();
}

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);
W01.ShowDialog();

 

 

Comments

  1. Pingback: writeessay

Leave a Reply