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. Pattern Matching in C#

Pattern Matching in C#

Scheduled Pinned Locked Moved C#
csharpregexquestion
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.
  • M Offline
    M Offline
    M J Jaya Chitra
    wrote on last edited by
    #1

    I want to have a textbox in which the user can enter only nos. In vb.net we can match the pattern by using the like operator followe by the preferences (e.keyChar Like "[0-9.]"). But how can i do it in C# Plz give me a hint.

    Best Regards, M. J. Jaya Chitra

    C 1 Reply Last reply
    0
    • M M J Jaya Chitra

      I want to have a textbox in which the user can enter only nos. In vb.net we can match the pattern by using the like operator followe by the preferences (e.keyChar Like "[0-9.]"). But how can i do it in C# Plz give me a hint.

      Best Regards, M. J. Jaya Chitra

      C Offline
      C Offline
      Christian Graus
      wrote on last edited by
      #2

      You can use a maskedtextbox, or use regex, or use char.IsDigit, etc, in a key pressed event handler. e.KeyCar LIKE "[0-9.]" is doing a regex pattern match, which is wasteful compared to char.IsDigit, but with different syntax, it works fine in C#.

      Christian Graus - Microsoft MVP - C++ Metal Musings - Rex and my new metal blog "I am working on a project that will convert a FORTRAN code to corresponding C++ code.I am not aware of FORTRAN syntax" ( spotted in the C++/CLI forum )

      K 1 Reply Last reply
      0
      • C Christian Graus

        You can use a maskedtextbox, or use regex, or use char.IsDigit, etc, in a key pressed event handler. e.KeyCar LIKE "[0-9.]" is doing a regex pattern match, which is wasteful compared to char.IsDigit, but with different syntax, it works fine in C#.

        Christian Graus - Microsoft MVP - C++ Metal Musings - Rex and my new metal blog "I am working on a project that will convert a FORTRAN code to corresponding C++ code.I am not aware of FORTRAN syntax" ( spotted in the C++/CLI forum )

        K Offline
        K Offline
        KKrista
        wrote on last edited by
        #3

        How about using RegularExpressionValidator control of C#? -- modified at 4:25 Tuesday 5th June, 2007

        Regards, Murali

        C 1 Reply Last reply
        0
        • K KKrista

          How about using RegularExpressionValidator control of C#? -- modified at 4:25 Tuesday 5th June, 2007

          Regards, Murali

          C Offline
          C Offline
          Christian Graus
          wrote on last edited by
          #4

          That does something entirely different, a regular expression validator tells you if your input was formatted correctly. He wants the control to not allow invalid input.

          Christian Graus - Microsoft MVP - C++ Metal Musings - Rex and my new metal blog "I am working on a project that will convert a FORTRAN code to corresponding C++ code.I am not aware of FORTRAN syntax" ( spotted in the C++/CLI forum )

          M 1 Reply Last reply
          0
          • C Christian Graus

            That does something entirely different, a regular expression validator tells you if your input was formatted correctly. He wants the control to not allow invalid input.

            Christian Graus - Microsoft MVP - C++ Metal Musings - Rex and my new metal blog "I am working on a project that will convert a FORTRAN code to corresponding C++ code.I am not aware of FORTRAN syntax" ( spotted in the C++/CLI forum )

            M Offline
            M Offline
            M J Jaya Chitra
            wrote on last edited by
            #5

            I have to allow only the nos so I tried by checking the keyvalue so it is checking fine but it should not be allowed to be in the textbox know but whenever i am setting e.keyvalue=0, it is giving that it is a read only property, so plz give me a hint how to disable the invalid characters and to allow only nos and .(dot).

            Best Regards, M. J. Jaya Chitra

            C 1 Reply Last reply
            0
            • M M J Jaya Chitra

              I have to allow only the nos so I tried by checking the keyvalue so it is checking fine but it should not be allowed to be in the textbox know but whenever i am setting e.keyvalue=0, it is giving that it is a read only property, so plz give me a hint how to disable the invalid characters and to allow only nos and .(dot).

              Best Regards, M. J. Jaya Chitra

              C Offline
              C Offline
              Christian Graus
              wrote on last edited by
              #6

              if (!char.IsDigit(e.KeyChar) && e.KeyChar != '.') { e.Handled = true; } Something like that. setting handled to true stops the base class from handling the key press ( so it is then ignored )

              Christian Graus - Microsoft MVP - C++ Metal Musings - Rex and my new metal blog "I am working on a project that will convert a FORTRAN code to corresponding C++ code.I am not aware of FORTRAN syntax" ( spotted in the C++/CLI forum )

              M 1 Reply Last reply
              0
              • C Christian Graus

                if (!char.IsDigit(e.KeyChar) && e.KeyChar != '.') { e.Handled = true; } Something like that. setting handled to true stops the base class from handling the key press ( so it is then ignored )

                Christian Graus - Microsoft MVP - C++ Metal Musings - Rex and my new metal blog "I am working on a project that will convert a FORTRAN code to corresponding C++ code.I am not aware of FORTRAN syntax" ( spotted in the C++/CLI forum )

                M Offline
                M Offline
                M J Jaya Chitra
                wrote on last edited by
                #7

                Thank you so much it is working well and good. Once again Thank you

                Best Regards, M. J. Jaya Chitra

                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