c#回车键事件

发布时间 2023-07-25 13:27:11作者: 拿受用

c#回车键

输入框,如果普通的不能输入就用
image
可以设置几个false,就和普通的输入框一样了
image

设置回车键事件
当你输入的时候判断是不是回车键
image

代码

 private void richTextBox1_KeyDown(object sender, KeyEventArgs e)
        {
            string inputValue = richTextBox1.Text;


            bool judge = CommonMethod.judgeKey(inputValue, e);

            if (judge == true)
            {
                this.connectSettingManage.ProfinetWriteDouble(this.siemensPlc, "DB66.DBD432", float.Parse(inputValue));
                MessageBox.Show("修改成功");
            }
        }

多个方法使用抽象成一个方法

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace VCSMill
{
    class CommonMethod
    {
        public static bool judgeKey(String inputValue, KeyEventArgs e)
        {

           

            //if条件检测按下的是不是Enter键,两个判断,,这个一定要放在第一个
            if (e.KeyCode != Keys.Enter)
            {
                return false;
            }
            else
            {
                if (inputValue.Equals(""))
                {
                    MessageBox.Show("输入不能为空");
                    return false;
                }

                // 获取输入框的值
                if (Regex.IsMatch(inputValue, @"[^0-9.]"))
                {
                    MessageBox.Show("输入失败!请输入有效的数字。");
                    return false;
                }

                float inputIntValue = float.Parse(inputValue);
                if (inputIntValue > 200.0)
                {
                    MessageBox.Show("输入失败!请输入的数字在有效范围内");
                    return false;
                }

            }



            return true;
        }
    }
}

演示结果

输入回车之后就能忘plc传入值
image