编程教程
您现在的位置: 中国个人站长站 >> 网络编程 >> ASP.NET教程 >> 教程正文 制作一个可以输入 IP 地址的控件
推荐位

制作一个可以输入 IP 地址的控件

中国个人站长站 ASP.NET教程 点击数: 更新时间:2007-7-8 12:47:42

准备工作:     
     1. 首先,打开vs.net,新建一个C#类库项目。如叫 IpBoxControl。
     2. 在vs.net 的菜单,“项目”,“添加用户控件”,把这个用户控件取名为IpBox。
     3. 把一个Panel从工具箱拖到IpBox的设计视图,取名为panel1,设置panel1的Location为(0,0),Size为(136, 20),BorderStyle为Fixed3D,BackColor为Window。
     4. 把一个TextBox从工具箱拖到panel1里。取名为textBox1,设置textBox1的Location为(8,0),Size为(20, 14),BorderStyle为None,MaxLength为3,TextAlign为Center,Text为““,BackColor为Window。
     5. 把一个Label从工具箱拖到panel1里。取名为label1,设置label1的Location为(32, 0),Size为(8, 14),BorderStyle为None,TextAlign为TopCenter,Text为“.”,BackColor为Window。
     6. 选取textBox1和label1,textBox1复制、粘贴上3份,label1复制、粘贴上2份。分别命名为textBox2、label2,textBox3、label3,textBox4。Location分别为(40, 0)、(64, 0)、(72, 0)、(96, 0)、(104, 0)。
     7. 设置IpBox的Location为(0,0),Size为(136, 20)。
    
现在一个ip box 的形状已经出来了,接下来要在程序里实现输入的控制。
源代码如下:


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 的摘要说明。
 /// </summary>
 [
 Description("横刀夺爱的 Ip Box 控件!用于输入 IP 地址。"),
 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 = "";
  /// <summary>
  /// 必需的设计器变量。
  /// </summary>
  private System.ComponentModel.Container components = null;

  public IpBox()
  {
   // 该调用是 Windows.Forms 窗体设计器所必需的。
   InitializeComponent();

   // TODO: 在 InitializeComponent 调用后添加任何初始化
   
  }

  /// <summary>
  /// 清理所有正在使用的资源。
  /// </summary>
  protected override void Dispose( bool disposing )
  {
   if( disposing )
   {
    if(components != null)
    {
     components.Dispose();
    }
   }
   base.Dispose( disposing );
  }

  #region 组件设计器生成的代码
  /// <summary>
  /// 设计器支持所需的方法 - 不要使用代码编辑器
  /// 修改此方法的内容。
  /// </summary>
  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.panel1.Controls.Add(this.label1);
   this.panel1.Controls.Add(this.textBox1);
   this.panel1.Location = new System.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(this.textBox4_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);
   

[1] [2] [3] 下一页

教程录入:swh    责任编辑:swh 
个人站长站与你风雨同舟!
本站所提供的资源均来源于互联网,如有侵权行为,请与本站管理员联系,我们会第一时间删除!
·如果您发现《制作一个可以输入 IP 地址的控件》文章有错误,也请通知我们修改!
联系邮箱chinageren#126.com,谢谢支持!
站内搜索:
版权所有:中国个人站长站 2007-2008 未经授权禁止复制或建立镜像 客服QQ号:112731235
copyright © 2007-2008 www.ChinaGeRen.com online services. all rights reserved. 苏ICP备05000059号