用写C#写的一个IP输入框控件

None.gif using  System; 
None.gif
using  System.Collections.Generic; 
None.gif
using  System.Text; 
None.gif
None.gif
namespace  IpBox 
ExpandedBlockStart.gifContractedBlock.gif
dot.gif
InBlock.gif    
using System; 
InBlock.gif    
using System.Collections; 
InBlock.gif    
using System.ComponentModel; 
InBlock.gif    
using System.Drawing; 
InBlock.gif    
using System.Data; 
InBlock.gif    
using System.Windows.Forms; 
InBlock.gif    
using System.Text.RegularExpressions; 
InBlock.gif
InBlock.gif    
public class IpBox : System.Windows.Forms.UserControl 
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif
InBlock.gif        
private System.Windows.Forms.Panel panel1; 
InBlock.gif        
private System.Windows.Forms.TextBox textBox4; 
InBlock.gif        
private System.Windows.Forms.Label label3; 
InBlock.gif        
private System.Windows.Forms.TextBox textBox3; 
InBlock.gif        
private System.Windows.Forms.Label label2; 
InBlock.gif        
private System.Windows.Forms.TextBox textBox2; 
InBlock.gif        
private System.Windows.Forms.Label label1; 
InBlock.gif        
private System.Windows.Forms.TextBox textBox1; 
InBlock.gif        
private string _text = ""
InBlock.gif         
InBlock.gif        
private System.ComponentModel.Container components = null
InBlock.gif
InBlock.gif        
public IpBox() 
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif
InBlock.gif            
// 该调用是 Windows.Forms 窗体设计器所必需的。 
InBlock.gif
            InitializeComponent(); 
InBlock.gif
InBlock.gif            
// TODO: 在 InitializeComponent 调用后添加任何初始化 
InBlock.gif

ExpandedSubBlockEnd.gif        }
 
InBlock.gif
InBlock.gif         
InBlock.gif        
protected override void Dispose(bool disposing) 
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif
InBlock.gif            
if (disposing) 
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif
InBlock.gif                
if (components != null
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif
InBlock.gif                    components.Dispose(); 
ExpandedSubBlockEnd.gif                }
 
ExpandedSubBlockEnd.gif            }
 
InBlock.gif            
base.Dispose(disposing); 
ExpandedSubBlockEnd.gif        }
 
InBlock.gif
ContractedSubBlock.gifExpandedSubBlockStart.gif        
组件设计器生成的代码#region 组件设计器生成的代码 
InBlock.gif      
InBlock.gif        
private void InitializeComponent() 
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif
InBlock.gif            
this.panel1 = new System.Windows.Forms.Panel(); 
InBlock.gif            
this.textBox4 = new System.Windows.Forms.TextBox(); 
InBlock.gif            
this.label3 = new System.Windows.Forms.Label(); 
InBlock.gif            
this.textBox3 = new System.Windows.Forms.TextBox(); 
InBlock.gif            
this.label2 = new System.Windows.Forms.Label(); 
InBlock.gif            
this.textBox2 = new System.Windows.Forms.TextBox(); 
InBlock.gif            
this.label1 = new System.Windows.Forms.Label(); 
InBlock.gif            
this.textBox1 = new System.Windows.Forms.TextBox(); 
InBlock.gif            
this.panel1.SuspendLayout(); 
InBlock.gif            
this.SuspendLayout(); 
InBlock.gif            
//  
InBlock.gif            
// panel1 
InBlock.gif            
//  
InBlock.gif
            this.panel1.BackColor = System.Drawing.Color.White; 
InBlock.gif            
this.panel1.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D; 
InBlock.gif            
this.panel1.Controls.Add(this.textBox4); 
InBlock.gif            
this.panel1.Controls.Add(this.label3); 
InBlock.gif            
this.panel1.Controls.Add(this.textBox3); 
InBlock.gif            
this.panel1.Controls.Add(this.label2); 
InBlock.gif            
this.panel1.Controls.Add(this.textBox2); 
InBlock.gif            
this.panel1.Controls.Add(this.label1); 
InBlock.gif            
this.panel1.Controls.Add(this.textBox1); 
InBlock.gif            
this.panel1.Location = new System.Drawing.Point(00); 
InBlock.gif            
this.panel1.Name = "panel1"
InBlock.gif            
this.panel1.Size = new System.Drawing.Size(13620); 
InBlock.gif            
this.panel1.TabIndex = 1
InBlock.gif            
//  
InBlock.gif            
// textBox4 
InBlock.gif            
//  
InBlock.gif
            this.textBox4.BorderStyle = System.Windows.Forms.BorderStyle.None; 
