Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Mask the TextBox

Mask the TextBox

Scheduled Pinned Locked Moved C#
question
7 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • Z Offline
    Z Offline
    zaboboa
    wrote on last edited by
    #1

    Hello, I would like to mask the text box such that it would only be for the money amounts, and it would have $ sign in front. So for input in textbox: 4500 Masked to: $ 4,500 Any ideas? Thank you.

    B M 2 Replies Last reply
    0
    • Z zaboboa

      Hello, I would like to mask the text box such that it would only be for the money amounts, and it would have $ sign in front. So for input in textbox: 4500 Masked to: $ 4,500 Any ideas? Thank you.

      B Offline
      B Offline
      BammBamm
      wrote on last edited by
      #2

      You could just format the number before you display it. double x = 4500.00; textBox2.Text= x.ToString("c"); output = $4,500.00

      1 Reply Last reply
      0
      • Z zaboboa

        Hello, I would like to mask the text box such that it would only be for the money amounts, and it would have $ sign in front. So for input in textbox: 4500 Masked to: $ 4,500 Any ideas? Thank you.

        M Offline
        M Offline
        Mohamad Al Husseiny
        wrote on last edited by
        #3

        This the modification version of MaskedTextControl from User Interfaces in C# book

        using System;
        using System.Windows.Forms;
        public class MaskedTextBox : TextBox
        {
        	private string mask;
        	public string Mask
        	{
        		get
        		{
        			return mask;
        		}
        		set
        		{
        			mask = value;
        			this.Text = "";
        		}
        	}
        
        Protected override void OnKeyPress(KeyPressEventArgs e)
        	{
        if (Mask != "")
        {
        e.Handled = true;
        string newText = this.Text;
        bool finished = false;
        for (int i = this.SelectionStart; i < mask.Length; i++)
        {
        	switch (mask[i].ToString())
        	{
        	case "#" :
        	   if (Char.IsDigit(e.KeyChar) )
        	   {
        	      if(this.TextLength
        

        In the Form Load event add the following maskTextBox.Mask = "$###,###"; MCAD

        Z 1 Reply Last reply
        0
        • M Mohamad Al Husseiny

          This the modification version of MaskedTextControl from User Interfaces in C# book

          using System;
          using System.Windows.Forms;
          public class MaskedTextBox : TextBox
          {
          	private string mask;
          	public string Mask
          	{
          		get
          		{
          			return mask;
          		}
          		set
          		{
          			mask = value;
          			this.Text = "";
          		}
          	}
          
          Protected override void OnKeyPress(KeyPressEventArgs e)
          	{
          if (Mask != "")
          {
          e.Handled = true;
          string newText = this.Text;
          bool finished = false;
          for (int i = this.SelectionStart; i < mask.Length; i++)
          {
          	switch (mask[i].ToString())
          	{
          	case "#" :
          	   if (Char.IsDigit(e.KeyChar) )
          	   {
          	      if(this.TextLength
          

          In the Form Load event add the following maskTextBox.Mask = "$###,###"; MCAD

          Z Offline
          Z Offline
          zaboboa
          wrote on last edited by
          #4

          There seems to be an error: if(this.TextLength { in this line, the IF statement is not completed, or am I reading it incorectly? Is it: if (this.TextLength != 0) Another thing, to make the amount bigger, do I just increase the mask line: "$###,###,###....", or there is a better way? Thank you

          M 1 Reply Last reply
          0
          • Z zaboboa

            There seems to be an error: if(this.TextLength { in this line, the IF statement is not completed, or am I reading it incorectly? Is it: if (this.TextLength != 0) Another thing, to make the amount bigger, do I just increase the mask line: "$###,###,###....", or there is a better way? Thank you

            M Offline
            M Offline
            Mohamad Al Husseiny
            wrote on last edited by
            #5

            sorry some thing wrong was happend replace if(this.TextLength { with if(this.TextLength<mask.Length) This prevent the user form enter number longer than mask lenght **zaboboa wrote:** _Another thing, to make the amount bigger, do I just increase the mask line: "$###,###,###....", or there is a better way?_ Yes you cab write mask like $###,###,### or even $(###)(###),(###) **Note**:this quick code so test it before you use it and you may add another thibgs like backspace... MCAD

            Z 1 Reply Last reply
            0
            • M Mohamad Al Husseiny

              sorry some thing wrong was happend replace if(this.TextLength { with if(this.TextLength<mask.Length) This prevent the user form enter number longer than mask lenght **zaboboa wrote:** _Another thing, to make the amount bigger, do I just increase the mask line: "$###,###,###....", or there is a better way?_ Yes you cab write mask like $###,###,### or even $(###)(###),(###) **Note**:this quick code so test it before you use it and you may add another thibgs like backspace... MCAD

              Z Offline
              Z Offline
              zaboboa
              wrote on last edited by
              #6

              You know, it will work better if you put this statement insted if(this.TextLength == this.SelectionStart) Of course for this mask to be perfect you have to add a Leave event, in case the user leaves the box, with the input like that $455,6 Thank you for your help, it gave me a big jumpstart.

              M 1 Reply Last reply
              0
              • Z zaboboa

                You know, it will work better if you put this statement insted if(this.TextLength == this.SelectionStart) Of course for this mask to be perfect you have to add a Leave event, in case the user leaves the box, with the input like that $455,6 Thank you for your help, it gave me a big jumpstart.

                M Offline
                M Offline
                Mohamad Al Husseiny
                wrote on last edited by
                #7

                zaboboa wrote: Of course for this mask to be perfect you have to add a Leave event, in case the user leaves the box, with the input like that $455,6 And Copy and Paste there are many things you can add it Go on MCAD

                1 Reply Last reply
                0
                Reply
                • Reply as topic
                Log in to reply
                • Oldest to Newest
                • Newest to Oldest
                • Most Votes


                • Login

                • Don't have an account? Register

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • World
                • Users
                • Groups