Quantcast
Channel: Windows Presentation Foundation (WPF) forum
Viewing all articles
Browse latest Browse all 18858

Integer and Floating TextBox in WPF

$
0
0

Hi,

I tried developing a UserControl TextBox that accepts Integer and floating point numbers by changing the border color when a non integer or floating point numbers are entered. But the Border cannot be changed to normal color after proper value is inserted.

My User Control code and .cs file are as follows

<UserControl x:Class="Billing.User_Controls_View.IntegerText"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
             mc:Ignorable="d" Height="24" Width="337"><TextBox Name="txt" ></TextBox></UserControl>

 public partial class IntegerText : UserControl
    {
        public IntegerText()
        {
            InitializeComponent();
            txt.TextChanged += txt_TextChanged;
            txt.GotFocus += txt_GotFocus;

        }


        public string Text
        {
            get {return txt.Text;}
            set { txt.Text = value; }
        }


        void txt_GotFocus(object sender, RoutedEventArgs e)
        {
            Background = Brushes.Yellow;
        }

        void txt_TextChanged(object sender, TextChangedEventArgs e)
        {
            e.Handled = !IsFloatnumber();


        }
        bool IsFloatnumber()
        {
            float number = 0;
            if ((float.TryParse(txt.Text, out number)))
                return false;
            else
                return true;
        }
    }

Please suggest any alternate solution for this,

Thanks,

Shreyas M


Viewing all articles
Browse latest Browse all 18858

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>