Make a control that can enter an IP address

xiaoxiao2021-03-06  71

Preparation: 1. First, open VS.NET, create a new C # class library project. Call iPboxControl. 2. In the VS.NET menu, "Project", "Add User Control", named this user control is IPBOX. 3. Drag a Panel from the toolbox to the IPBox design view, name to Panel1, set the location of Panel1 to (0), size is (136, 20), borderstyle is Fixed3D, backcolor is Window. 4. Drag a TextBox from the toolbox to PANEL1. Takened to TextBox1, setting the textbox1 Location as (8, 0), size is (20, 14), borderstyle is None, Maxlength is 3, TextAlign is Center, Text is "", backcolor is Window. 5. Drab a Label dragged from the toolbox to Panel1. Taken to Label1, set Label1's location as (32, 0), size is (8, 14), borderstyle is none, textalign is TopCenter, Text is ".", Backcolor is Window. 6. Choose TextBox1 and Label1, TextBox1 replication, paste 3 copies, Label1 copy, and 2 copies. Named TEXTBOX2, Label2, TextBox3, Label3, TextBox4. Location is (40, 0), (64, 0), (72, 0), (96, 0), (104, 0). 7. Set ipbox's location as (0, 0), Size is (136, 20). Now a shape of IP box has already come out, then you want to implement the input control in the program. Source code is as follows: using System; using System.Collections; using System.ComponentModel; using System.Drawing; using System.Data; using System.Windows.Forms; using System.Text.RegularExpressions; namespace XRascal.Controls {/// < Summary> /// UserControl1 Abstract Description. /// [Description ("The IP BOX control of the horizontal knife! It is used to enter the IP address.

"), ToolboxBitmap (typeof (XRascal.Controls.IpBox))] public class IpBox: System.Windows.Forms.UserControl {private System.Windows.Forms.Panel panel1; private System.Windows.Forms.TextBox textBox4; private System. Windows.Forms.Label label3; private System.Windows.Forms.TextBox textBox3; private System.Windows.Forms.Label label2; private System.Windows.Forms.TextBox textBox2; private System.Windows.Forms.Label label1; private System. Windows.Forms.TextBox textBox1; private string_text = ""; ///

/// Required designer variable. /// private system.componentmodel.container components = null; public ipbox () {// This call is necessary for the Windows.Forms Form Designer. InitializationComponent (); // Todo: Add any initialization after the initializationComponent call} /// /// Clean all the resources being used. / // Protected Override Void Dispose (bool disposing) {if (disponents! = null) {components.dispose ();}} Base.Dispose (Disposing);} #region component designer generation Code /// /// designer supports the required method - do not use the code editor // to modify the contents of this method.

/// private void initializecomponent () {this.Panel1 = new system.windows.Forms.Panel (); this.textbox4 = new system.windows.forms.textbox (); this.label3 = new system. Windows.Forms.Label (); this.textBox3 = new system.windows.forms.textbox (); this.label2 = new system.windows.forms.label (); this.textbox2 = new system.Windows.Forms.TextBox (); This.Label1 = new system.windows.forms.label (); this.textbox1 = new system.windows.forms.textbox (); this.panel1.suspendlayout (); this.suspendlayout (); // / / / panel1 // this.panel1.backcolor = system.drawing.color.White; this.Panel1.Borderstyle = system.windows.Forms.Borderstyle.Fixed3D; this.Panel1.controls.add (this.textbox4); this.Panel1 .Controls.add (this.label3); this.Panel1.controls.add (this.TextBox3); this.Panel1.controls.add (this.label2); this.Panel1.controls.add (this.TextBox2); this. .pa1.controls.add (this.label1); this.Panel1.controls.add (this.textBox1); this.Panel1.Location = new sys Tem.drawing.point (0, 0); this.Panel1.name = "panel1"; this.panel1.size = new system.drawing.size (136, 20); this.Panel1.tabindex = 1; // / / textBox4 // this.textBox4.BorderStyle = System.Windows.Forms.BorderStyle.None; this.textBox4.Location = new System.Drawing.Point (104, 0); this.textBox4.MaxLength = 3; this.textBox4. name = "textBox4"; this.textBox4.Size = new System.Drawing.Size (20, 14); this.textBox4.TabIndex = 6; this.textBox4.TextAlign = System.Windows.Forms.HorizontalAlignment.Center; this. TextBox4.KeyPress = new system.windows.Forms.KeyPressEventHandler (THISTBOX4_KEYPRESS);

