digit input only in text box

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • David

    digit input only in text box

    hello, little newbie question,
    i'm tring to create textbox control with ability do write there only
    digits'.
    i tried to check at -left focus- event, but it's not that i really want, i
    want not to be able even to write to control anything but digits'...

    how can i do that? or there can i find any sample


  • Mike Kitchen

    #2
    Re: digit input only in text box

    Hi David

    Have alook at the textbox KeyPress event, then inspect KeyEventArgs
    parameter and interogate which key has been pressed.

    The following code may help

    private void textBox1_KeyPre ss(object sender, KeyPressEventAr gs e)
    {
    const char Delete = (char)8;
    e.Handled = !Char.IsDigit(e .KeyChar) && e.KeyChar != Delete;
    }

    Use "e.Handled = true;" to stop characters being accepted.

    Hope this helps

    Publicjoe
    C# Tutorial at http://www.publicjoe.f9.co.uk/csharp/tut.html
    C# Snippets at http://www.publicjoe.f9.co.uk/csharp/snip/snippets.html
    C# Ebook at http://www.publicjoe.f9.co.uk/csharp/samples/ebook.html
    VB Ebook at http://www.publicjoe.f9.co.uk/vbnet/samples/ebook.html


    "David" <david_tm3@hotm ail.com> wrote in message
    news:#JvNVf6cEH A.2664@TK2MSFTN GP09.phx.gbl...[color=blue]
    > hello, little newbie question,
    > i'm tring to create textbox control with ability do write there only
    > digits'.
    > i tried to check at -left focus- event, but it's not that i really want, i
    > want not to be able even to write to control anything but digits'...
    >
    > how can i do that? or there can i find any sample
    >
    >[/color]


    Comment

    • Adrian

      #3
      Re: digit input only in text box

      You can create a digital textBox class. The trouble is, that under certain
      circumstances,
      the MS IDE might decide to throw out all your digital textboxes, created in
      that way.
      That may cause one hell of a job to put things right again. At least, that
      was my exerience.

      So what I do in stead is this

      #region ...
      <come code>
      this.textBox1 += new
      System.Windows. Forms.KeyPressE ventHandler(thi s.numbersCheck) ;
      <some code>
      #endregion

      <somewhere else>
      private void numbersChecked( Object o, KeyPressEventAr gs e)
      {
      if(!char.IsCont rol(e.KeyChar) && !char.IsDigit(e .KeyChar()
      e.Handled is true;
      }

      *************** *************** *************** ***

      "David" <david_tm3@hotm ail.com> wrote in message
      news:%23JvNVf6c EHA.2664@TK2MSF TNGP09.phx.gbl. ..[color=blue]
      > hello, little newbie question,
      > i'm tring to create textbox control with ability do write there only
      > digits'.
      > i tried to check at -left focus- event, but it's not that i really want, i
      > want not to be able even to write to control anything but digits'...
      >
      > how can i do that? or there can i find any sample
      >
      >[/color]


      Comment

      • Shiva

        #4
        Re: digit input only in text box

        Hi,

        You can handle the keypress event of the textbox. Based on the keycode
        pressed, you can either block or continue with the key.

        An example (in VB.NET) could be found here:



        "David" <david_tm3@hotm ail.com> wrote in message
        news:#JvNVf6cEH A.2664@TK2MSFTN GP09.phx.gbl...
        hello, little newbie question,
        i'm tring to create textbox control with ability do write there only
        digits'.
        i tried to check at -left focus- event, but it's not that i really want, i
        want not to be able even to write to control anything but digits'...

        how can i do that? or there can i find any sample



        Comment

        • Radovan Radic

          #5
          Re: digit input only in text box

          I created my own numeric text box, and it is working properly. Basically it
          overrides OnKeyPress and OnKeyDown protected methods and formats text.
          The only problem i had with it is, when i put it on the windows form in VS
          IDE, text becomes "numericTextBox 1" although i did some initializations in
          the constructor saying
          public NumericTextBox( )
          {
          Text = FormatText(0);
          ...
          }
          How can i hanlde this?

          "Adrian" <aa@aa.aa> wrote in message
          news:2d1a9$4106 0725$3e15a001$1 0206@freeler.nl ...[color=blue]
          > You can create a digital textBox class. The trouble is, that under certain
          > circumstances,
          > the MS IDE might decide to throw out all your digital textboxes, created[/color]
          in[color=blue]
          > that way.
          > That may cause one hell of a job to put things right again. At least, that
          > was my exerience.
          >
          > So what I do in stead is this
          >
          > #region ...
          > <come code>
          > this.textBox1 += new
          > System.Windows. Forms.KeyPressE ventHandler(thi s.numbersCheck) ;
          > <some code>
          > #endregion
          >
          > <somewhere else>
          > private void numbersChecked( Object o, KeyPressEventAr gs e)
          > {
          > if(!char.IsCont rol(e.KeyChar) && !char.IsDigit(e .KeyChar()
          > e.Handled is true;
          > }
          >
          > *************** *************** *************** ***
          >
          > "David" <david_tm3@hotm ail.com> wrote in message
          > news:%23JvNVf6c EHA.2664@TK2MSF TNGP09.phx.gbl. ..[color=green]
          > > hello, little newbie question,
          > > i'm tring to create textbox control with ability do write there only
          > > digits'.
          > > i tried to check at -left focus- event, but it's not that i really want,[/color][/color]
          i[color=blue][color=green]
          > > want not to be able even to write to control anything but digits'...
          > >
          > > how can i do that? or there can i find any sample
          > >
          > >[/color]
          >
          >[/color]



          Comment

          • Mariusz Wojsyk

            #6
            Re: digit input only in text box

            Greetings.

            U¿ytkownik "David" <david_tm3@hotm ail.com> napisa³ w wiadomo¶ci
            news:%23JvNVf6c EHA.2664@TK2MSF TNGP09.phx.gbl. ..
            [color=blue]
            > i'm tring to create textbox control with ability do write there only
            > digits'.
            > i tried to check at -left focus- event, but it's not that i really want, i
            > want not to be able even to write to control anything but digits'...[/color]

            Check the KeyDown event. It should be what you are looking for.

            BR
            Mariusz Wojsyk

            --
            "My software never has bugs. It just develops random features."


            Comment

            Working...