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. [Message Deleted]

[Message Deleted]

Scheduled Pinned Locked Moved C#
12 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.
  • H hkjghkj1

    [Message Deleted]

    OriginalGriffO Offline
    OriginalGriffO Offline
    OriginalGriff
    wrote on last edited by
    #3

    Why not use the built in methods?

    if (e.KeyChar == 13)
    {
    if (Test1.Text.IndexOfAny(new char[]{'@', '.', ',', '!'}) < 0)
    {
    MessageBox.Show("The name can' t hold '.', '!', '[]'", "Info", 0, MessageBoxIcon.Error);
    return;
    }
    if (Text1.Text != "")
    {
    ListBox1.Items.Add(Text1.Text);
    }
    Text1.Text = "";
    }

    No trees were harmed in the sending of this message; however, a significant number of electrons were slightly inconvenienced. This message is made of fully recyclable Zeros and Ones

    "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
    "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

    H 1 Reply Last reply
    0
    • L Luc Pattyn

      :confused: execute your code in your mind assuming the textbox holds "abc", for two iterations, and you will know. :)

      Luc Pattyn [Forum Guidelines] [My Articles]


      Avoiding unwanted divs (as in "articles needing approval") with the help of this FireFox add-in


      OriginalGriffO Offline
      OriginalGriffO Offline
      OriginalGriff
      wrote on last edited by
      #4

      Or - and this is just a suggestion, so I'll whisper - use the debugger You might find it helps...

      No trees were harmed in the sending of this message; however, a significant number of electrons were slightly inconvenienced. This message is made of fully recyclable Zeros and Ones

      "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
      "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

      L 1 Reply Last reply
      0
      • OriginalGriffO OriginalGriff

        Or - and this is just a suggestion, so I'll whisper - use the debugger You might find it helps...

        No trees were harmed in the sending of this message; however, a significant number of electrons were slightly inconvenienced. This message is made of fully recyclable Zeros and Ones

        L Offline
        L Offline
        Luc Pattyn
        wrote on last edited by
        #5

        I'll pretend I didn't hear that. IMO debuggers are fine for solving hard problems, just looking at the code and executing it mentally is what always has to come first. :)

        Luc Pattyn [Forum Guidelines] [My Articles]


        Avoiding unwanted divs (as in "articles needing approval") with the help of this FireFox add-in


        OriginalGriffO 1 Reply Last reply
        0
        • H hkjghkj1

          [Message Deleted]

          L Offline
          L Offline
          Luc Pattyn
          wrote on last edited by
          #6

          why test after the unwanted characters have been added to the text? why not check them on entry, i.e. use the KeyDown event and refuse the characters. Or even override the IsInputChar() method. :)

          Luc Pattyn [Forum Guidelines] [My Articles]


          Avoiding unwanted divs (as in "articles needing approval") with the help of this FireFox add-in


          OriginalGriffO 1 Reply Last reply
          0
          • L Luc Pattyn

            I'll pretend I didn't hear that. IMO debuggers are fine for solving hard problems, just looking at the code and executing it mentally is what always has to come first. :)

            Luc Pattyn [Forum Guidelines] [My Articles]


            Avoiding unwanted divs (as in "articles needing approval") with the help of this FireFox add-in


            OriginalGriffO Offline
            OriginalGriffO Offline
            OriginalGriff
            wrote on last edited by
            #7

            I know what you mean, but sometimes I can stare at code and only see what I meant to write!

            No trees were harmed in the sending of this message; however, a significant number of electrons were slightly inconvenienced. This message is made of fully recyclable Zeros and Ones

            "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
            "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

            1 Reply Last reply
            0
            • L Luc Pattyn

              why test after the unwanted characters have been added to the text? why not check them on entry, i.e. use the KeyDown event and refuse the characters. Or even override the IsInputChar() method. :)

              Luc Pattyn [Forum Guidelines] [My Articles]


              Avoiding unwanted divs (as in "articles needing approval") with the help of this FireFox add-in


              OriginalGriffO Offline
              OriginalGriffO Offline
              OriginalGriff
              wrote on last edited by
              #8

              Complicated - what happens if the user pastes it in?

              No trees were harmed in the sending of this message; however, a significant number of electrons were slightly inconvenienced. This message is made of fully recyclable Zeros and Ones

              "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
              "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

              L 1 Reply Last reply
              0
              • OriginalGriffO OriginalGriff

                Complicated - what happens if the user pastes it in?

                No trees were harmed in the sending of this message; however, a significant number of electrons were slightly inconvenienced. This message is made of fully recyclable Zeros and Ones

                L Offline
                L Offline
                Luc Pattyn
                wrote on last edited by
                #9

                Correct, one actually needs both kinds of validation to get immediate feedback. IMO issuing a bad character message at the end of a line isn't part of a good UI. :)

                Luc Pattyn [Forum Guidelines] [My Articles]


                Avoiding unwanted divs (as in "articles needing approval") with the help of this FireFox add-in


                OriginalGriffO 1 Reply Last reply
                0
                • L Luc Pattyn

                  Correct, one actually needs both kinds of validation to get immediate feedback. IMO issuing a bad character message at the end of a line isn't part of a good UI. :)

                  Luc Pattyn [Forum Guidelines] [My Articles]


                  Avoiding unwanted divs (as in "articles needing approval") with the help of this FireFox add-in


                  OriginalGriffO Offline
                  OriginalGriffO Offline
                  OriginalGriff
                  wrote on last edited by
                  #10

                  Luc Pattyn wrote:

                  issuing a bad character message at the end of a line isn't part of a good UI.

                  That throws most of t'interweb in the bin, then...

                  No trees were harmed in the sending of this message; however, a significant number of electrons were slightly inconvenienced. This message is made of fully recyclable Zeros and Ones

                  "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
                  "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

                  1 Reply Last reply
                  0
                  • OriginalGriffO OriginalGriff

                    Why not use the built in methods?

                    if (e.KeyChar == 13)
                    {
                    if (Test1.Text.IndexOfAny(new char[]{'@', '.', ',', '!'}) < 0)
                    {
                    MessageBox.Show("The name can' t hold '.', '!', '[]'", "Info", 0, MessageBoxIcon.Error);
                    return;
                    }
                    if (Text1.Text != "")
                    {
                    ListBox1.Items.Add(Text1.Text);
                    }
                    Text1.Text = "";
                    }

                    No trees were harmed in the sending of this message; however, a significant number of electrons were slightly inconvenienced. This message is made of fully recyclable Zeros and Ones

                    H Offline
                    H Offline
                    hkjghkj1
                    wrote on last edited by
                    #11

                    [Message Deleted]

                    OriginalGriffO 1 Reply Last reply
                    0
                    • H hkjghkj1

                      [Message Deleted]

                      OriginalGriffO Offline
                      OriginalGriffO Offline
                      OriginalGriff
                      wrote on last edited by
                      #12

                      Yep. Now, I could say I left that as an exercise for the reader? Or just admit it: I made a mistake - sorry! Also, to work with your original code it needs to be:

                      if (e.KeyChar == 13)
                      {
                      if (Test1.Text.IndexOfAny(new char[]{'.', '!', '[', ']'}) >= 0)
                      {
                      MessageBox.Show("The name can't hold '.', '!', '[]'", "Info", 0, MessageBoxIcon.Error);
                      return;
                      }
                      if (Text1.Text != "")
                      {
                      ListBox1.Items.Add(Text1.Text);
                      }
                      Text1.Text = "";
                      }

                      because I added comma and atsign, and forgot your square brackets as well. :-O

                      No trees were harmed in the sending of this message; however, a significant number of electrons were slightly inconvenienced. This message is made of fully recyclable Zeros and Ones

                      "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
                      "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

                      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