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. Masked Textbox Prompt

Masked Textbox Prompt

Scheduled Pinned Locked Moved C#
helpquestiontutorial
6 Posts 2 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.
  • L Offline
    L Offline
    Lodeclaw
    wrote on last edited by
    #1

    Hello! I've been having some issues with masked textboxes and have been unable to find a solution, so I hope one of you knowledgeable forumers can point me in the right direction. Let's say for example I have a masked textbox and I want to restrict it to a 4digit numeric-only ID#. The Mask is set as "0000" and the PromptChar is "_". My problem arises when I run the project and enter the textbox. The | is placed at the end of the Prompt "____" and in order to input the data I want properly I need to backspace, key over, or click to the beginning of the string. How can I have a masked textbox always place the data entry marker (what's it called?) at the beginning of the Prompt string when the textbox becomes the selected control? Any help would be hugely appreciated. Thanks in advance!

    N 1 Reply Last reply
    0
    • L Lodeclaw

      Hello! I've been having some issues with masked textboxes and have been unable to find a solution, so I hope one of you knowledgeable forumers can point me in the right direction. Let's say for example I have a masked textbox and I want to restrict it to a 4digit numeric-only ID#. The Mask is set as "0000" and the PromptChar is "_". My problem arises when I run the project and enter the textbox. The | is placed at the end of the Prompt "____" and in order to input the data I want properly I need to backspace, key over, or click to the beginning of the string. How can I have a masked textbox always place the data entry marker (what's it called?) at the beginning of the Prompt string when the textbox becomes the selected control? Any help would be hugely appreciated. Thanks in advance!

      N Offline
      N Offline
      Nagy Vilmos
      wrote on last edited by
      #2

      use the SelectionStart property to set the position of the cursor (with nothing selected)


      Panic, Chaos, Destruction. My work here is done.

      L 1 Reply Last reply
      0
      • N Nagy Vilmos

        use the SelectionStart property to set the position of the cursor (with nothing selected)


        Panic, Chaos, Destruction. My work here is done.

        L Offline
        L Offline
        Lodeclaw
        wrote on last edited by
        #3

        It baffles me why this property isn't actually IN the properties list for masked textboxes! Thanks william. Am I going to have to use an Enter event for every masked textbox in order to do this?

        N 1 Reply Last reply
        0
        • L Lodeclaw

          It baffles me why this property isn't actually IN the properties list for masked textboxes! Thanks william. Am I going to have to use an Enter event for every masked textbox in order to do this?

          N Offline
          N Offline
          Nagy Vilmos
          wrote on last edited by
          #4

          I don't know why the control can't have some sane behaviour. It is rather pants, btu there you go. Try writting a single Enter event handler and using it to for all your controls.


          Panic, Chaos, Destruction. My work here is done.

          L 1 Reply Last reply
          0
          • N Nagy Vilmos

            I don't know why the control can't have some sane behaviour. It is rather pants, btu there you go. Try writting a single Enter event handler and using it to for all your controls.


            Panic, Chaos, Destruction. My work here is done.

            L Offline
            L Offline
            Lodeclaw
            wrote on last edited by
            #5

            private void txtCustID_Enter(object sender, EventArgs e)
            {
            txtCustID.SelectionStart = 0;
            }

            Am I using the property incorrectly? I'm still having the same problem.

            N 1 Reply Last reply
            0
            • L Lodeclaw

              private void txtCustID_Enter(object sender, EventArgs e)
              {
              txtCustID.SelectionStart = 0;
              }

              Am I using the property incorrectly? I'm still having the same problem.

              N Offline
              N Offline
              Nagy Vilmos
              wrote on last edited by
              #6

              You are using the property well, but on checking back you have the wrong event:

                  private void maskedEdit\_GotFocus(object sender, EventArgs e)
                  {
                      MaskedTextBox mtb = (MaskedTextBox)sender;
                      mtb.SelectionStart = 0;
                  }
              

              It appears that after the Enter event, the selection is set to the previous values. But if you use GotFocus it works. Just add the event handler manually after InitializeComponent in the constructor:

                      InitializeComponent();
                      maskedTextBox1.GotFocus += new EventHandler(this.maskedEdit\_GotFocus);
                      maskedTextBox2.GotFocus += new EventHandler(this.maskedEdit\_GotFocus);
              

              Panic, Chaos, Destruction. My work here is done.

              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