How do I add thousand marks in a databound TextBox ? RRS feed

  • Question

  • Hello, a TextBox like this [ 1000000] doesn't look user friendly to users. I want to show [ 1,000,000], I can add thousand marks to TextBoxes after data is read from a database. But I want to do it in my custom TextBoxe events so that is is done automatically. Is there a "getting data" event in a TextBox ? I can add thousand marks on this event. Also, I need to delete thousand marks when the TextBox got Entered. And thousand marks are added again on the Leave event.

    Regular expressions are useful in this case because if the users didn't type anything, marks will be saved to database. I tried that many years ago. I set (   )_______ as for telephone number textboxes. ()________ is saved to database and that is meaningless.

    Thank you.

    Wednesday, March 28, 2018 7:16 AM


  • Hello,

    Try setting up a binding on the TextBox Text property. In the following example there is a BindingSource, the formatting does not require this as we could bind to the DataTable instead yet usually when binding to data say that came from a database it's prudent to use a BindingSource to traverse data.

    using System;
    using System.Data;
    using System.Globalization;
    using System.Windows.Forms;
    namespace WindowsFormsApp1
        public partial class Form1 : Form
            private Binding _binding1;
            private BindingSource _bs = new BindingSource();
            private string _currSymbol = CultureInfo.CurrentCulture.NumberFormat.CurrencySymbol;
            public Form1()
                Shown += Form1_Shown;
            private void Form1_Shown(object sender, EventArgs e)
                DataTable dt = new DataTable();
                dt.Columns.Add("DecimalOne", typeof(decimal));
                dt.Rows.Add(new object[] { 100 });
                _bs.DataSource = dt;
                txtDecimalOne.DataBindings.Add("Text", _bs, "DecimalOne", false);
                _binding1 = txtDecimalOne.DataBindings["Text"];
                _binding1.Format += DecimalPlaces_Format;
                txtDecimalOne.Enter += TxtDecimalOne_Enter;
            private void TxtDecimalOne_Enter(object sender, EventArgs e)
                if (txtDecimalOne.Text.StartsWith(_currSymbol))
                    txtDecimalOne.Text = txtDecimalOne.Text.Replace(_currSymbol, "");
            private void DecimalPlaces_Format(object sender, ConvertEventArgs e)
                if (!(Convert.IsDBNull(e.Value)))
                    e.Value = Convert.ToDecimal(e.Value).ToString("C2");

    If you are using a data source say from a TableAdapter you could use the alternate to get to the formatting.

    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    Wednesday, March 28, 2018 9:50 AM