服务器之家:专注于VPS、云服务器配置技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - C# - C#实现Winform小数字键盘模拟器

C#实现Winform小数字键盘模拟器

2022-12-08 11:32中洲少年 C#

本文主要介绍了C#实现Winform小数字键盘模拟器,文中通过示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

文章开始之前,先看一下效果图,看是不是您正所需要的:

C#实现Winform小数字键盘模拟器

一、构建计算器的界面

要构建出一个好看点的计算器界面,还是需要颇费些小心思的,我做这个的时候,也花了两三个小时的时间构建这个界面。

其主要的使用控制是TableLayoutPanel控件。

另外一个小难点则在于内容控件Textbox的显示,要让文字垂直居中,在没有重写Textbox控件的情况下要达到这个效果,也是花了些小心思。

其它的界面则没有什么的。至于加减号嘛,则用输入法的特殊符号即可。

二、构建控件的开放属性

一共开放了3个属性,不够自己加。这3个如下,看注释应该能懂:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
/// <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;

三、控件键盘输入

我们的目的是让小键盘来输入数字,所以需要禁止实体键盘输入文字字母等信息,以及小数字点最多只能出现一次,具体逻辑如下:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
/// <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时,光标位置始终移到最末尾处。但是有些情况,比如你要让光标在数字中间删除几个数字或者添加几个数字,就不能让光标自动跑到最末尾处了。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
/// <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;
}

五、实现退格、清除内容的功能

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
/// <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确认得到结果的功能

原理是通过事件来实现的。代码如下:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
/// <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();
}

到此这篇关于C#实现Winform小数字键盘模拟器的文章就介绍到这了,更多相关C# Winform数字键盘模拟器内容请搜索服务器之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持服务器之家!

原文链接:https://blog.csdn.net/mazhiyuan1981/article/details/121280371

延伸 · 阅读

精彩推荐
  • C#C#中Abstract 、Virtual和Override的使用及区别

    C#中Abstract 、Virtual和Override的使用及区别

    C#中virtual,abstract,override用于方法重载,子类覆盖了父类的相同方法,父类中的实现不可能再被外面调用。本文给大家重点介绍C#中Abstract 、Virtual和Overrid...

    雪人学习8772022-11-22
  • C#C# 类的声明详解

    C# 类的声明详解

    本文主要对C# 类的声明进行详细介绍。具有一定的参考价值,下面跟着小编一起来看下吧...

    liyongke7852021-12-20
  • C#c#基于NVelocity实现代码生成

    c#基于NVelocity实现代码生成

    这篇文章主要介绍了c#基于NVelocity实现代码生成的方法,帮助大家更好的理解和使用c#,感兴趣的朋友可以了解下...

    code2roc3512022-10-31
  • C#unity实现弧形移动 可角度自定

    unity实现弧形移动 可角度自定

    这篇文章主要为大家详细介绍了unity实现弧形移动,可角度自定,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...

    于子潇9622022-09-16
  • C#VSCode调试C#程序及附缺失.dll文件的解决办法

    VSCode调试C#程序及附缺失.dll文件的解决办法

    这篇文章主要介绍了VSCode调试C#程序及附缺失.dll文件的解决办法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需...

    khq溪风8442022-10-10
  • C#C# 获取动态key的json对象的值案例

    C# 获取动态key的json对象的值案例

    这篇文章主要介绍了C# 获取动态key的json对象的值案例,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧...

    冬冬冬冬冬冬冬冬冬冬4742022-10-31
  • C#C#文件目录操作方法汇总

    C#文件目录操作方法汇总

    本文主要列举出C#文件和目录操作的一些方法,包括创建、移动、遍历目录,读写文件等方法,有需要的小伙伴可以学习一下。...

    jerrylsxu7932021-11-19
  • C#详解C# 线程的挂起与唤醒

    详解C# 线程的挂起与唤醒

    这篇文章主要介绍了详解C# 线程的挂起与唤醒,帮助大家更好的理解和学习使用c#,感兴趣的朋友可以了解下...

    jack_Meng9432022-11-16