InBlock.gif            
this.textBox4.Location = new System.Drawing.Point(1040); 
InBlock.gif            
this.textBox4.MaxLength = 3
InBlock.gif            
this.textBox4.Name = "textBox4"
InBlock.gif            
this.textBox4.Size = new System.Drawing.Size(2014); 
InBlock.gif            
this.textBox4.TabIndex = 6
InBlock.gif            
this.textBox4.TextAlign = System.Windows.Forms.HorizontalAlignment.Center; 
InBlock.gif            
this.textBox4.KeyPress += new System.Windows.Forms.KeyPressEventHandler(this.textBox4_KeyPress); 
InBlock.gif            
//  
InBlock.gif            
// label3 
InBlock.gif            
//  
InBlock.gif
            this.label3.Location = new System.Drawing.Point(960); 
InBlock.gif            
this.label3.Name = "label3"
InBlock.gif            
this.label3.Size = new System.Drawing.Size(814); 
InBlock.gif            
this.label3.TabIndex = 5
InBlock.gif            
this.label3.Text = "."
InBlock.gif            
this.label3.TextAlign = System.Drawing.ContentAlignment.TopCenter; 
InBlock.gif            
//  
InBlock.gif            
// textBox3 
InBlock.gif            
//  
InBlock.gif
            this.textBox3.BorderStyle = System.Windows.Forms.BorderStyle.None; 
InBlock.gif            
this.textBox3.Location = new System.Drawing.Point(720); 
InBlock.gif            
this.textBox3.MaxLength = 3
InBlock.gif            
this.textBox3.Name = "textBox3"
InBlock.gif            
this.textBox3.Size = new System.Drawing.Size(2014); 
InBlock.gif            
this.textBox3.TabIndex = 4
InBlock.gif            
this.textBox3.TextAlign = System.Windows.Forms.HorizontalAlignment.Center; 
InBlock.gif            
this.textBox3.KeyPress += new System.Windows.Forms.KeyPressEventHandler(this.textBox3_KeyPress); 
InBlock.gif            
//  
InBlock.gif            
// label2 
InBlock.gif            
//  
InBlock.gif
            this.label2.Location = new System.Drawing.Point(640); 
InBlock.gif            
this.label2.Name = "label2"
InBlock.gif            
this.label2.Size = new System.Drawing.Size(814); 
InBlock.gif            
this.label2.TabIndex = 3
InBlock.gif            
this.label2.Text = "."
InBlock.gif            
this.label2.TextAlign = System.Drawing.ContentAlignment.TopCenter; 
InBlock.gif            
//  
InBlock.gif            
// textBox2 
InBlock.gif            
//  
InBlock.gif
            this.textBox2.BorderStyle = System.Windows.Forms.BorderStyle.None; 
InBlock.gif            
this.textBox2.Location = new System.Drawing.Point(400); 
InBlock.gif            
this.textBox2.MaxLength = 3
InBlock.gif            
this.textBox2.Name = "textBox2"
InBlock.gif            
this.textBox2.Size = new System.Drawing.Size(2014); 
InBlock.gif            
this.textBox2.TabIndex = 2
InBlock.gif            
this.textBox2.TextAlign = System.Windows.Forms.HorizontalAlignment.Center; 
InBlock.gif            
this.textBox2.KeyPress += new System.Windows.Forms.KeyPressEventHandler(this.textBox2_KeyPress); 
InBlock.gif            
//  
InBlock.gif            
// label1 
InBlock.gif            
//  
InBlock.gif
            this.label1.Location = new System.Drawing.Point(320); 
