⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 usercontrol1.cs

📁 java基础方面的一些实例代码
💻 CS
字号:
using System;
using System.Collections;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Windows.Forms;

//namespace mcaoFirstWindowsControlLibrary
namespace mcaoWinAppAdvance
{
	/// <summary>
	/// UserControl1 的摘要说明。
	/// </summary>
	public class UserControl1 : System.Windows.Forms.UserControl
	{
		private System.Windows.Forms.Label label1;
		private System.Windows.Forms.Label label2;
		private System.Windows.Forms.TextBox IPtextBox1;
		private System.Windows.Forms.Label label3;
		private System.Windows.Forms.PictureBox pictureBox1;
		private int digitPos = 0;
		private int DelimitNumber = 0;
		/// <summary>
		/// 必需的设计器变量。
		/// </summary>
		private System.ComponentModel.Container components = null;

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

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

		}

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

		#region 组件设计器生成的代码
		/// <summary>
		/// 设计器支持所需的方法 - 不要使用代码编辑器 
		/// 修改此方法的内容。
		/// </summary>
		private void InitializeComponent()
		{
			System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(UserControl1));
			this.label1 = new System.Windows.Forms.Label();
			this.label2 = new System.Windows.Forms.Label();
			this.IPtextBox1 = new System.Windows.Forms.TextBox();
			this.label3 = new System.Windows.Forms.Label();
			this.pictureBox1 = new System.Windows.Forms.PictureBox();
			this.SuspendLayout();
			// 
			// label1
			// 
			this.label1.Font = new System.Drawing.Font("宋体", 10.5F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(134)));
			this.label1.ForeColor = System.Drawing.SystemColors.Highlight;
			this.label1.Location = new System.Drawing.Point(0, 8);
			this.label1.Name = "label1";
			this.label1.Size = new System.Drawing.Size(168, 24);
			this.label1.TabIndex = 0;
			this.label1.Text = "这是我自己定义的控件";
			// 
			// label2
			// 
			this.label2.Location = new System.Drawing.Point(0, 40);
			this.label2.Name = "label2";
			this.label2.Size = new System.Drawing.Size(56, 24);
			this.label2.TabIndex = 1;
			this.label2.Text = "IP输入:";
			// 
			// IPtextBox1
			// 
			this.IPtextBox1.Location = new System.Drawing.Point(56, 32);
			this.IPtextBox1.Name = "IPtextBox1";
			this.IPtextBox1.Size = new System.Drawing.Size(136, 21);
			this.IPtextBox1.TabIndex = 2;
			this.IPtextBox1.Text = "";
			this.IPtextBox1.KeyPress += new System.Windows.Forms.KeyPressEventHandler(this.IPtextBox1_KeyPress);
			// 
			// label3
			// 
			this.label3.Font = new System.Drawing.Font("宋体", 10.5F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(134)));
			this.label3.ForeColor = System.Drawing.SystemColors.Highlight;
			this.label3.Location = new System.Drawing.Point(0, 64);
			this.label3.Name = "label3";
			this.label3.Size = new System.Drawing.Size(72, 24);
			this.label3.TabIndex = 0;
			this.label3.Text = "版权所有";
			// 
			// pictureBox1
			// 
			this.pictureBox1.Image = ((System.Drawing.Image)(resources.GetObject("pictureBox1.Image")));
			this.pictureBox1.Location = new System.Drawing.Point(80, 64);
			this.pictureBox1.Name = "pictureBox1";
			this.pictureBox1.Size = new System.Drawing.Size(32, 24);
			this.pictureBox1.TabIndex = 3;
			this.pictureBox1.TabStop = false;
			// 
			// UserControl1
			// 
			this.Controls.Add(this.pictureBox1);
			this.Controls.Add(this.IPtextBox1);
			this.Controls.Add(this.label2);
			this.Controls.Add(this.label1);
			this.Controls.Add(this.label3);
			this.Name = "UserControl1";
			this.Size = new System.Drawing.Size(200, 88);
			this.ResumeLayout(false);

		}
		#endregion

		
		private void IPtextBox1_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e)
		{
			int len = IPtextBox1.Text.Length;
			int indx = IPtextBox1.Text.LastIndexOf(".");

			// if test is highlighted reset vars
			if (IPtextBox1.SelectedText == IPtextBox1.Text) 
			{
				indx = -1;
				digitPos = 0;
				DelimitNumber = 0;
			}

			if (Char.IsDigit(e.KeyChar) || e.KeyChar == '.' || e.KeyChar == 8)
			{
				// only digit, Backspace and dot are accepted
				string tmp = IPtextBox1.Text;
				if (e.KeyChar != 8) 
				{
					if (e.KeyChar != '.' )
					{
						if (indx > 0)
							digitPos = len-indx;
						else
							digitPos++;
					}

					if (digitPos == 3 && e.KeyChar != '.')
					{
						string tmp2 = IPtextBox1.Text.Substring(indx+1) + e.KeyChar;
						if (Int32.Parse(tmp2)> 255) // check validation
							MessageBox.Show("The number can't be bigger than 255 -> " + tmp2);
						else
						{
							if (DelimitNumber<3)
							{
								IPtextBox1.AppendText(e.KeyChar.ToString());
								IPtextBox1.AppendText(".");
								DelimitNumber++;
								e.Handled = true;
							}
						} // end of else
					} // end of if (digitPos == 3 && e.KeyChar != '.')
					else
						if (digitPos == 4 && DelimitNumber < 3)
						IPtextBox1.AppendText(".");					
				}//end of if (e.KeyChar != 8)
				else //相对于if (e.KeyChar != 8)
				{
					e.Handled = false;
					if ((len-indx) == 1)
					{
						DelimitNumber--;
						if (indx > -1 )
						{
							digitPos = len-indx;
						}
						else
							digitPos--;
					} // end of if((len-indx) == 1)
					else  //相对于if((len-indx) == 1)
					{
						if(indx > -1)
							digitPos = len-indx-1;
						else
							digitPos = len-1;
					} // end of else 相对于if((len-indx) == 1)
				} // end of else 相对于if (e.KeyChar != 8)
				//MessageBox.Show("OK");
			} // end of (Char.IsDigit(e.KeyChar) || e.KeyChar == '.' || e.KeyChar == 8)
			else
				e.Handled = true;			
			
		} // end of current method

	} // end of class
} // end of namespace

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -