C# Tutorial 57: TextBox which accepts only numbers in C#

แชร์
ฝัง
  • เผยแพร่เมื่อ 23 ม.ค. 2025

ความคิดเห็น • 63

  • @randomhappyguy6719
    @randomhappyguy6719 8 ปีที่แล้ว +11

    .Net has upgraded and int representation of characters are no longer needed. We can use code like this:
    char ch = e.KeyChar;
    if (!char.IsDigit(ch) &&
    ch != Convert.ToChar(Keys.Back) &&
    ch != Convert.ToChar(Keys.Delete))
    e.Handled = true;
    Here we use a Convert.ToChar(Object object) to directly convert a Keys enumeration variable to a character, then compare.

    • @kinglore27
      @kinglore27 7 ปีที่แล้ว +1

      Thank you!

    • @kojydamn7818
      @kojydamn7818 3 ปีที่แล้ว

      Thank you so much, this works perfectly!

  • @ZCProductions211
    @ZCProductions211 11 ปีที่แล้ว +5

    If Anyone is wondering how to make it possible to enter a period like 1.70 then add
    && ch != 46 to the if statement after the ch != 8. I had to do this for the program i am making.

    • @filipMmM
      @filipMmM 7 ปีที่แล้ว

      thank you :)

    • @krolik3524
      @krolik3524 6 ปีที่แล้ว

      Thank you.

  • @shovikanand3214
    @shovikanand3214 6 ปีที่แล้ว

    very knowledgeful video....but i want to add more concept in this, here e is object of keypressEventArgs Class. & this class has 2 properties namely keychar & handled. keychar gives u key pressed by user. & handled doesnt allows wrong entry into textboxes. here char is structures. & Isdigit, Isletter, IsDigitorLetter,Issymbol .....are its properties..
    hence we use , char.Isdigit(e.keychar) & similary e.handled= true; restrict wrong entry & e.handled= false; to allow entry in textbox. hope u get benifited after reading this...

  • @yameen_yasin
    @yameen_yasin 6 ปีที่แล้ว

    thankew sir aap explane bhi ache krte ho or kaam to hota he aap k acha hai

  • @muraligsn
    @muraligsn 9 ปีที่แล้ว +25

    Thanks for the video..but please stop wiggling the mouse.

    • @kaimemes
      @kaimemes 5 ปีที่แล้ว +5

      what you gotta do wit ha big fat mouse
      *WIGGLE WIGGLE WIGGLE WIGGLE WIGGLE WIGGLE WIGGLE WIGGLE WIGGLE WIGGLE WIGGLE WIGGLE WIGGLE WIGGLE WIGGLE WIGGLE WIGGLE WIGGLE WIGGLE WIGGLE WIGGLE WIGGLE WIGGLE WIGGLE WIGGLE WIGGLE WIGGLE WIGGLE WIGGLE WIGGLE WIGGLE WIGGLE WIGGLE WIGGLE WIGGLE WIGGLE WIGGLE WIGGLE WIGGLE WIGGLE WIGGLE WIGGLE WIGGLE WIGGLE WIGGLE WIGGLE WIGGLE WIGGLE WIGGLE WIGGLE WIGGLE WIGGLE WIGGLE WIGGLE WIGGLE WIGGLE WIGGLE WIGGLE WIGGLE WIGGLE WIGGLE WIGGLE WIGGLE WIGGLE WIGGLE WIGGLE WIGGLE WIGGLE WIGGLE WIGGLE WIGGLE WIGGLE WIGGLE WIGGLE WIGGLE WIGGLE WIGGLE WIGGLE WIGGLE WIGGLE WIGGLE WIGGLE WIGGLE WIGGLE WIGGLE WIGGLE WIGGLE WIGGLE WIGGLE WIGGLE WIGGLE WIGGLE WIGGLE WIGGLE WIGGLE WIGGLE WIGGLE WIGGLE WIGGLE WIGGLE WIGGLE WIGGLE WIGGLE WIGGLE WIGGLE WIGGLE WIGGLE WIGGLE WIGGLE WIGGLE WIGGLE WIGGLE WIGGLE WIGGLE WIGGLE WIGGLE WIGGLE WIGGLE WIGGLE WIGGLE WIGGLE WIGGLE WIGGLE WIGGLE WIGGLE WIGGLE WIGGLE WIGGLE WIGGLE WIGGLE WIGGLE WIGGLE WIGGLE WIGGLE WIGGLE WIGGLE WIGGLE WIGGLE WIGGLE WIGGLE WIGGLE WIGGLE WIGGLE WIGGLE WIGGLE WIGGLE WIGGLE WIGGLE WIGGLE WIGGLE WIGGLE WIGGLE WIGGLE WIGGLE WIGGLE WIGGLE WIGGLE WIGGLE WIGGLE WIGGLE WIGGLE WIGGLE WIGGLE WIGGLE WIGGLE WIGGLE WIGGLE WIGGLE WIGGLE WIGGLE WIGGLE WIGGLE WIGGLE WIGGLE WIGGLE WIGGLE WIGGLE WIGGLE WIGGLE WIGGLE WIGGLE WIGGLE WIGGLE WIGGLE WIGGLE WIGGLE WIGGLE WIGGLE WIGGLE WIGGLE WIGGLE WIGGLE WIGGLE WIGGLE WIGGLE WIGGLE WIGGLE WIGGLE WIGGLE WIGGLE WIGGLE WIGGLE WIGGLE WIGGLE WIGGLE WIGGLE WIGGLE WIGGLE WIGGLE WIGGLE WIGGLE WIGGLE WIGGLE WIGGLE WIGGLE WIGGLE WIGGLE WIGGLE WIGGLE WIGGLE WIGGLE WIGGLE WIGGLE WIGGLE WIGGLE WIGGLE WIGGLE WIGGLE WIGGLE WIGGLE WIGGLE WIGGLE WIGGLE WIGGLE WIGGLE WIGGLE WIGGLE WIGGLE WIGGLE WIGGLE WIGGLE WIGGLE WIGGLE WIGGLE WIGGLE WIGGLE WIGGLE WIGGLE WIGGLE WIGGLE WIGGLE WIGGLE WIGGLE WIGGLE WIGGLE WIGGLE WIGGLE WIGGLE WIGGLE WIGGLE WIGGLE WIGGLE WIGGLE WIGGLE WIGGLE WIGGLE WIGGLE WIGGLE WIGGLE WIGGLE WIGGLE WIGGLE WIGGLE WIGGLE WIGGLE WIGGLE WIGGLE WIGGLE WIGGLE WIGGLE WIGGLE WIGGLE WIGGLE WIGGLE WIGGLE WIGGLE WIGGLE WIGGLE WIGGLE WIGGLE WIGGLE WIGGLE WIGGLE WIGGLE WIGGLE WIGGLE WIGGLE WIGGLE WIGGLE WIGGLE WIGGLE WIGGLE WIGGLE WIGGLE WIGGLE WIGGLE WIGGLE WIGGLE WIGGLE WIGGLE WIGGLE WIGGLE WIGGLE WIGGLE WIGGLE WIGGLE WIGGLE WIGGLE WIGGLE WIGGLE WIGGLE WIGGLE WIGGLE WIGGLE WIGGLE WIGGLE WIGGLE WIGGLE WIGGLE WIGGLE WIGGLE WIGGLE WIGGLE WIGGLE WIGGLE WIGGLE WIGGLE WIGGLE WIGGLE WIGGLE WIGGLE WIGGLE WIGGLE WIGGLE WIGGLE WIGGLE WIGGLE WIGGLE WIGGLE WIGGLE WIGGLE WIGGLE WIGGLE WIGGLE WIGGLE WIGGLE WIGGLE WIGGLE WIGGLE WIGGLE WIGGLE WIGGLE WIGGLE WIGGLE WIGGLE WIGGLE WIGGLE WIGGLE*

  • @avi12
    @avi12 11 ปีที่แล้ว

    I was looking for this so much time. *Subscribed*

  • @amazingbandproyects3253
    @amazingbandproyects3253 2 ปีที่แล้ว

    Works : ) ..Thank you so much.....Regards from Costa Rica : )

  • @girishbawaskar3775
    @girishbawaskar3775 7 ปีที่แล้ว +1

    and if i want only char than ?????

  • @LivinExtreme
    @LivinExtreme 11 ปีที่แล้ว +2

    what if I paste it doesn't work then

  • @23anonymous46
    @23anonymous46 7 ปีที่แล้ว

    It's really help me so much, thank you very much guy C#

  • @MegaMaliak
    @MegaMaliak 5 ปีที่แล้ว

    Hi how do i make validation to allow format like prices ONLY? 999.99 is the precise amount i need. Nothing more than that, but can't go below 0.

  • @HearthsTavern
    @HearthsTavern 8 ปีที่แล้ว +2

    Great Video! Thanks for the upload
    How can I make this accept decimal?

  • @Kaizer-qe7kk
    @Kaizer-qe7kk 8 ปีที่แล้ว

    One question. How do I get data from the textbox as an integer?
    I need to perform a calculation with the data obtained.

  • @3dwinarcip394
    @3dwinarcip394 9 หลายเดือนก่อน

    Niceee❤❤❤ Thanks

  • @انامسلموكفى
    @انامسلموكفى 4 ปีที่แล้ว

    Thnak u for your video its great i just have a small isssue with this filtraion how can we for example just allow numbers between 0 and 13 ?

  • @mikaillim9978
    @mikaillim9978 11 ปีที่แล้ว +1

    hii.. why you can using DateTimePicker ? it simple :D

  • @AhsanAli-uu9ui
    @AhsanAli-uu9ui 6 ปีที่แล้ว

    i cant found your Remaining lectures of this course in this Channel. what i do? please give me link of complt tutorial of this course. Thanks

  • @arvicishechema
    @arvicishechema 7 ปีที่แล้ว

    You helped me so much. Thank you bhai

  • @rodrigo-vl7bi
    @rodrigo-vl7bi 5 ปีที่แล้ว

    thanks for showing this solution, but im sure there must be a simplier way, al keep looking for it

  • @malthesrensen4953
    @malthesrensen4953 5 ปีที่แล้ว

    Seems wierd that this isen't in the library already. When pasting, you can still add text to the field for example, so that has to be fixed aswell. Seems like a lot of work for something that should've been in the library already

  • @MrFerny82
    @MrFerny82 11 ปีที่แล้ว

    Hi, your tutorial is great but i'm running into an issue and I want to use number an at the end the character -, how to i do that? my example should look like this only 102030-10
    Thanks

  • @oblivionX000
    @oblivionX000 7 ปีที่แล้ว

    Is that possible with decimals as well? Cause I would try to input a period for the decimal which then the code prevents me from doing so.

  • @idsoftware7
    @idsoftware7 5 ปีที่แล้ว

    how about decimals ?

  • @IAmESG
    @IAmESG 8 ปีที่แล้ว +2

    Or this:
    if (System.Text.RegularExpressions.Regex.IsMatch(tb11.Text, "[^0-9-]")) // only accepts 0-9 and negative
    {
    MessageBox.Show("Please enter numbers only!");
    tb11.Clear();
    }

  • @funtaro
    @funtaro 8 ปีที่แล้ว

    Thanks, it's help me alot

  • @adrianm.4873
    @adrianm.4873 4 ปีที่แล้ว

    Thank you!

  • @kmuralikrishna1582
    @kmuralikrishna1582 5 ปีที่แล้ว

    if we want combination of numbers and string ?

  • @intelligentcoding689
    @intelligentcoding689 3 ปีที่แล้ว

    Amazing. thanks

  • @Verses01
    @Verses01 7 ปีที่แล้ว

    Awesome Thank you!!!

  • @hafizullahmutahidi264
    @hafizullahmutahidi264 7 ปีที่แล้ว

    Thank You So So Much

  • @Soundtech98
    @Soundtech98 8 ปีที่แล้ว

    Still doesn't prevent the user from pasting directly into the text box...

  • @wunnathaw
    @wunnathaw 7 ปีที่แล้ว

    that is cool..but I input 012 or 1.11 .It cannot validate .

    • @jobijose8354
      @jobijose8354 6 ปีที่แล้ว

      this is not an validation process infact we can block some sort of data thats all

  • @brv0102
    @brv0102 7 ปีที่แล้ว

    thanks, bro!

  • @deano2187
    @deano2187 8 ปีที่แล้ว

    Thank you senpai

  • @atquoc9071
    @atquoc9071 5 ปีที่แล้ว

    Thanks for video

  • @ariii39
    @ariii39 4 ปีที่แล้ว

    Thanks!!!!

  • @bayaromer5850
    @bayaromer5850 6 ปีที่แล้ว

    thank you soooooooo much

  • @muhdzikrie8682
    @muhdzikrie8682 3 ปีที่แล้ว

    thanks

  • @thiagosarraf7265
    @thiagosarraf7265 5 ปีที่แล้ว

    where are you from? I'm brazilian

  • @TheTrueKailash
    @TheTrueKailash 8 ปีที่แล้ว

    thank you

  • @YukthiJayakody
    @YukthiJayakody 8 ปีที่แล้ว

    Thanks a lot....

  • @sheetalvishwakarma2697
    @sheetalvishwakarma2697 6 ปีที่แล้ว

    Voice is not auditable

  • @prodbyfys
    @prodbyfys 7 ปีที่แล้ว

    Thanks Bro

  • @bluehornet6752
    @bluehornet6752 11 ปีที่แล้ว +1

    Here's a routine that's worked well for me...
    private void CharValidation_KeyDown(object sender, KeyEventArgs e)
    {
    // Rejects any keys not in the range of 0-9 (by ASCII number),
    // the Delete key or the Back key
    if ((e.KeyValue < 48 || e.KeyValue > 57) && e.KeyCode != Keys.Delete
    && e.KeyCode != Keys.Back
    {
    e.SuppressKeyPress = true;
    SystemSounds.Beep.Play();
    }
    }

    • @Volln0v
      @Volln0v 10 ปีที่แล้ว

      This works, but not if a user used the num pad. This is what I used for my program, it's kind of lengthy but it allowed for numpad decimal, period, numlock, and arrow keys. Probably not the most efficient way, but it works.
      if ((e.KeyValue < 48 || e.KeyValue > 57) && e.KeyCode != Keys.OemPeriod && e.KeyCode != Keys.NumLock && e.KeyCode != Keys.Decimal && e.KeyCode != Keys.Delete
      && e.KeyCode != Keys.Back && e.KeyCode != Keys.NumPad0 && e.KeyCode != Keys.NumPad1 && e.KeyCode != Keys.NumPad2 && e.KeyCode != Keys.NumPad3 && e.KeyCode != Keys.NumPad4 && e.KeyCode != Keys.NumPad5 && e.KeyCode != Keys.NumPad6 && e.KeyCode != Keys.NumPad7 && e.KeyCode != Keys.NumPad8 && e.KeyCode != Keys.NumPad9 && e.KeyCode != Keys.Left && e.KeyCode != Keys.Right)
      {
      e.SuppressKeyPress = true;
      SystemSounds.Beep.Play();
      }

  • @dennisJNT
    @dennisJNT 6 ปีที่แล้ว

    nice man..

  • @gnscode7988
    @gnscode7988 4 ปีที่แล้ว

    helpfull

  • @kristiancagaoan2223
    @kristiancagaoan2223 10 ปีที่แล้ว +3

    THANKS BRUH

  • @iiN1GH7M4R3ii
    @iiN1GH7M4R3ii 6 ปีที่แล้ว

    thank youuuuu

  • @claytonvolner5950
    @claytonvolner5950 2 ปีที่แล้ว

    Great video. Thanks for the help

  • @MonderMurshed
    @MonderMurshed 2 ปีที่แล้ว

    THANK U BRO!

  • @vihangasilva
    @vihangasilva 8 ปีที่แล้ว

    Thank you