C# 提供一个Winform小数字键盘模拟器
生活随笔
收集整理的這篇文章主要介紹了
C# 提供一个Winform小数字键盘模拟器
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
文章開始之前,先看一下效果圖,看是不是您正所需要的:
一、構建計算器的界面
要構建出一個好看點的計算器界面,還是需要頗費些小心思的,我做這個的時候,也花了兩三個小時的時間構建這個界面。
其主要的使用控制是TableLayoutPanel控件。
另外一個小難點則在于內容控件Textbox的顯示,要讓文字垂直居中,在沒有重寫Textbox控件的情況下要達到這個效果,也是花了些小心思。
其它的界面則沒有什么的。至于加減號嘛,則用輸入法的特殊符號即可。
二、構建控件的開放屬性
一共開放了3個屬性,不夠自己加。這3個如下,看注釋應該能懂:
/// <summary> /// 可接受的最小值,最小為-3.402823E+38 /// </summary> [Browsable(true)] [Category("Zhongzhou")] [DefaultValue(0)] [Description("可接受的最小值,最小為-3.402823E+38")] public float Min { get; set; } = 0;/// <summary> /// 可接受的最大值,最大為3.402823E+38 /// </summary> [Browsable(true)] [Category("Zhongzhou")] [DefaultValue(0)] [Description("可接受的最大值,最大為3.402823E+38")] public float Max { get; set; } = 0;/// <summary> /// 設置小數點的精度位數,默認為2位小數點 /// </summary> [Browsable(true)] [Category("Zhongzhou")] [DefaultValue(2)] [Description("設置小數點的精度位數,默認為2位小數點")] public int Precision { get; set; } = 2;?三、控件鍵盤輸入
我們的目的是讓小鍵盤來輸入數字,所以需要禁止實體鍵盤輸入文字字母等信息,以及小數字點最多只能出現一次,具體邏輯如下:
/// <summary> /// 當使用實物鍵盤輸入文本內容時觸發 /// </summary> /// <param name="e"></param> private void OnKeyPressed(KeyPressEventArgs e) {//13表示回車if (e.KeyChar == 13){this.OnEntered();e.Handled = true;return;}//48代表0,57代表9,8代表空格,46代表小數點if ((e.KeyChar < 48 || e.KeyChar >= 57) && (e.KeyChar != 8) && (e.KeyChar != 46)){e.Handled = true;return;}//判斷多次輸入小數點,僅允許出現1次小數點if (e.KeyChar == 46){this.PointHandle();this.SetContentFocus();e.Handled = true;return;} }/// <summary> /// 處理小數點 /// </summary> /// <returns><see langword="true"/>表示處理成功,<see langword="false"/>表示未處理</returns> private bool PointHandle() {string content = this.ContentTextBox.Text;if (content.IndexOf('.') != -1){return false;}if (string.IsNullOrEmpty(content)){this.SetContent("0.");return true;}//取光標位置int index = this.ContentTextBox.SelectionStart;string str = this.ContentTextBox.Text.Substring(0, index);if (str == "+" || str == "-"){return this.SetContent(string.Join(string.Empty, str, "0.", this.ContentTextBox.Text.Substring(index, this.ContentTextBox.Text.Length - index)));}return this.SetContent(string.Join(string.Empty, str, ".", this.ContentTextBox.Text.Substring(index, this.ContentTextBox.Text.Length - index))); }四、讓文本框處理焦點狀態以及光標位置的處理
光標位置,需要特殊處理的,默認參數cursorPosition=-1時,光標位置始終移到最末尾處。但是有些情況,比如你要讓光標在數字中間刪除幾個數字或者添加幾個數字,就不能讓光標自動跑到最末尾處了。
/// <summary> /// 設置新值 /// </summary> /// <param name="newContent">表示新值</param> private bool SetContent(string newContent) {int precision = this.Precision;if (string.IsNullOrEmpty(newContent)){this.ContentTextBox.Text = string.Empty;return true;}var scheme = newContent.Split('.');if (scheme.Length == 2){var realPrecision = scheme[1].Length;if (realPrecision > precision){return false;}}this.ContentTextBox.Text = newContent;return true; }五、實現退格、清除內容的功能
/// <summary> /// 清除內容 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void ClearButton_Click(object sender, EventArgs e) {this.SetContent(string.Empty);this.SetContentFocus(); }/// <summary> /// 退格內容 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void BackButton_Click(object sender, EventArgs e) {//取光標位置int index = this.ContentTextBox.SelectionStart;//剪切內容string cutStr = this.ContentTextBox.Text.Substring(0, index);//剩余內容string remainStr = this.ContentTextBox.Text.Substring(index, this.ContentTextBox.Text.Length - index);int position = this.SetContent(string.Join(string.Empty, cutStr.Substring(0, cutStr.Length - 1), remainStr)) ? index - 1 : index;this.SetContentFocus(position); }六、實現Enter確認得到結果的功能
原理是通過事件來實現的。代碼如下:
/// <summary> /// 當按下回車按鈕時的事件委托 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> public delegate void EnteredEventHandler(object sender, float e);/// <summary> /// 當按下回車按鈕時的事件 /// </summary> public event EnteredEventHandler Entered;/// <summary> /// 當迷你小鍵盤按下回車時觸發事件 /// </summary> protected virtual void OnEntered() {float min = this.Min;float max = this.Max;var value = string.IsNullOrEmpty(this.ContentTextBox.Text) ? 0 : Convert.ToSingle(this.ContentTextBox.Text);if (max != 0 && value > max){MessageBox.Show("值不在最大范圍內", "提示");return;}if (min != 0 && value < min){MessageBox.Show("值不在最小范圍內", "提示");return;}this.Entered?.Invoke(this, value); }/// <inheritdoc cref="OnEntered"/> private void EnterButton_Click(object sender, EventArgs e) {this.OnEntered();this.SetContentFocus(); }最后附上源碼下載:MiniKeyboard.Designer.zip-互聯網文檔類資源-CSDN下載
祝您用餐愉快
總結
以上是生活随笔為你收集整理的C# 提供一个Winform小数字键盘模拟器的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Libcurl库详解
- 下一篇: neo4j 3.4.7安装和使用