SoFunction
Updated on 2025-03-06

C# Use TextBox as data input method

Recently, the author needs to interact with data between the upper and lower computers. After extensive reference to the big guy's information, he has a relatively complete function of using the Textbox control for data input.
The main functions of the program block: implement input data and convert it into a byte array and then send it to the lower computer through the serial port.

Read data from the TextBox control and send

private void Botton_Float_Click(object sender, EventArgs e)
 {
     if ( == "Close the serial port")
     {
         if(TextBox_Tem_Cal.Text != String .Empty) //Discern whether the data input box is empty         {
             HexMath CRC = new HexMath();
             Byte[] buffer = new Byte[6];
             
             float tem_cal_float = (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]);
             (buffer, 0, 6);
         }
        else
         {
             ("Calibration data cannot be empty");
         }
     }
     else
     {
         ("Serial port not opened");
     }
 }

Limit the input data of the TextBox control

private void TextBox_Tem_Cal_KeyPress(object sender, KeyPressEventArgs e)//The event is triggered when the key is pressed in TextBox, so that only numbers can be entered{
    //Judge whether the key is the type to be entered.    if (((int) < 48 || (int) > 57) && (int) != 8 && (int) != 46)
         = true;

    //The processing of decimal points.    if ((int) == 46)                           //Decimal point    {
        if (TextBox_Tem_Cal. <= 0)
             = true;   //The decimal point cannot be in the first position        else
        {
            float f;
            float oldf;
            bool b1 = false, b2 = false;
            b1 = (TextBox_Tem_Cal.Text, out oldf);
            b2 = (TextBox_Tem_Cal.Text + (), out f);
            if (b2 == false)
            {
                if (b1 == true)
                     = true;
                else
                     = false;
            }
        }
    }
}

Float and byte arrays

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 < ; i++)
            {
                *((byte*)pf + i) = *(px + i);
            }
        }
        return a;
    }
}

Program reference:

TextBox input limit
C# byte and float conversion

This is the article about C# using TextBox as data input method. For more related C# TextBox data input content, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!