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 ;}}}}