// // label3 // this.label3.location = new system.drawing.point (96, 0); this.label3.name = "label3"; this.label3.size = new system.drawing.size (8, 14); this.label3.tabindex = 5; this.label3.text = "."; This.label3.textalign = system.drawing.contentAlignment.topcenter; // // textBox3 // this.textbox3.borderStyle = System. Windows.Forms.Borderstyle.None; this.textBox3.Location = new system.drawing.point (72, 0); this.textBox3.maxlength = 3; this.textbox3.name = "textbox3"; this.TextBox3.size = new System.Drawing.Size (20, 14); this.textBox3.TabIndex = 4; this.textBox3.TextAlign = System.Windows.Forms.HorizontalAlignment.Center; this.textBox3.KeyPress = new System.Windows.Forms. KeypressEventHandler (THISTEXTBOX3_KEYPRESS); /// Label2 // this.label2.location = new system.drawing.point (64, 0); this.label2.name = "label2"; this.label2.size = new system .Drawing.size (8, 14); this.label2.tabindex = 3; this.label2.text = "; this.Lab el2.TextAlign = System.Drawing.ContentAlignment.TopCenter; // // textBox2 // this.textBox2.BorderStyle = System.Windows.Forms.BorderStyle.None; this.textBox2.Location = new System.Drawing.Point (40, 0); this.textBox2.maxlength = 3; this.textBox2.name = "textbox2"; this.textbox2.size = new system.drawing.size (20, 14); this.TextBox2.TabINDEX = 2; this.textbox2 .Textalign = system.windows.forms.hor; this.textbox2.keypress = new system.windows.Forms.KeyPressEventHandler (THISTEXTBOX2_KEYPRESS);

/// Label1 // this.label1.Location = new system.drawing.point (32, 0); this.label1.name = "label1"; this.label1.size = new system.drawing.size (8, 14); this.label1.tabindex = 1; this.label1.text = "."; This.label1.textalign = system.drawing.contentAlignment.topcenter; //// textBox1 // this.textbox1.borderStyle = System. Windows.Forms.Borderstyle.None; this.textBox1.Location = new system.drawing.point (8, 0); this.textBox1.maxlength = 3; this.textbox1.name = "textbox1"; this.textbox1.size = new System.Drawing.Size (20, 14); this.textBox1.TabIndex = 0; this.textBox1.TextAlign = System.Windows.Forms.HorizontalAlignment.Center; this.textBox1.KeyPress = new System.Windows.Forms. KeyPressEventHandler (THISTBOX1_KEYPRESS); /// ipbox // this.controls.add (this.Panel1); this.name = "ipbox"; this.size = new system.drawing.size (136, 20); this .panel1.resumelayout (false); this.ResumeLayout (false);} #ENDREGION Private void textbox1_ KeyDown (object sender, System.Windows.Forms.KeyEventArgs e) {//System.Text.Encoding.GetEncoding (0) .GetString ();} private void textBox1_KeyPress (object sender, System.Windows.Forms.KeyPressEventArgs e) { MaskIpAddr (textBox1, e);} private void textBox2_KeyPress (object sender, System.Windows.Forms.KeyPressEventArgs e) {MaskIpAddr (textBox2, e);} private void textBox3_KeyPress (object sender, System.Windows.Forms.KeyPressEventArgs e) { Maskipaddr (TextBox3, e);} private void textbox4_keypress (object sender, system.windows) {Maskipaddr (TextBox4, e);

} Private void maskipaddr (system.windows.forms.textbox textbox, keypressEventargs e) {int Len = textBox.text.length; if (char.isdigit (e.keychar) || e.keychar == '.' || e . Keychar == 8) {IF (e.keychar! = 8) {if (len == 2 && E.Keychar! = '.') {String tmp = textbox.text e.Keychar; if (TextBox.name == "TextBox1") {if (int32.parse (tmp)> 223) // Performance {MessageBox.show (TMP "is not a valid item. Please specify a value between 1 and 223.") TextBox.text = "223"; TextBox.focus (); return;} textBox2.focus (); textBox2.selectall ();} else if (TextBox.name == "textbox2") {if (int32.parse TMP> 255) {MessageBox.show (TMP "is not a valid item. Please specify a value between 1 and 255."); TextBox.text = "255"; textBox.focus (); return; TextBox3.focus (); TextBox3.selectall ();} else if (TextBox.Name == "TextBox3") {if (int32.parse (tmp)> 255) { MessageBox.show (TMP "is not a valid item. Please specify a value between 1 and 255. "); TextBox.Text =" 255 "; TextBox.focus (); return;} textbox4.focus (); textBox4.selectall ();} else ==" textbox4 ") {if (int32.) {If (INT32. PARSE (TMP)> 255) {MessageBox.show (TMP "is not a valid item. Please specify a value between 1 and 255.

"); TextBox.text =" 255 "; textbox.focus (); return;}}}}}} {if (TextBox.name ==" TextBox1 "&& textbox.text! = "") {TextBox2.focus (); textBox2.selectall ();} if (TextBox.Name == "TextBox2" && textbox.text! = "") {Textbox3.focus (); textbox3.selectall ();} IF (TextBox.Name == "TextBox3" && textBox.text! = "") {textbox4.focus (); textBox4.selectall ();} if (textbox.name == "TextBox4" && textbox.text! = " ") {} E.handled = true;}} else {if (textbox.name ==" textBox1 "&& textbox.text ==" ") {} if (TextBox.name ==" TextBox2 "&& textbox.text = = "") {TextBox1.focus (); textBox1.selectionStart = textBox1.text.length;} if (textbox.name == "textBox3" && textbox.text == "") {textBox2.focus (); textbox2. SelectionStart = TextBox2.text.le NGTH;} if (TextBox.name == "TextBox4" && textBox.text == "") {textbox3.focus (); textBox3.seext.length;} E.handled = false;}} Else E .Handled = true;} [Browsable] ///

/// Gets the text of IPBOX.

转载请注明原文地址:https://www.9cbs.com/read-109621.html

New Post(0)