InBlock.gif            
this.label1.Name = "label1"
InBlock.gif            
this.label1.Size = new System.Drawing.Size(814); 
InBlock.gif            
this.label1.TabIndex = 1
InBlock.gif            
this.label1.Text = "."
InBlock.gif            
this.label1.TextAlign = System.Drawing.ContentAlignment.TopCenter; 
InBlock.gif            
//  
InBlock.gif            
// textBox1 
InBlock.gif            
//  
InBlock.gif
            this.textBox1.BorderStyle = System.Windows.Forms.BorderStyle.None; 
InBlock.gif            
this.textBox1.Location = new System.Drawing.Point(80); 
InBlock.gif            
this.textBox1.MaxLength = 3
InBlock.gif            
this.textBox1.Name = "textBox1"
InBlock.gif            
this.textBox1.Size = new System.Drawing.Size(2014); 
InBlock.gif            
this.textBox1.TabIndex = 0
InBlock.gif            
this.textBox1.TextAlign = System.Windows.Forms.HorizontalAlignment.Center; 
InBlock.gif            
this.textBox1.KeyPress += new System.Windows.Forms.KeyPressEventHandler(this.textBox1_KeyPress); 
InBlock.gif            
//  
InBlock.gif            
// IpBox 
InBlock.gif            
//  
InBlock.gif
            this.Controls.Add(this.panel1); 
InBlock.gif            
this.Name = "IpBox"
InBlock.gif            
this.Size = new System.Drawing.Size(13620); 
InBlock.gif            
this.panel1.ResumeLayout(false); 
InBlock.gif            
this.panel1.PerformLayout(); 
InBlock.gif            
this.ResumeLayout(false); 
InBlock.gif
ExpandedSubBlockEnd.gif        }
 
ExpandedSubBlockEnd.gif        
#endregion
 
InBlock.gif
InBlock.gif        
private void textBox1_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e) 
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif
InBlock.gif            
//System.Text.Encoding.GetEncoding(0).GetString(); 
ExpandedSubBlockEnd.gif
        }
 
InBlock.gif
InBlock.gif        
private void textBox1_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e) 
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif
InBlock.gif            MaskIpAddr(textBox1, e); 
ExpandedSubBlockEnd.gif        }
 
InBlock.gif
InBlock.gif        
private void textBox2_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e) 
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif
InBlock.gif            MaskIpAddr(textBox2, e); 
ExpandedSubBlockEnd.gif        }
 
InBlock.gif
InBlock.gif        
private void textBox3_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e) 
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif
InBlock.gif            MaskIpAddr(textBox3, e); 
ExpandedSubBlockEnd.gif        }
 
InBlock.gif
InBlock.gif        
private void textBox4_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e) 
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif
InBlock.gif            MaskIpAddr(textBox4, e); 
ExpandedSubBlockEnd.gif        }
 
