Make a control that can enter an IP address

xiaoxiao2021-03-06  57

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. The source code is as follows: use system; use system.componentmodel; using system.drawing; useing system.data; using system.windows.forms; using system.text.regularexpressions;

Namespace Xrascal.Controls {///

/// userControl1 summary description. /// [Description ("" IP BOX control for horizontal knives! " .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 required for the Windows.Forms Form Designer. InitializationComponent ();

// Todo: Add any initialization after INITIALIZEC Mobomponent call

///

/// Clean all the resources being used. /// Protected Override Void Dispose (Bool Disposing) {if (disponents! = Null) {components.dispose ();}} Base.Dispose (4);

#REGION component designer generated 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 e) {maskipaddr (TextBox1, e);}

Private void textbox2_keypress (object sender, system.windows e) {maskipaddr (TextBox2, e);}

Private void textbox3_keypress (object sender, system.windows.windows e) {Maskipaddr (TextBox3, e);}

Private void textbox4_keypress (object sender, system.windows e) {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) // verifies {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) {Messa Gebox.Show 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. /// public new string text {get {if (TextBox1.text == "|| textbox2.text ==" "|| textbox3.text ==" "|| textbox4.text ==" " ) {_Text = ""; return_text;} else {_text = convert.TOINT32 (TextBox1.text) .tostring () " " Convert.Toint32 (TextBox2.text) .tostring () ". Convert .TOINT32 (TEXTBOX3.TEXT) .tostring () "." Convert.Toint32 (TextBox4.text) .tostring (); return_text;}} / * set // {i (value! = Null) {// 255-255-255-255 // String Pattern = @ "^ ([1-9] | / D {2} | 1 [0-9] {1,2} | 2 [0-5] {2}) /. (D {1, 2} | 1 [0-9] {1,2} | [1-2] [0-5] {2}) /. (/ d {1, 2} | 0-9] {1,2} | [1-2] [0-5] {2}) /. (/ D {1, 2} | 1 [0-9] {1, 2} | [1- 2] [0-5] {2}) $ "; // 223-255-255-255 String Pattern = @" ^ ([1-9] | / D {2} | 1 [0-9] {1 2} | 2 [1-2]) /. (/ D {1, 2} | 1 [0-9] {1,2} | [1-2] [0-5] { 2}) /. (/ D {1, 2} | 1 [0-9] {1, 2} | [1-2] [0-5] {2}) /. (/ D {1, 2} | 1 [0-9] {1, 2} | [1-2] {2}) $ "; if (! Regex.ismatch (value, pattern)) {_text =" "; throw new Exception ("IP address format error!" } Else {string [] ipnum = value.split ('.'); TextBox1.text = ipnum [0]; TextBox2.text = ipnum [1]; TextBox3.text = ipnum [2]; TextBox4.text = IPNUM [3];}}

_Text = value;

} * /}

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

New Post(0)