C#实现输入内容长度检查

在工作中,一些工作场景下,需要对我们录入的数据做一些简单的准确性判定。好比录入数据的长度则是此中一种比力直不雅的判定。下面,我们以一个C#winfrom程式编写长度验证的布景来看看,C#事项录入数据长度查抄的实现。

东西/原料

  • 电脑

方式/步调

  1. 1

    在建一个winfrom项目,在上面添加两个label控件和两个textbox控件,一个用于填写要管控的长度设置,一个用来领受录入的数据。

  2. 2

    编写查抄函数checklength().先把两个textbox框中的长度确定出来。

      private void checklength()

            {

                int m = int.Parse(textBox1.Text);

                int n = textBox2.Text.Length;

            }

  3. 3

    判定语句编写。

     private void checklength()

            {

                int m = int.Parse(textBox1.Text);//界说长度

                int n = textBox2.Text.Length;//录入内容长度

                if (m != n)

                {

                    MessageBox.Show("输入长度错误!");

                }

                else

                {

                    MessageBox.Show("OK!");

                }

  4. 4

    完美下代码,条码长度错误今后让输入框变为红色,并把输入内容全数选中。

      private void checklength()

            {

                int m = int.Parse(textBox1.Text);//界说长度

                int n = textBox2.Text.Length;//录入内容长度

                if (m != n)

                {

                    MessageBox.Show("输入长度错误!");

                    textBox2.BackColor = Color.Red;

                    textBox2.SelectAll();

                }

                else

                {

                    MessageBox.Show("OK!");

                    textBox2.BackColor = Color.White;

                    textBox2.SelectAll();

                }

            }

  5. 5

    操纵回车事务来验证录入的内容长度是否合适自界说长度。

           private void textBox2_KeyDown(object sender, KeyEventArgs e)

            {

                if (e.KeyCode == Keys.Enter)

                {

                        checklength();

                }

            }

        }

  6. 6

    验证成果合适要求,附完整源代码:

    using System;

    using System.Collections.Generic;

    using System.ComponentModel;

    using System.Data;

    using System.Drawing;

    using System.Linq;

    using System.Text;

    using System.Windows.Forms;

    namespace WindowsFormsApplication1

    {

        public partial class Form1 : Form

        {

            public Form1()

            {

                InitializeComponent();

            }

            private void checklength()

            {

                int m = int.Parse(textBox1.Text);//界说长度

                int n = textBox2.Text.Length;//录入内容长度

                if (m != n)

                {

                    MessageBox.Show("输入长度错误!");

                    textBox2.BackColor = Color.Red;

                    textBox2.SelectAll();

                }

                else

                {

                    MessageBox.Show("OK!");

                    textBox2.BackColor = Color.White;

                    textBox2.SelectAll();

                }

            }

            private void textBox2_KeyDown(object sender, KeyEventArgs e)

            {

                if (e.KeyCode == Keys.Enter)

                {

                    

                        checklength();

                    

                }

            }

        }

    }

  • 发表于 2019-04-03 20:02
  • 阅读 ( 810 )
  • 分类:其他类型

你可能感兴趣的文章

相关问题

0 条评论

请先 登录 后评论
联系我们:uytrv@hotmail.com 问答工具