InBlock.gif
InBlock.gif        
private void MaskIpAddr(System.Windows.Forms.TextBox textBox, KeyPressEventArgs e) 
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif
InBlock.gif            
int len = textBox.Text.Length; 
InBlock.gif
InBlock.gif            
if (Char.IsDigit(e.KeyChar) || e.KeyChar == '.' || e.KeyChar == 8
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif
InBlock.gif                
if (e.KeyChar != 8
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif
InBlock.gif                    
if (len == 2 && e.KeyChar != '.'
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
dot.gif
InBlock.gif                        
string tmp = textBox.Text + e.KeyChar; 
InBlock.gif                        
if (textBox.Name == "textBox1"
ExpandedSubBlockStart.gifContractedSubBlock.gif                        
dot.gif
InBlock.gif                            
if (Int32.Parse(tmp) > 223// 进行验证 
ExpandedSubBlockStart.gifContractedSubBlock.gif
                            dot.gif
InBlock.gif                                MessageBox.Show(tmp 
+ " 不是一个有效项目。请指定一个介于 1 和 223 之间的数值。"); 
InBlock.gif                                textBox.Text 
= "223"
InBlock.gif                                textBox.Focus(); 
InBlock.gif                                
return
ExpandedSubBlockEnd.gif                            }
 
InBlock.gif
InBlock.gif                            textBox2.Focus(); 
InBlock.gif                            textBox2.SelectAll(); 
InBlock.gif
ExpandedSubBlockEnd.gif                        }
 
InBlock.gif                        
else if (textBox.Name == "textBox2"
ExpandedSubBlockStart.gifContractedSubBlock.gif                        
dot.gif
InBlock.gif                            
if (Int32.Parse(tmp) > 255
ExpandedSubBlockStart.gifContractedSubBlock.gif                            
dot.gif
InBlock.gif                                MessageBox.Show(tmp 
+ " 不是一个有效项目。请指定一个介于 1 和 255 之间的数值。"); 
InBlock.gif                                textBox.Text 
= "255"
InBlock.gif                                textBox.Focus(); 
InBlock.gif                                
return
ExpandedSubBlockEnd.gif                            }
 
InBlock.gif                            textBox3.Focus(); 
InBlock.gif                            textBox3.SelectAll(); 
ExpandedSubBlockEnd.gif                        }
 
InBlock.gif                        
else if (textBox.Name == "textBox3"
ExpandedSubBlockStart.gifContractedSubBlock.gif                        
dot.gif
InBlock.gif                            
if (Int32.Parse(tmp) > 255
ExpandedSubBlockStart.gifContractedSubBlock.gif                            
dot.gif
InBlock.gif                                MessageBox.Show(tmp 
+ " 不是一个有效项目。请指定一个介于 1 和 255 之间的数值。"); 
InBlock.gif                                textBox.Text 
= "255"
InBlock.gif                                textBox.Focus(); 
InBlock.gif                                
return
ExpandedSubBlockEnd.gif                            }
 
InBlock.gif                            textBox4.Focus(); 
InBlock.gif                            textBox4.SelectAll(); 
ExpandedSubBlockEnd.gif                        }
 
InBlock.gif                        
else if (textBox.Name == "textBox4"
ExpandedSubBlockStart.gifContractedSubBlock.gif                        
dot.gif
InBlock.gif                            
if (Int32.Parse(tmp) > 255
ExpandedSubBlockStart.gifContractedSubBlock.gif                            
dot.gif
InBlock.gif                                MessageBox.Show(tmp 
+ " 不是一个有效项目。请指定一个介于 1 和 255 之间的数值。"); 
InBlock.gif                                textBox.Text 
= "255"
InBlock.gif                                textBox.Focus(); 
InBlock.gif                                
return
ExpandedSubBlockEnd.gif                            }
 
InBlock.gif
ExpandedSubBlockEnd.gif                        }
 
InBlock.gif
ExpandedSubBlockEnd.gif                    }
 
InBlock.gif                    
if (e.KeyChar == '.'
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
dot.gif
InBlock.gif                        
if (textBox.Name == "textBox1" && textBox.Text != ""
ExpandedSubBlockStart.gifContractedSubBlock.gif                        
dot.gif
InBlock.gif                            textBox2.Focus(); 
InBlock.gif                            textBox2.SelectAll(); 
ExpandedSubBlockEnd.gif                        }
 
InBlock.gif                        
if (textBox.Name == "textBox2" && textBox.Text != ""
ExpandedSubBlockStart.gifContractedSubBlock.gif                        
dot.gif
InBlock.gif                            textBox3.Focus(); 
InBlock.gif                            textBox3.SelectAll(); 
ExpandedSubBlockEnd.gif                        }
 
InBlock.gif                        
if (textBox.Name == "textBox3" && textBox.Text != ""
ExpandedSubBlockStart.gifContractedSubBlock.gif                        
dot.gif
InBlock.gif                            textBox4.Focus(); 
InBlock.gif                            textBox4.SelectAll(); 
ExpandedSubBlockEnd.gif                        }
 
InBlock.gif                        
if (textBox.Name == "textBox4" && textBox.Text != ""
ExpandedSubBlockStart.gifContractedSubBlock.gif                        
dot.gif
InBlock.gif
ExpandedSubBlockEnd.gif                        }
 
InBlock.gif                        e.Handled 
= true
ExpandedSubBlockEnd.gif                    }
 
ExpandedSubBlockEnd.gif                }
 
InBlock.gif                
else 
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif
InBlock.gif                    
if (textBox.Name == "textBox1" && textBox.Text == ""
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
dot.gif
InBlock.gif
ExpandedSubBlockEnd.gif                    }
 
InBlock.gif                    
if (textBox.Name == "textBox2" && textBox.Text == ""
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
dot.gif
InBlock.gif                        textBox1.Focus(); 
InBlock.gif                        textBox1.SelectionStart 
= textBox1.Text.Length; 
ExpandedSubBlockEnd.gif                    }
 
InBlock.gif                    
if (textBox.Name == "textBox3" && textBox.Text == ""
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
dot.gif
InBlock.gif                        textBox2.Focus(); 
InBlock.gif                        textBox2.SelectionStart 
= textBox2.Text.Length; 
ExpandedSubBlockEnd.gif                    }
 
InBlock.gif                    
if (textBox.Name == "textBox4" && textBox.Text == ""
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
dot.gif
InBlock.gif                        textBox3.Focus(); 
InBlock.gif                        textBox3.SelectionStart 
= textBox3.Text.Length; 
ExpandedSubBlockEnd.gif                    }
 
InBlock.gif                    e.Handled 
= false
ExpandedSubBlockEnd.gif                }
 
