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

📄 usercontrol1.cs

📁 Csharp实例编程百例.rar
💻 CS
字号:
using System;
using System.Collections;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Windows.Forms;

namespace MyNumberBox
{
	/// <summary>
	/// UserControl1 的摘要说明。
	/// </summary>
	public class MyNumberBox : System.Windows.Forms.TextBox 
	{
		/// <summary>
		/// 必需的设计器变量。
		/// </summary>
		private System.ComponentModel.Container components = null;

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

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

		}

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

		#region Component Designer generated code
		/// <summary>
		/// 设计器支持所需的方法 - 不要使用代码编辑器 
		/// 修改此方法的内容。
		/// </summary>
		private void InitializeComponent()
		{
			// 
			// MyNumberBox
			// 
			this.Text = "0.";
			this.KeyPress += new System.Windows.Forms.KeyPressEventHandler(this.MyNumberBox_KeyPress);

		}
		#endregion

		//是否重新输入数字,如果重新输入,则先清空
		public bool reInput = false; 
		//是否开始输入数字
		private bool beginInput = false;
		//是否已经输入小数点
		private bool dotInput = false;

		//重置初始状态
		public void reSet()			
		{	
			beginInput = false;
			dotInput = false;
			this.Text = "0.";
			this.Focus ();
			//设置光标位置,在0上
			this.SelectionStart = this.Text.Length-1;
		}
		//只读属性,读取文本框中的数值
		public double InputNumber
		{
			get
			{
				if (this.Text !="")
				{
					return double.Parse (this.Text );
				}
				else
				{
					return(0);
				}
			}
		}

		//调用键盘按下事件处理方法
		public void MyInputKey(object sender, char kchar)
		{
			KeyPressEventArgs e = new KeyPressEventArgs(kchar);
			this.MyNumberBox_KeyPress(sender, e);
		}

		private void MyNumberBox_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e)
		{	//设置键盘事件已处理,其余工作自己完成
			e.Handled = true;
			//当键盘输入为数字、小数点和BackSpace时,进行处理
			if ( Char.IsDigit (e.KeyChar ) || e.KeyChar == '.' || e.KeyChar == 8)
			{
				//如果需要重新输入,则恢复初始状态
				if (reInput)
				{
					reInput =false;
					this.reSet();
				}
				//当还没开始键盘输入时,此时文本为"0."
				if (!beginInput)	
				{
					if (e.KeyChar!=8)
					{
						beginInput = true;
						//如果第一个输入的字符是数字,显示"数字."
						if (Char.IsDigit (e.KeyChar))
						{	
							this.Text = e.KeyChar .ToString () + ".";
						}
						else//输入小数点,仍显示"0.",不变
						{	
							dotInput = true;
						}
					}
					else
					{	//Backspace,什么也不做
					}
				}else //已经开始键盘输入了
				{
					if (e.KeyChar == 8)//输入BackSpace键
					{
						if(dotInput)
						{	//如果小数点在最后一位,则不变,显示仍为"数字."
							if(this.Text[this.Text.Length-1]=='.')
							{
								dotInput = false;
							}
							else
							{	//删除最后一位数字
								this.Text = this.Text.Substring (0,this.Text .Length -1);
							}
						}
						else
						{	//还没有输入小数点,显示为"数字.",删除最后一位数字
							this.Text = this.Text.Remove(this.Text .Length-2,1);
							//如果没有数字了,则输入全删除了,重置
							if (this.Text == ".")
							{
								this.reSet ();
							}
						}
					}
					//输入数字字符
					else if(Char.IsDigit (e.KeyChar ))
					{
						//已经输入小数点了,把数字直接加在后面
						if(dotInput)
						{
							this.Text += e.KeyChar .ToString ();
						}
						else//还没有输入小数点,把数字加在小数点前
						{
							this.Text = this.Text.Insert (this.Text .Length -1, e.KeyChar .ToString ());
						}
					}
					//输入小数点,如果已经输入小数点,不需处理,如果没有输入,修改dotInput。
					else if (e.KeyChar == '.')
					{
						dotInput = true;
					}
				}
			}
			//如果已经输入小数点,光标在文本最后,没有输入小数点,光标在小数点前
			if (dotInput)
			{
				this.SelectionStart = this.Text .Length;
			}
			else
			{	this.SelectionStart = this.Text .Length-1;
			}
		}
	}
}

⌨️ 快捷键说明

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