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. How to paste only valid value in textbox.

How to paste only valid value in textbox.

Scheduled Pinned Locked Moved C#
tutorial
14 Posts 6 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.
  • N Nagy Vilmos

    Have you tried anything yet?


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

    S Offline
    S Offline
    S a n d y
    wrote on last edited by
    #3

    No, I do not understand how to do it?

    N 1 Reply Last reply
    0
    • S S a n d y

      No, I do not understand how to do it?

      N Offline
      N Offline
      N a v a n e e t h
      wrote on last edited by
      #4

      Check this[^]

      Navaneeth How to use google | Ask smart questions

      1 Reply Last reply
      0
      • S S a n d y

        Hi All, I want to paste only valid value(like numeric value) in textbox through mouse(right click) and keyboard(Ctrl+V). Thanks in advance.

        V Offline
        V Offline
        Vikram A Punathambekar
        wrote on last edited by
        #5

        Handle the TextChanged event. Even better, use a NumericUpDown control.

        Cheers, Vıkram.


        "You idiot British surprise me that your generators which grew up after Mid 50s had no brain at all." - Adnan Siddiqi.

        1 Reply Last reply
        0
        • S S a n d y

          Hi All, I want to paste only valid value(like numeric value) in textbox through mouse(right click) and keyboard(Ctrl+V). Thanks in advance.

          D Offline
          D Offline
          DaveyM69
          wrote on last edited by
          #6

          override WndProc, trap WM_PASTE and process there. Check out pinvoke.net.

          Dave
          BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)
          Visual Basic is not used by normal people so we're not covering it here. (Uncyclopedia)

          S 1 Reply Last reply
          0
          • D DaveyM69

            override WndProc, trap WM_PASTE and process there. Check out pinvoke.net.

            Dave
            BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)
            Visual Basic is not used by normal people so we're not covering it here. (Uncyclopedia)

            S Offline
            S Offline
            S a n d y
            wrote on last edited by
            #7

            Thanks, but I don't know how to use this. Please give me a little example.

            D 1 Reply Last reply
            0
            • S S a n d y

              Hi All, I want to paste only valid value(like numeric value) in textbox through mouse(right click) and keyboard(Ctrl+V). Thanks in advance.

              V Offline
              V Offline
              Vengatachalapathy Palanivel
              wrote on last edited by
              #8

              If that's a web application or windows application . If it's a web application, then you can go about by Javascripts.

              S 1 Reply Last reply
              0
              • S S a n d y

                Thanks, but I don't know how to use this. Please give me a little example.

                D Offline
                D Offline
                DaveyM69
                wrote on last edited by
                #9

                OK. This example creates a customized TextBox so add this class, build then drop onto a form.

                public class RestrictedTextBox : TextBox
                {
                    private const int WM\_PASTE = 0x0302;
                
                    protected override void WndProc(ref Message m)
                    {
                        if (m.Msg == WM\_PASTE)
                        {
                            if (!IsPasteValid())
                            {
                                m.Result = IntPtr.Zero;
                                return;
                            }
                        }
                        base.WndProc(ref m);
                    }
                
                    private bool IsPasteValid()
                    {
                        bool rtn = false;
                        IDataObject obj = Clipboard.GetDataObject();
                        string pasteString = (string)obj.GetData(typeof(string));
                        // Do checks on pasteString here and set rtn to true if OK.
                        return rtn;
                    }
                }
                

                Dave
                BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)
                Visual Basic is not used by normal people so we're not covering it here. (Uncyclopedia)

                S 1 Reply Last reply
                0
                • V Vengatachalapathy Palanivel

                  If that's a web application or windows application . If it's a web application, then you can go about by Javascripts.

                  S Offline
                  S Offline
                  S a n d y
                  wrote on last edited by
                  #10

                  Its Window application.

                  V 1 Reply Last reply
                  0
                  • D DaveyM69

                    OK. This example creates a customized TextBox so add this class, build then drop onto a form.

                    public class RestrictedTextBox : TextBox
                    {
                        private const int WM\_PASTE = 0x0302;
                    
                        protected override void WndProc(ref Message m)
                        {
                            if (m.Msg == WM\_PASTE)
                            {
                                if (!IsPasteValid())
                                {
                                    m.Result = IntPtr.Zero;
                                    return;
                                }
                            }
                            base.WndProc(ref m);
                        }
                    
                        private bool IsPasteValid()
                        {
                            bool rtn = false;
                            IDataObject obj = Clipboard.GetDataObject();
                            string pasteString = (string)obj.GetData(typeof(string));
                            // Do checks on pasteString here and set rtn to true if OK.
                            return rtn;
                        }
                    }
                    

                    Dave
                    BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)
                    Visual Basic is not used by normal people so we're not covering it here. (Uncyclopedia)

                    S Offline
                    S Offline
                    S a n d y
                    wrote on last edited by
                    #11

                    Thanks

                    1 Reply Last reply
                    0
                    • S S a n d y

                      Its Window application.

                      V Offline
                      V Offline
                      Vengatachalapathy Palanivel
                      wrote on last edited by
                      #12

                      I think, this piece of code will help you out...

                      private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
                      {
                      if ( Convert.ToInt16(e.KeyChar) >= 48 && Convert.ToInt16(e.KeyChar) <=57)
                      {
                      e.KeyChar = e.KeyChar;
                      }
                      else
                      {
                      e.KeyChar = Convert.ToChar(0);
                      }
                      }

                          private void textBox1\_TextChanged(object sender, EventArgs e)
                          {
                              char\[\] strSample = new char\[((System.Windows.Forms.TextBox)(sender)).Text.ToString().Length\];
                      
                              strSample = (((System.Windows.Forms.TextBox)(sender)).Text).ToCharArray();
                      
                              for (int i=0; i<strSample.Length;i++)
                              {
                                  if (Convert.ToInt16(strSample\[i\]) >= 48 && Convert.ToInt16(strSample\[i\]) <= 57)
                                  {
                                      continue;
                                  }
                                  else
                                  {
                                      ((System.Windows.Forms.TextBox)(sender)).Text = "";
                                      break;
                                  }
                              }
                              
                          }
                      

                      Try this and let me know if you have any concerns. Regards, Vengat P

                      S 1 Reply Last reply
                      0
                      • V Vengatachalapathy Palanivel

                        I think, this piece of code will help you out...

                        private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
                        {
                        if ( Convert.ToInt16(e.KeyChar) >= 48 && Convert.ToInt16(e.KeyChar) <=57)
                        {
                        e.KeyChar = e.KeyChar;
                        }
                        else
                        {
                        e.KeyChar = Convert.ToChar(0);
                        }
                        }

                            private void textBox1\_TextChanged(object sender, EventArgs e)
                            {
                                char\[\] strSample = new char\[((System.Windows.Forms.TextBox)(sender)).Text.ToString().Length\];
                        
                                strSample = (((System.Windows.Forms.TextBox)(sender)).Text).ToCharArray();
                        
                                for (int i=0; i<strSample.Length;i++)
                                {
                                    if (Convert.ToInt16(strSample\[i\]) >= 48 && Convert.ToInt16(strSample\[i\]) <= 57)
                                    {
                                        continue;
                                    }
                                    else
                                    {
                                        ((System.Windows.Forms.TextBox)(sender)).Text = "";
                                        break;
                                    }
                                }
                                
                            }
                        

                        Try this and let me know if you have any concerns. Regards, Vengat P

                        S Offline
                        S Offline
                        S a n d y
                        wrote on last edited by
                        #13

                        Thanks Vengat, Everything is worked fine but if we paste characters then textbox goes blank. In that case we want current value of textbox. Then how to do that.

                        V 1 Reply Last reply
                        0
                        • S S a n d y

                          Thanks Vengat, Everything is worked fine but if we paste characters then textbox goes blank. In that case we want current value of textbox. Then how to do that.

                          V Offline
                          V Offline
                          Vengatachalapathy Palanivel
                          wrote on last edited by
                          #14

                          I am also new to windows application, but i'm sure that, you can bind the value of textbox again with the help of a global variable(that will be updated for each key press event and text change event). Sample: private string strTestVal = string.Empty; on keypress event: in if condition strTestVal = textBox1.Text.ToString(); on text change event: textBox1.Text = strTestVal;

                          Regards, Vengat P

                          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