ExpandedSubBlockEnd.gif            }
 
InBlock.gif            
else 
InBlock.gif                e.Handled 
= true
ExpandedSubBlockEnd.gif        }
 
InBlock.gif
InBlock.gif
InBlock.gif        [Browsable(
true)] 
InBlock.gif         
InBlock.gif        
// 获取 IpBox 的文本。 
InBlock.gif
         
InBlock.gif        
public new string Text 
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif
InBlock.gif            
get 
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif
InBlock.gif                
if (textBox1.Text == "" 
InBlock.gif                 
|| textBox2.Text == "" 
InBlock.gif                 
|| textBox3.Text == "" 
InBlock.gif                 
|| textBox4.Text == ""
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif
InBlock.gif                    _text 
= ""
InBlock.gif                    
return _text; 
ExpandedSubBlockEnd.gif                }
 
InBlock.gif                
else 
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif
InBlock.gif                    _text 
= Convert.ToInt32(textBox1.Text).ToString() + "." + Convert.ToInt32(textBox2.Text).ToString() + "." + Convert.ToInt32(textBox3.Text).ToString() + "." + Convert.ToInt32(textBox4.Text).ToString(); 
InBlock.gif                    
return _text; 
ExpandedSubBlockEnd.gif                }
 
InBlock.gif
ExpandedSubBlockEnd.gif            }
 
ExpandedSubBlockStart.gifContractedSubBlock.gif            
/**//* set  //  
InBlock.gif             { 
InBlock.gif              if(value != null) 
InBlock.gif              { 
InBlock.gif               // 255-255-255-255 
InBlock.gif               //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}|1[0-9]{1,2}|[1-2][0-5]{2})\.(\d{1,2}|1[0-9]{1,2}|[1-2][0-5]{2})$"; 
InBlock.gif               // 223-255-255-255 
InBlock.gif               string pattern = @"^([1-9]|\d{2}|1[0-9]{1,2}|2[1-2][1-3])\.(\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][0-5]{2})$"; 
InBlock.gif
InBlock.gif               if( !Regex.IsMatch(value , pattern) ) 
InBlock.gif               { 
InBlock.gif                _text = ""; 
InBlock.gif                throw new Exception("IP 地址格式错误!"); 
InBlock.gif               } 
InBlock.gif               else 
InBlock.gif               { 
InBlock.gif                string[] ipnum = value.Split('.'); 
InBlock.gif                textBox1.Text = ipnum[0]; 
InBlock.gif                textBox2.Text = ipnum[1]; 
InBlock.gif                textBox3.Text = ipnum[2]; 
InBlock.gif                textBox4.Text = ipnum[3]; 
InBlock.gif               } 
InBlock.gif              } 
InBlock.gif
InBlock.gif              _text = value; 
InBlock.gif
InBlock.gif             } 
ExpandedSubBlockEnd.gif            
*/
 
InBlock.gif
ExpandedSubBlockEnd.gif        }
 
InBlock.gif
ExpandedSubBlockEnd.gif    }
 
ExpandedBlockEnd.gif}
 
None.gif
None.gif
None.gif

转载于:https://www.cnblogs.com/hzuIT/articles/819079.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值