C#使用TextBox作數(shù)據(jù)輸入方法
筆者最近需要上位機(jī)與下位機(jī)進(jìn)行數(shù)據(jù)交互,在廣泛參考大佬的資料后,較為完善地使用Textbox控件進(jìn)行數(shù)據(jù)輸入的功能。
程序段主要功能:實(shí)現(xiàn)輸入數(shù)據(jù)并轉(zhuǎn)換成byte數(shù)組再通過(guò)串口發(fā)送至下位機(jī)。
讀取TextBox控件中數(shù)據(jù)并發(fā)送
private void Botton_Float_Click(object sender, EventArgs e)
{
if (button1.Text == "關(guān)閉串口")
{
if(TextBox_Tem_Cal.Text != String .Empty) //判斷數(shù)據(jù)輸入框是否為空
{
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("校準(zhǔn)數(shù)據(jù)不能為空");
}
}
else
{
MessageBox.Show("串口未打開(kāi)");
}
}
限制TextBox控件輸入數(shù)據(jù)
private void TextBox_Tem_Cal_KeyPress(object sender, KeyPressEventArgs e)//在TextBox中按下按鍵時(shí)觸發(fā)事件,保證只能輸入數(shù)字
{
//判斷按鍵是不是要輸入的類(lèi)型。
if (((int)e.KeyChar < 48 || (int)e.KeyChar > 57) && (int)e.KeyChar != 8 && (int)e.KeyChar != 46)
e.Handled = true;
//小數(shù)點(diǎn)的處理。
if ((int)e.KeyChar == 46) //小數(shù)點(diǎn)
{
if (TextBox_Tem_Cal.Text.Length <= 0)
e.Handled = true; //小數(shù)點(diǎn)不能在第一位
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數(shù)組 互轉(zhuǎn)
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轉(zhuǎn)換
到此這篇關(guān)于C#使用TextBox作數(shù)據(jù)輸入方法的文章就介紹到這了,更多相關(guān)C# TextBox數(shù)據(jù)輸入內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
C#動(dòng)態(tài)創(chuàng)建button按鈕的方法實(shí)例詳解
這篇文章主要介紹了C#動(dòng)態(tài)創(chuàng)建button按鈕的方法實(shí)例詳解的相關(guān)資料,需要的朋友可以參考下2017-06-06
C#針對(duì)xml基本操作及保存配置文件應(yīng)用實(shí)例
這篇文章主要介紹了C#針對(duì)xml基本操作及保存配置文件應(yīng)用實(shí)例,包括了針對(duì)XML文件的定義、初始化、創(chuàng)建、以及增刪改查等基礎(chǔ)操作,并配有詳細(xì)的實(shí)例加以說(shuō)明,需要的朋友可以參考下2014-10-10
C#實(shí)現(xiàn)簡(jiǎn)單的3DES加密解密功能示例
這篇文章主要介紹了C#實(shí)現(xiàn)簡(jiǎn)單的3DES加密解密功能,結(jié)合實(shí)例形式分析了C#實(shí)現(xiàn)3DES加密解密的定義、使用等具體步驟與相關(guān)操作技巧,需要的朋友可以參考下2017-08-08
C#實(shí)現(xiàn)身份證驗(yàn)證功能的示例代碼
這篇文章主要為大家詳細(xì)介紹了如何利用C#實(shí)現(xiàn)身份證驗(yàn)證功能,文中的示例代碼講解詳細(xì),對(duì)我們學(xué)習(xí)C#有一定的幫助,感興趣的小伙伴可以跟隨小編一起了解一下2022-12-12
C#使用DateAndTime.DateDiff實(shí)現(xiàn)計(jì)算年齡
這篇文章主要為大家詳細(xì)介紹了C#如何使用DateAndTime.DateDiff實(shí)現(xiàn)根據(jù)生日計(jì)算年齡,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以了解一下2024-01-01
在C#中g(shù)lobal關(guān)鍵字的作用及其用法
global 是 C# 2.0 中新增的關(guān)鍵字,理論上說(shuō),如果代碼寫(xiě)得好的話(huà),根本不需要用到它,但是不排除一些特別的情況,比如修改別人的代碼,本文僅舉例說(shuō)明。2016-03-03

