C# 生成指定位数的编号

本文介绍使用C#如何生成指定位数的编号。效果如下:

知识点

1、获取键盘按键命令。Keys类

参考代码

csharp 复制代码
// Boolean flag used to determine when a character other than a number is entered.
private bool nonNumberEntered = false;

// Handle the KeyDown event to determine the type of character entered into the control.
private void textBox1_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
{
    // Initialize the flag to false.
    nonNumberEntered = false;

    // Determine whether the keystroke is a number from the top of the keyboard.
    if (e.KeyCode < Keys.D0 || e.KeyCode > Keys.D9)
    {
        // Determine whether the keystroke is a number from the keypad.
        if (e.KeyCode < Keys.NumPad0 || e.KeyCode > Keys.NumPad9)
        {
            // Determine whether the keystroke is a backspace.
            if(e.KeyCode != Keys.Back)
            {
                // A non-numerical keystroke was pressed.
                // Set the flag to true and evaluate in KeyPress event.
                nonNumberEntered = true;
            }
        }
    }
    //If shift key was pressed, it's not a number.
    if (Control.ModifierKeys == Keys.Shift) {
        nonNumberEntered = true;
    }
}

// This event occurs after the KeyDown event and can be used to prevent
// characters from entering the control.
private void textBox1_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e)
{
    // Check for the flag being set in the KeyDown event.
    if (nonNumberEntered == true)
    {
        // Stop the character from being entered into the control since it is non-numerical.
        e.Handled = true;
    }
}

2、TextBox控件的常用事件

KeyPress事件:当文本框取得焦点停驻时,并释放某个键后发生。

Enter 事件:当文本框取得焦点停驻时会有 Enter 事件发生;

Leave 事件:当焦点停驻离开文本框时会有 Leave 事件发生;

TextChanged 事件:当文本框内容有更改时会有 TextChanged 事件发生。

csharp 复制代码
private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
{
    if(e.KeyChar==(char)Keys.Return)
    {
        if(textBox1.Text.Length>8)
        {
            textBox1.Text = textBox1.Text.Substring(0, 8);
        }
        else
        {
            int j =8-textBox1.Text.Length;
            for(int i=0;i<j;i++)
            {
                textBox1.Text = "0" + textBox1.Text;
            }
        }
    }
}

代码

csharp 复制代码
        private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
        {
            if(e.KeyChar==(char)Keys.Return)
            {
                if(textBox1.Text.Length>8)
                {
                    textBox1.Text = textBox1.Text.Substring(0, 8);
                }
                else
                {
                    int j =8-textBox1.Text.Length;
                    for(int i=0;i<j;i++)
                    {
                        textBox1.Text = "0" + textBox1.Text;
                    }
                }
            }
        }
相关推荐
Eiceblue41 分钟前
将 Python 列表导出为 Excel 文件:一维、二维、字典列表
开发语言·python·excel·visual studio code
我是唐青枫8 小时前
C#.NET 索引器完全解析:语法、场景与最佳实践
c#·.net
代码or搬砖8 小时前
String字符串
android·java·开发语言
leo__5209 小时前
基于两步成像算法的聚束模式SAR MATLAB实现
开发语言·算法·matlab
Macbethad10 小时前
自动化测试技术报告
开发语言·lua
不会画画的画师10 小时前
Go开发指南:io/ioutil包应用和迁移指南
开发语言·后端·golang
2503_9284115610 小时前
12.22 wxml语法
开发语言·前端·javascript
59803541510 小时前
【java工具类】小数、整数转中文大写
android·java·开发语言
JIngJaneIL10 小时前
基于java + vue个人博客系统(源码+数据库+文档)
java·开发语言·前端·数据库·vue.js·spring boot
吃喝不愁霸王餐APP开发者10 小时前
Java后端服务在对接全国性霸王餐API时的多数据中心部署与就近调用策略
java·开发语言