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

  • SEARCH
  • Home
  • 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 9160931
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T13:51:12+00:00 2026-06-17T13:51:12+00:00

In my window application I need masked textbox which accept real decmal numbers. eg.

  • 0

In my window application I need masked textbox which accept real decmal numbers.

eg.
1) 1.56
2) 22.34
3) 123.34
4) 12312.34
This all value should be valid.

Can anyone tell me how can I do this?
And ya if anyone have better solution for real decimal numbers, instead of this masked TextBox
than I love to see it.
Thanks…

  • 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-17T13:51:14+00:00Added an answer on June 17, 2026 at 1:51 pm

    Use a custom control like this one (modify it to fulfill your needs):

    using System; 
    using System.ComponentModel; 
    using System.Text; 
    using System.Windows.Forms; 
    using System.Drawing; 
    
    namespace CustomControls 
    { 
        public enum PasteRejectReasons 
        { 
            Unknown = 0, 
            NoData, 
            InvalidCharacter, 
            Accepted 
        } 
    
        public class DecimalTextBox : TextBox 
        { 
            public const int WM_PASTE = 0x0302; 
            public event EventHandler<KeyRejectedEventArgs> KeyRejected; 
            public event EventHandler<PasteEventArgs> PasteRejected; 
    
            private bool _DecimalSeparator = false; 
            private int _Precision; 
    
            public new HorizontalAlignment TextAlign 
            { 
                get { return base.TextAlign; } 
                set { base.TextAlign = value; } 
            } 
    
            public int Precision 
            { 
                get { return _Precision; } 
                set { _Precision = value; } 
            } 
    
            public DecimalTextBox() 
            { 
                TextAlign = HorizontalAlignment.Right; 
                Precision = 3; 
            } 
    
            protected override void OnGotFocus(EventArgs e) 
            { 
                SelectAll(); 
                base.OnGotFocus(e); 
            } 
    
            protected override void OnKeyDown(KeyEventArgs e) 
            { 
                bool validate = true; 
    
                if (Text.Contains(".") || Text.Contains(",")) 
                { 
                    int indexSep; 
                    string[] split; 
                    string partiDecimal = ""; 
    
                    if (Text.Contains(".")) 
                        indexSep = Text.IndexOf('.'); 
                    else 
                        indexSep = Text.IndexOf(','); 
    
                    split = Text.Split(new char[] { ',', '.' }); 
                    partiDecimal += split[1]; 
    
                    if (partiDecimal.Length >= Precision) 
                        if (SelectionStart > Text.Length - (partiDecimal.Length + 1)) 
                            validate = false; 
                } 
    
                bool result = true; 
    
                bool validateKeys = (e.KeyCode == Keys.Enter); 
    
                bool numericKeys = ( 
                    ((e.KeyCode >= Keys.D0 && e.KeyCode <= Keys.D9) || 
                    (e.KeyCode >= Keys.NumPad0 && e.KeyCode <= Keys.NumPad9)) 
                    && e.Modifiers != Keys.Shift 
                    && validate); 
    
                bool ctrlA = e.KeyCode == Keys.A && e.Modifiers == Keys.Control; 
    
                bool editKeys = ( 
                    (e.KeyCode == Keys.Z && e.Modifiers == Keys.Control) || 
                    (e.KeyCode == Keys.X && e.Modifiers == Keys.Control) || 
                    (e.KeyCode == Keys.C && e.Modifiers == Keys.Control) || 
                    (e.KeyCode == Keys.V && e.Modifiers == Keys.Control) || 
                    e.KeyCode == Keys.Delete || 
                    e.KeyCode == Keys.Back); 
    
                bool navigationKeys = ( 
                    e.KeyCode == Keys.Up || 
                    e.KeyCode == Keys.Right || 
                    e.KeyCode == Keys.Down || 
                    e.KeyCode == Keys.Left || 
                    e.KeyCode == Keys.Home || 
                    e.KeyCode == Keys.End); 
    
                bool decimalSeparator = (( 
                    e.KeyCode == Keys.Decimal || 
                    e.KeyValue == 190 || 
                    e.KeyValue == 188)&& 
                    TextLength != 0 && 
                    SelectionLength == 0); 
    
                if (decimalSeparator)  
                { 
                    if (!_DecimalSeparator) 
                        _DecimalSeparator = true; 
                    else 
                        decimalSeparator = false; 
                } 
    
                if (!(numericKeys || editKeys || navigationKeys || decimalSeparator || validateKeys)) 
                { 
                    if (ctrlA)
                        SelectAll();
    
                    result = false; 
                } 
    
                if (!result)
                { 
                    e.SuppressKeyPress = true; 
                    e.Handled = true; 
    
                    if (!ctrlA)
                        OnKeyRejected(new KeyRejectedEventArgs(e.KeyCode)); 
                } 
                else 
                    base.OnKeyDown(e); 
            } 
    
            protected override void OnKeyPress(KeyPressEventArgs e) 
            { 
                if (e.KeyChar == ';' || e.KeyChar == '?') 
                { 
                    if (!(Text.Contains(",") || Text.Contains("."))) 
                        _DecimalSeparator = false; 
                    e.Handled = true; 
                } 
            } 
    
            protected override void OnTextChanged(EventArgs e) 
            { 
                bool invalid = false; 
                int i = 0; 
                foreach (char c in Text) // Check for any non digit characters. 
                { 
                    if (!(char.IsDigit(c) || c == ',' || c == '.')) 
                    { 
                        invalid = true; 
                        break; 
                    } 
    
                    if (c == ',' || c == '.') 
                        i++; 
                } 
    
                if (i == 0) 
                    _DecimalSeparator = false; 
                else if (i > 1) 
                    invalid = true; 
    
                if (invalid) 
                { 
                    Text = ""; 
                    return; 
                } 
    
                if (Text.Contains(".") || Text.Contains(",")) 
                { 
                    string charSep = ""; 
                    string[] split; 
                    string partiEntier = ""; 
    
                    if (Text.Contains(".")) 
                        charSep = "."; 
                    else 
                        charSep = ","; 
    
                    split = Text.Split(new char[] { ',', '.' }); 
                    partiEntier += split[0]; 
                    if (partiEntier == "") 
                        Text = "0" + charSep + split[1]; 
                } 
                base.OnTextChanged(e); 
            } 
    
            protected override void WndProc(ref Message m) 
            { 
                if (m.Msg == WM_PASTE) 
                { 
                    PasteEventArgs e = CheckPasteValid(); 
                    if (e.RejectReason != PasteRejectReasons.Accepted) 
                    { 
                        m.Result = IntPtr.Zero; 
                        OnPasteRejected(e); 
                        return; 
                    } 
                } 
                base.WndProc(ref m); 
            } 
    
            private PasteEventArgs CheckPasteValid() 
            { 
                PasteRejectReasons rejectReason = PasteRejectReasons.Accepted; 
                string originalText = Text; 
                string clipboardText = string.Empty; 
                string textResult = string.Empty; 
    
                try 
                { 
                    clipboardText = Clipboard.GetText(TextDataFormat.Text); 
                    if (clipboardText.Length > 0)
                    { 
    
                        textResult = ( 
                            Text.Remove(SelectionStart, SelectionLength).Insert(SelectionStart, clipboardText)); 
                        foreach (char c in clipboardText)
                        { 
                            if (!char.IsDigit(c)) 
                            { 
                                rejectReason = PasteRejectReasons.InvalidCharacter; 
                                break; 
                            } 
                        } 
                    } 
                    else 
                        rejectReason = PasteRejectReasons.NoData; 
                } 
                catch 
                { 
                    rejectReason = PasteRejectReasons.Unknown; 
                } 
                return new PasteEventArgs(originalText, clipboardText, textResult, rejectReason); 
            } 
    
            protected virtual void OnKeyRejected(KeyRejectedEventArgs e) 
            { 
                EventHandler<KeyRejectedEventArgs> handler = KeyRejected; 
                if (handler != null) 
                    handler(this, e); 
            } 
    
            protected virtual void OnPasteRejected(PasteEventArgs e) 
            { 
                EventHandler<PasteEventArgs> handler = PasteRejected; 
                if (handler != null) 
                    handler(this, e); 
            } 
        } 
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a non-fullscreen DirectX window displaying video, which I for application purposes need
I have developed a window application in VS2005 using C#. I need to integrate
For my web application I need to close the child window whenever the parent
I need to get the Handler to the child Window of a certain application
I'm writing a Python+Qt4 application that would ideally need to pop up a window
In my Windows Phone application I need to accept an untrusted certificate by default
In my application i need to create HBITMAP objects to which I render and
i am using .net window application . i need to make my form UI
I need to multicast an x11 application window to multiple (sai 100) clients in
I need to open popup window in my flex / air application that will

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.