I have a textbox, I wana enter 4 digit and when I press enter it should show a label I have add the label but its not working the code I have look more like this

if(e.KeyChar == 4)
        e.Handled = true;

Any help will be highly Appreciated

Thanx in advance


I wana enter 4 digit

means you have to allow only 4 digits?


you mean, when you press '4'
If so, put quote before 4 in

if(e.KeyChar == 4)
if(e.KeyChar == '4')

because u checking the char

I mean I wana allow 4 digits that is 1234 then when I press enter the label should be added

Then you need to change the keypress value to that of the return key, as well as montior for the length of input to be 4

Use MaxLength of TextBox to restrict your input only 4 character. or use MaskedTextBox for input only digits.

namespace car_radio
    public partial class Form1 : Form

        Byte[] data = new byte[1];
        int car;
        int car1;
        public Form1()

        private void Form1_Load(object sender, EventArgs e)

        private void button6_Click(object sender, EventArgs e)
            //box color
            button1.BackColor = Color.Black;
            button2.BackColor = Color.Black;
            button3.BackColor = Color.Black;
            button4.BackColor = Color.Black;
            button5.BackColor = Color.Black;
            button6.BackColor = Color.Green;
            button9.BackColor = Color.Black;
            button13.BackColor = Color.Black;
            button14.BackColor = Color.Black;
            button15.BackColor = Color.Black;

            car = 5;
            data[0] = Convert.ToByte(car);
            serialPort1.Write(data, 0, 1);
            textBox1.Text = "รถเดินหน้าเลี้ยวซ้าย";
            label1.Text = "" + car;
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.