In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly introduces how to implement the Winform small numeric keyboard simulator in C#. It is very detailed and has a certain reference value. Interested friends must finish reading it!
Before you start the article, take a look at the renderings to see if it's what you need:
I. build the interface of the calculator
It takes a little thought to build a good-looking calculator interface, and it took me two or three hours to build it when I did this.
Its main usage control is the TableLayoutPanel control.
Another small difficulty is the display of the content control Textbox, which takes some care to center the text vertically without rewriting the Textbox control.
The other interfaces are fine. As for the plus or minus sign, you can use the special symbol of the input method.
Second, build the open properties of the control
A total of 3 attributes have been opened, which are not enough to add by yourself. These three are as follows. If you look at the notes, you should be able to understand:
/ / the minimum acceptable value is-3.402823E+38/// [Browsable (true)] [Category ("Zhongzhou")] [DefaultValue (0)] [Description ("acceptable minimum, minimum is-3.402823E+38")] public float Min {get; set;} = 0 / / the maximum acceptable value is 3.402823E+38/// [Browsable (true)] [Category ("Zhongzhou")] [DefaultValue (0)] [Description ("maximum acceptable value, maximum is 3.402823E+38")] public float Max {get; set;} = 0 / sets the precision of the decimal point. Default is 2 decimal places. Default is 2 decimal places / [Browsable (true)] [Category ("Zhongzhou")] [DefaultValue (2)] [Description ("set the precision number of decimal places, default to 2 decimal places")] public int Precision {get; set;} = 2. Control keyboard input.
Our goal is to allow the keypad to enter numbers, so we need to prohibit the physical keyboard from entering information such as text and letters, and small number dots can only appear once at most. The specific logic is as follows:
/ trigger / private void OnKeyPressed (KeyPressEventArgs e) {/ / 13 indicates enter if (e.KeyChar = = 13) {this.OnEntered (); e.Handled = true; return;} / / 48 represents 0line 57 represents 9 line 8 represents spaces, 46 represents decimal point if ((e.KeyChar)) when you type text content using a physical keyboard
< 48 || e.KeyChar >= 57) & & (e.KeyChar! = 8) & & (e.KeyChar! = 46) {e.Handled = true; return;} / / determines that the decimal point can be entered multiple times. Only one decimal point if (e.KeyChar = = 46) {this.PointHandle (); this.SetContentFocus (); e.Handled = true; return is allowed. }} / processing the decimal point / indicates that private bool PointHandle () {string content = this.ContentTextBox.Text; if (content.IndexOf ('.')! =-1) {return false;} if (string.IsNullOrEmpty (content)) {this.SetContent ("0."); return true } / / take cursor position 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));} IV. Let the text box handle the focus state and cursor position.
Cursor position, need special treatment, the default parameter cursorPosition=- 1, the cursor position is always moved to the end. But in some cases, for example, if you want the cursor to delete or add a few numbers in the middle of the number, you can't let the cursor run to the end automatically.
/ / set a new value / indicates the new value 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;} 5. Realize backspace and clear content / private void ClearButton_Click (object sender, EventArgs e) {this.SetContent (string.Empty); this.SetContentFocus () } / backspace content / private void BackButton_Click (object sender, EventArgs e) {/ / take cursor position int index = this.ContentTextBox.SelectionStart; / / cut content string cutStr = this.ContentTextBox.Text.Substring (0, index); / / remaining content 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);} VI. Realize the function of Enter to confirm the result.
The principle is realized through events. The code is as follows:
/ / event delegation when the enter button is pressed / public delegate void EnteredEventHandler (object sender, float e); / event when the enter button is pressed / / public event EnteredEventHandler Entered; / event triggered when the mini-keyboard presses enter / 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 ("value is not in maximum range", "prompt"); return;} if (min! = 0 & & value < min) {MessageBox.Show ("value is not in minimum range", "prompt"); return } this.Entered?.Invoke (this, value);} / / private void EnterButton_Click (object sender, EventArgs e) {this.OnEntered (); this.SetContentFocus ();} above is all the content of the article "how to implement Winform small numeric keypad simulator in C#". Thank you for reading! Hope to share the content to help you, more related knowledge, welcome to follow the industry information channel!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.