Cuxstom Format Masked Text Box

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • CoachBarker
    New Member
    • Jul 2007
    • 18

    Cuxstom Format Masked Text Box

    I have a masked text box that needs to be formatted as

    Three letters a period three numbers

    ABC.123

    in VB.net Visual Studio 2005

    any help would be greatly appreciated.

    CoachBarker
  • TRScheel
    Recognized Expert Contributor
    • Apr 2007
    • 638

    #2
    Originally posted by CoachBarker
    I have a masked text box that needs to be formatted as

    Three letters a period three numbers

    ABC.123

    in VB.net Visual Studio 2005

    any help would be greatly appreciated.

    CoachBarker
    Why not just have two textboxes that only allow 3 characters, have one that accepts alpha characters, the other only numbers, and seperate them by a label that has only the '.' in it.

    [TEXTBOX1].[TEXTBOX2]

    Comment

    • CoachBarker
      New Member
      • Jul 2007
      • 18

      #3
      Originally posted by TRScheel
      Why not just have two textboxes that only allow 3 characters, have one that accepts alpha characters, the other only numbers, and seperate them by a label that has only the '.' in it.

      [TEXTBOX1].[TEXTBOX2]

      The text box is used to list the Name of a Course that is being stored in database. Don't want to go to the extreme of concatenating the string to pass it to the data base

      Comment

      • Plater
        Recognized Expert Expert
        • Apr 2007
        • 7872

        #4
        Is this a web or windows application?
        Windows is easy with the maskedtextbox, web is a bit more tricky

        Comment

        • TRScheel
          Recognized Expert Contributor
          • Apr 2007
          • 638

          #5
          Originally posted by CoachBarker
          The text box is used to list the Name of a Course that is being stored in database. Don't want to go to the extreme of concatenating the string to pass it to the data base
          Well there are two other methods I have for you then. Either:

          A) Catch each letter coming and going from the textbox, and if it doesnt fit the pattern, dissallow it (or inform the user somehow). Something like:

          [code=cpp]
          void TextBox1_TextCh anged(object sender, EventArgs e)
          {
          for (int i = 0; i < 3 && i < ((TextBox)sende r).Text.Length; i++)
          {
          if (!IsAlpha(((Tex tBox)sender).Te xt[i]))
          {
          InformUserOfBad Character(i);
          }
          }

          if (((TextBox)send er).Text.Length > 3)
          {
          if (((TextBox)send er).Text[3] != '.')
          {
          InformUserOfBad Character(3);
          }
          }

          for (int i = 4; i < 7 && i < ((TextBox)sende r).Text.Length; i++)
          {
          if (!IsNumeric(((T extBox)sender). Text[i]))
          {
          InformUserOfBad Character(i);
          }
          }
          }

          static bool IsAlpha(char test)
          {
          return (test >= 65 && test <= 90) || (test >= 97 && test <= 122);
          }

          static bool IsNumeric(char test)
          {
          return test >= 48 && test <= 57;
          }
          [/code]


          or

          B) Create a property that fakes the two text box's values in the example I gave above. Something like:

          [code=cpp]
          public string CourseName
          {
          get { return string.Format(" {0}.{1}", TextBox1.Text, TextBox2.Text); }
          }
          [/code]

          Setting it would run through the same difficulties as method A. You would need to make sure the value is of valid size, and then of valid input. If not, I would probably throw an exception to be handled else where.

          Comment

          • CoachBarker
            New Member
            • Jul 2007
            • 18

            #6
            Originally posted by Plater
            Is this a web or windows application?
            Windows is easy with the maskedtextbox, web is a bit more tricky
            This is a windows application.

            Comment

            • Plater
              Recognized Expert Expert
              • Apr 2007
              • 7872

              #7
              Originally posted by CoachBarker
              This is a windows application.
              Well then good news!


              The MaskedTextBox is your friend

              Comment

              • TRScheel
                Recognized Expert Contributor
                • Apr 2007
                • 638

                #8
                Originally posted by Plater
                Well then good news!


                The MaskedTextBox is your friend
                Never used the masktextbox, but I suppose that with this he could use:

                [code=cpp]
                MyMaskedTextBox .Mask = "LLL.000";
                [/code]

                Comment

                • CoachBarker
                  New Member
                  • Jul 2007
                  • 18

                  #9
                  Originally posted by TRScheel
                  Never used the masktextbox, but I suppose that with this he could use:

                  [code=cpp]
                  MyMaskedTextBox .Mask = "LLL.000";
                  [/code]
                  Thank you all for your help I ended up formatting it this way
                  >LLL.000 and the greter than sign means that the letters are all captilized

                  Thanks
                  CoachBarker

                  Comment

                  Working...