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

แชร์
ฝัง
  • เผยแพร่เมื่อ 3 พ.ค. 2013
  • ★★★Top Online Courses From ProgrammingKnowledge ★★★
    Python Programming Course ➡️ bit.ly/2vsuMaS ⚫️ bit.ly/2GOaeQB
    Java Programming Course ➡️ bit.ly/2GEfQMf ⚫️ bit.ly/2Vvjy4a
    Bash Shell Scripting Course ➡️ bit.ly/2DBVF0C ⚫️ bit.ly/2UM06vF
    Linux Command Line Tutorials ➡️ bit.ly/2IXuil0 ⚫️ bit.ly/2IXukt8
    C Programming Course ➡️ bit.ly/2GQCiD1 ⚫️ bit.ly/2ZGN6ej
    C++ Programming Course ➡️ bit.ly/2V4oEVJ ⚫️ bit.ly/2XMvqMs
    PHP Programming Course ➡️ bit.ly/2XP71WH ⚫️ bit.ly/2vs3od6
    Android Development Course ➡️ bit.ly/2UHih5H ⚫️ bit.ly/2IMhVci
    C# Programming Course ➡️ bit.ly/2Vr7HEl ⚫️ bit.ly/2W6RXTU
    JavaFx Programming Course ➡️ bit.ly/2XMvZWA ⚫️ bit.ly/2V2CoAi
    NodeJs Programming Course ➡️ bit.ly/2GPg7gA ⚫️ bit.ly/2GQYTQ2
    Jenkins Course For Developers and DevOps ➡️ bit.ly/2Wd4l4W ⚫️ bit.ly/2J1B1ug
    Scala Programming Tutorial Course ➡️ bit.ly/2PysyA4 ⚫️ bit.ly/2PCaVj2
    Bootstrap Responsive Web Design Tutorial ➡️ bit.ly/2DFQ2yC ⚫️ bit.ly/2VoJWwH
    MongoDB Tutorial Course ➡️ bit.ly/2LaCJfP ⚫️ bit.ly/2WaI7Ap
    QT C++ GUI Tutorial For Beginners ➡️ bit.ly/2vwqHSZ
    ★★★ Online Courses to learn ★★★
    Get 2 FREE Months of Unlimited Classes from skillshare - skillshare.eqcm.net/r1KEj
    Data Science - bit.ly/2lD9h5L | bit.ly/2lI8wIl
    Machine Learning - bit.ly/2WGGQpb | bit.ly/2GghLXX
    Artificial Intelligence - bit.ly/2lYqaYx | bit.ly/2NmaPya
    MERN Stack E-Degree Program - bit.ly/2kx2NFe | bit.ly/2lWj4no
    DevOps E-degree - bit.ly/2k1PwUQ | bit.ly/2k8Ypfy
    Data Analytics with R - bit.ly/2lBKqz8 | bit.ly/2lAjos3
    AWS Certification Training - bit.ly/2kmLtTu | bit.ly/2lAkQL1
    Projects in Java - bit.ly/2kzn25d | bit.ly/2lBMffs
    Machine Learning With TensorFlow - bit.ly/2m1z3AF | bit.ly/2lBMhnA
    Angular 8 - Complete Essential Guide - bit.ly/2lYvYRP
    Kotlin Android Development Masterclass - bit.ly/2GcblsI
    Learn iOS Programming Building Advance Projects - bit.ly/2kyX7ue
    ★★★ Follow ★★★
    My Website - www.codebind.com
    DISCLAIMER: This video and description contains affiliate links, which means that if you click on one of the product links, I’ll receive a small commission. This help support the channel and allows us to continue to make videos like this. Thank you for the support!
    c# - How do I make a textbox that only accepts numbers?
    c# - How do I generate a textbox that only accepts numbers?
    Allow user to only enter numbers in textbox and if character
    Textbox only number or decimal
    Textbox must accept only numbers and
    Textbox only accepting numbers in C# windows application
    C# Numbers Only Textbox - Visual C#
    Textbox to accept only numbers in C# windows Forms C# .NET
    textbox accept only numbers

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

  • @randomhappyguy6719
    @randomhappyguy6719 7 ปีที่แล้ว +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 6 ปีที่แล้ว +1

      Thank you!

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

      Thank you so much, this works perfectly!

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

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

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

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

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

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

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

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

  • @muraligsn
    @muraligsn 8 ปีที่แล้ว +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*

  • @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

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

    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.

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

    Awesome Thank you!!!

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

    You helped me so much. Thank you bhai

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

    THANK U BRO!

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

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

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

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

  • @ZCProductions211
    @ZCProductions211 10 ปีที่แล้ว +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.

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

    Thanks, it's help me alot

  • @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

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

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

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

    what if I paste it doesn't work then

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

    Thank you!

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

    thanks, bro!

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

    Amazing. thanks

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

    Thank You So So Much

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

    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.

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

    Niceee❤❤❤ Thanks

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

    Thanks for video

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

    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...

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

    thanks

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

    Thank you

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

    Thanks a lot....

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

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

  • @user-xg7hi5mh3g
    @user-xg7hi5mh3g 3 ปีที่แล้ว

    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 ?

    • @user-pm4ng4wf8d
      @user-pm4ng4wf8d 3 ปีที่แล้ว

      استخدم شرط if لتحديد أصغر و أكبر قيمة

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

    thank you soooooooo much

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

    thank you

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

    Thanks Bro

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

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

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

    Thank you senpai

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

    Thanks!!!!

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

    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

  • @IAmESG
    @IAmESG 7 ปีที่แล้ว +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();
    }

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

    nice man..

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

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

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

    helpfull

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

    how about decimals ?

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

    if we want combination of numbers and string ?

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

    THANKS BRUH

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

    Great video. Thanks for the help

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

    thank youuuuu

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

    where are you from? I'm brazilian

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

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

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

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

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

    Voice is not auditable

  • @bluehornet6752
    @bluehornet6752 10 ปีที่แล้ว +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();
      }