C#使用TextBox作数据输入方法

作者:怪都督 时间:2023-11-16 22:26:14 

笔者最近需要上位机与下位机进行数据交互,在广泛参考大佬的资料后,较为完善地使用Textbox控件进行数据输入的功能。
程序段主要功能:实现输入数据并转换成byte数组再通过串口发送至下位机。

读取TextBox控件中数据并发送


private void Botton_Float_Click(object sender, EventArgs e)
{
    if (button1.Text == "关闭串口")
    {
        if(TextBox_Tem_Cal.Text != String .Empty) //判断数据输入框是否为空
        {
            HexMath CRC = new HexMath();
            Byte[] buffer = new Byte[6];

float tem_cal_float = float.Parse(TextBox_Tem_Cal.Text);
            Byte[] float_byte_array = new Byte[4];
            float_byte_array = FloatToBytes(tem_cal_float);

buffer[0] = float_byte_array[0];
            buffer[1] = float_byte_array[1];
            buffer[2] = float_byte_array[2];
            buffer[3 ] = float_byte_array[3];

CRC.CalculateCrc16(buffer, out buffer[5], out buffer[4]);
            serialPort1.Write(buffer, 0, 6);
        }
       else
        {
            MessageBox.Show("校准数据不能为空");
        }
    }
    else
    {
        MessageBox.Show("串口未打开");
    }
}

限制TextBox控件输入数据


private void TextBox_Tem_Cal_KeyPress(object sender, KeyPressEventArgs e)//在TextBox中按下按键时触发事件,保证只能输入数字
{
   //判断按键是不是要输入的类型。
   if (((int)e.KeyChar < 48 || (int)e.KeyChar > 57) && (int)e.KeyChar != 8 && (int)e.KeyChar != 46)
       e.Handled = true;

//小数点的处理。
   if ((int)e.KeyChar == 46)                           //小数点
   {
       if (TextBox_Tem_Cal.Text.Length <= 0)
           e.Handled = true;   //小数点不能在第一位
       else
       {
           float f;
           float oldf;
           bool b1 = false, b2 = false;
           b1 = float.TryParse(TextBox_Tem_Cal.Text, out oldf);
           b2 = float.TryParse(TextBox_Tem_Cal.Text + e.KeyChar.ToString(), out f);
           if (b2 == false)
           {
               if (b1 == true)
                   e.Handled = true;
               else
                   e.Handled = false;
           }
       }
   }
}

Float 与 byte数组 互转


private static byte[] FloatToBytes(float data)
{
   unsafe
   {
       byte* pdata = (byte*)&data;
       byte[] byteArray = new byte[sizeof(float)];
       for (int i = 0; i < sizeof(float); ++i)
           byteArray[i] = *pdata++;
       return byteArray;
   }
}
private static float BytesToFloat(byte[] data)
{
   unsafe
   {
       float a = 0.0F;
       byte i;
       byte[] x = data;
       void* pf;
       fixed (byte* px = x)
       {
           pf = &a;
           for (i = 0; i < data.Length; i++)
           {
               *((byte*)pf + i) = *(px + i);
           }
       }
       return a;
   }
}

程序参考:

TextBox输入限制
C# byte与float转换

来源:https://blog.csdn.net/weixin_44625313/article/details/114329916

标签:C#,TextBox,输入
0
投稿

猜你喜欢

  • Android 如何使用短信链接打开APP

    2022-01-09 02:45:25
  • Silverlight将图片转换为byte的实现代码

    2022-03-07 11:07:15
  • 关于Jsoup将相对路径转为绝对路径的方法

    2022-03-11 04:13:50
  • MyBatis中使用foreach循环的坑及解决

    2023-11-02 12:47:51
  • java面向对象:API(接口)与集合(ArrayList)

    2021-06-07 03:28:19
  • VC对自定义资源加密解密(AES)的详解

    2023-01-10 00:27:15
  • 基于C#调用c++Dll结构体数组指针的问题详解

    2021-12-10 23:16:41
  • 详解C#借助.NET框架中的XmlTextReader类读取XML的方法

    2023-01-18 23:31:47
  • 安卓系统中实现摇一摇画面振动效果的方法

    2023-12-05 02:53:53
  • C#中label内容显示不全、不完整的解决方法

    2022-09-13 02:43:46
  • c# rsa加密解密详解

    2023-06-11 00:54:17
  • Java实现顺序栈原理解析

    2021-08-26 15:47:16
  • Android调用google地图生成路线图实现代码

    2023-06-04 09:37:25
  • Spring中多配置文件及引用其他bean的方式

    2023-07-01 17:31:03
  • Android 应用指定浏览器开发实例

    2022-02-10 03:34:09
  • 关于fastjson的@JSONField注解的一些问题(详解)

    2021-11-23 09:09:34
  • Java中的ArrayList容量及扩容方式

    2023-10-17 17:24:40
  • C#中正则表达式(Regex)过滤内容的基本使用方法

    2023-11-26 12:51:00
  • 简单解析java方法在调用在内存中的执行过程

    2022-04-12 15:46:26
  • Android编程中自定义dialog用法实例

    2023-05-10 07:21:37
  • asp之家 软件编程 m.aspxhome.com