Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8681257
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T21:27:41+00:00 2026-06-12T21:27:41+00:00

I am working on WPF app in which I have a textbox, Button, Label

  • 0

I am working on WPF app in which I have a textbox, Button, Label & a toggle Button. In my application, I should perform two operations as follows:

  1. Set the input of textbox only between 0.0 to 5.0. Thus if any value written which exceeds 5.0, must be restricted to 5. Similarly any value written below 0.0 must be restricted and only 0.0 must be displayed.

  2. My label should display content as “value” V. I.e. if value is 3.4 V as volts should be concatenated with it.

Here Goes The Xaml:

<TextBox Grid.Column="1" Text="{Binding VoltageText}" Name="VoltageBox" />        
<Label Grid.Column="2" Content="{Binding CurrentText}" Name="CurrentLabel" />

Model Class:

string voltageText = string.Empty;
    public string VoltageText
    {
        get
        {
            return voltageText;
        }

        set
        {
            voltageText = value;
            OnPropertyChanged("VoltageText");
        }
    }

This textbox must have input restrictions as mentioned in my first points 🙂 Here goes the textchanged method I wrote to allow only 0-9 and . in textbox:

// present in xaml.cs
private void VoltageBox_TextChanged(object sender, TextChangedEventArgs e)
    {
        string txt = VoltageBox.Text;
        if (txt != "")
        {
            VoltageBox.Text = Regex.Replace(VoltageBox.Text, "[^0-9] [^.]", "");
            if (txt != VoltageBox.Text)
            {
                VoltageBox.Select(VoltageBox.Text.Length, 0);
            }
        }
    }

    string currentText = "0 V";
    public string CurrentText
    {
        get
        {
            return currentText;
        }

        set
        {
            currentText = value;
            OnPropertyChanged("CurrentText");
        }
    }     

You can notice on startup Currenttext = 0 V and by executing these statements I want to change the value of the CurrentText:

double tempval = 120.0;

string CurrentVal = Convert.ToString(tempval / 10);
CurrentText = CurrentVal; // here V as volts must be concatenated and value must be 12.0 V
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T21:27:43+00:00Added an answer on June 12, 2026 at 9:27 pm

    Here is a rough way on how to do things.

    First on your ViewModel, have one property that is your voltage effective value, and a calculated property that will add a “V” to it. It’s the ViewModel responsability to do these kind of thing, not the View one.

        private double voltage = 0.0D;
        public double Voltage
        {
            get
            {
                return voltage;
            }
    
            set
            {
                if (value > 5.0D || value < 0.0D)
                    throw new InvalidOperationException();
                voltage = value;
                OnPropertyChanged("Voltage");
            }
        }
    
        public string VoltageText
        {
            get
            {
                return Voltage + " V";
            }
        }
    

    Then for your control, don’t use TextChanged, prefer using PreviewTextInput

    In your waml :

        <TextBox Grid.Column="1" Text="{Binding Voltage}" Name="VoltageBox" PreviewTextInput="TextBox_PreviewTextInput" />
        <Label Grid.Column="2" Content="{Binding VoltageText}" Name="CurrentLabel" />
    

    In code behind

        private void TextBox_PreviewTextInput(object sender, TextCompositionEventArgs e)
        {
            var textBox = sender as TextBox;
            if (textBox == null)
                throw new ArgumentException("sender");
    
            double result;
            var insertedText = e.Text;
            if (!double.TryParse(insertedText, out result) && insertedText != ".")
            {
                e.Handled = true;
            }
            else
            {
                string textboxCurrentValue = textBox.Text;
                int selectionLength = textBox.SelectionLength;
                int selectionStart = textBox.SelectionStart;
                if (selectionLength != 0)
                {
                    textboxCurrentValue = textboxCurrentValue.Remove(selectionStart, selectionLength);
                }
                textboxCurrentValue = textboxCurrentValue.Insert(selectionStart, insertedText);
    
                if (!double.TryParse(textboxCurrentValue, out result) || result > 5.0D || result < 0.0D)
                {
                    e.Handled = true;
                }
            }
        }
    

    This should get you started !

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a working database application using WPF and SQL Server 2008 R2, which
I have a working WPF app using MVVM pattern in which the ViewModel calls
I am new to WPF and .net. I have a WPF app which someone
I have a WPF 4 app which I want to enable drag and drop
I have a WPF application which uses a JumpList (Recent only). Everything works perfectly
I have a WPF application which so far has been client only, but now
I'm working on a wpf c# app and I have a question. I have
I am working on an app named E-Diary in WPF. Now I have to
I am working on a WPF application which doesnot seem to release all the
We have a working WPF app that we are looking into running in the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.