C #-for event definition and registration, how can we enter only numbers in a text box, 0 to 9, 10 numbers, A to Z, and A to Z fifty-Twelve characters.

Source: Internet
Author: User
using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Linq;using System.Text;using System.Windows.Forms;namespace WindowsFormsApplication2{    public partial class Form1 : Form    {        public Form1()        {            InitializeComponent();        }        private void radioButton1_CheckedChanged(object sender, EventArgs e)        {            this.label1.Text = this.textBox1.Text;        }        private void radioButton2_CheckedChanged(object sender, EventArgs e)        {            this.label1.Text = this.textBox2.Text;        }        private void Form1_Load(object sender, EventArgs e)        {            this.textBox1.KeyPress += new KeyPressEventHandler(textBox1_KeyPress);        }        private void textBox1_TextChanged(object sender, EventArgs e)        {                    }        private void textBox1_KeyPress(object sender, KeyPressEventArgs e)        {            if (e.KeyChar < '0' || e.KeyChar > '9')            {                e.Handled = true;            }        }                 }}

 

Comments: The keypress event is used in the implementation process. However, when implementing this event, you need to register the event in form1_load, that is, this. textbox1.keypress + = new keypresseventhandler (textbox?keypress); this step is the key.

 

 

The following is a string of 0 to 9 digits, A to Z, or A to Z fifty-Twelve characters.

 

Using system; using system. collections. generic; using system. componentmodel; using system. data; using system. drawing; using system. LINQ; using system. text; using system. windows. forms; namespace windowsformsapplication2 {public partial class form1: FORM {public form1 () {initializecomponent ();} private void radiobutton1_checkedchanged (Object sender, eventargs e) {This. label1.text = This. textbox1.text ;} Private void radiobutton2_checkedchanged (Object sender, eventargs e) {This. label1.text = This. textbox2.text;} private void form1_load (Object sender, eventargs e) {This. textbox1.keypress + = new keypresseventhandler (textbox?keypress);} private void textbox=textchanged (Object sender, eventargs e) {}// 4. refer to the p162 page of the textbook to design a form. There is a text box in the form, which can only enter 0 to 9 10 types of numbers, A to Z, or A to Z fifty-Twelve characters. Private void textbox1_keypress (Object sender, keypresseventargs e) {If (E. keychar <'0' | E. keychar> 'Z') {e. handled = false ;}}}}

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.