您好,登錄后才能下訂單哦!
這篇文章主要介紹C#如何實(shí)現(xiàn)Winform小數(shù)字鍵盤模擬器,文中介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們一定要看完!
文章開始之前,先看一下效果圖,看是不是您正所需要的:
要構(gòu)建出一個(gè)好看點(diǎn)的計(jì)算器界面,還是需要頗費(fèi)些小心思的,我做這個(gè)的時(shí)候,也花了兩三個(gè)小時(shí)的時(shí)間構(gòu)建這個(gè)界面。
其主要的使用控制是TableLayoutPanel控件。
另外一個(gè)小難點(diǎn)則在于內(nèi)容控件Textbox的顯示,要讓文字垂直居中,在沒有重寫Textbox控件的情況下要達(dá)到這個(gè)效果,也是花了些小心思。
其它的界面則沒有什么的。至于加減號嘛,則用輸入法的特殊符號即可。
一共開放了3個(gè)屬性,不夠自己加。這3個(gè)如下,看注釋應(yīng)該能懂:
/// <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> /// 設(shè)置小數(shù)點(diǎn)的精度位數(shù),默認(rèn)為2位小數(shù)點(diǎn) /// </summary> [Browsable(true)] [Category("Zhongzhou")] [DefaultValue(2)] [Description("設(shè)置小數(shù)點(diǎn)的精度位數(shù),默認(rèn)為2位小數(shù)點(diǎn)")] public int Precision { get; set; } = 2;
我們的目的是讓小鍵盤來輸入數(shù)字,所以需要禁止實(shí)體鍵盤輸入文字字母等信息,以及小數(shù)字點(diǎn)最多只能出現(xiàn)一次,具體邏輯如下:
/// <summary> /// 當(dāng)使用實(shí)物鍵盤輸入文本內(nèi)容時(shí)觸發(fā) /// </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代表小數(shù)點(diǎn) if ((e.KeyChar < 48 || e.KeyChar >= 57) && (e.KeyChar != 8) && (e.KeyChar != 46)) { e.Handled = true; return; } //判斷多次輸入小數(shù)點(diǎn),僅允許出現(xiàn)1次小數(shù)點(diǎn) if (e.KeyChar == 46) { this.PointHandle(); this.SetContentFocus(); e.Handled = true; return; } } /// <summary> /// 處理小數(shù)點(diǎn) /// </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; } //取光標(biāo)位置 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))); }
光標(biāo)位置,需要特殊處理的,默認(rèn)參數(shù)cursorPosition=-1時(shí),光標(biāo)位置始終移到最末尾處。但是有些情況,比如你要讓光標(biāo)在數(shù)字中間刪除幾個(gè)數(shù)字或者添加幾個(gè)數(shù)字,就不能讓光標(biāo)自動跑到最末尾處了。
/// <summary> /// 設(shè)置新值 /// </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> /// 清除內(nèi)容 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void ClearButton_Click(object sender, EventArgs e) { this.SetContent(string.Empty); this.SetContentFocus(); } /// <summary> /// 退格內(nèi)容 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void BackButton_Click(object sender, EventArgs e) { //取光標(biāo)位置 int index = this.ContentTextBox.SelectionStart; //剪切內(nèi)容 string cutStr = this.ContentTextBox.Text.Substring(0, index); //剩余內(nèi)容 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); }
原理是通過事件來實(shí)現(xiàn)的。代碼如下:
/// <summary> /// 當(dāng)按下回車按鈕時(shí)的事件委托 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> public delegate void EnteredEventHandler(object sender, float e); /// <summary> /// 當(dāng)按下回車按鈕時(shí)的事件 /// </summary> public event EnteredEventHandler Entered; /// <summary> /// 當(dāng)迷你小鍵盤按下回車時(shí)觸發(fā)事件 /// </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("值不在最大范圍內(nèi)", "提示"); return; } if (min != 0 && value < min) { MessageBox.Show("值不在最小范圍內(nèi)", "提示"); return; } this.Entered?.Invoke(this, value); } /// <inheritdoc cref="OnEntered"/> private void EnterButton_Click(object sender, EventArgs e) { this.OnEntered(); this.SetContentFocus(); }
以上是“C#如何實(shí)現(xiàn)Winform小數(shù)字鍵盤模擬器”這篇文章的所有內(nèi)容,感謝各位的閱讀!希望分享的內(nèi)容對大家有幫助,更多相關(guān)知識,歡迎關(guān)注億速云行業(yè)資訊頻道!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。