如何以C#形式更改标签文本

时间:2019-04-03 16:42:18

标签: c# label textchanged

我有一个将标签文本更改为使用条形码扫描仪扫描的序列号的应用程序。该代码将一直工作到表单上没有按钮为止。

我尝试过(1)专注于标签,(2)创建一个分组框,然后使用Tab键顺序和以下方式将标签放到该分组框中并专注于此:

 this.ActiveControl = groupBox2;

两者都不起作用。

以下是概述应用程序的代码:

 private void Form1_Load(object sender, EventArgs e)
    {
        this.ActiveControl = groupBox2;

        this.KeyDown += new KeyEventHandler(Form1_KeyDown);
        this.KeyUp += new KeyEventHandler(Form1_KeyUp);
    }

    private void Form1_KeyUp(object sender, KeyEventArgs e)
    {
        // if keyboard input is allowed to read
        if (UseKeyboard && e.KeyData != Keys.Enter)
        {
            MessageBox.Show(e.KeyData.ToString());
        }

        /* check if keydown and keyup is not different
         * and keydown event is not fired again before the keyup event fired for the same key
         * and keydown is not null
         * Barcode never fired keydown event more than 1 time before the same key fired keyup event
         * Barcode generally finishes all events (like keydown > keypress > keyup) of single key at a time, if two different keys are pressed then it is with keyboard
         */
        if (cforKeyDown != (char)e.KeyCode || cforKeyDown == '\0')
        {
            cforKeyDown = '\0';
            _barcode.Clear();
            return;
        }

        // getting the time difference between 2 keys
        int elapsed = (DateTime.Now.Millisecond - _lastKeystroke);

        /*
         * Barcode scanner usually takes less than 17 milliseconds to read, increase this if neccessary of your barcode scanner is slower
         * also assuming human can not type faster than 17 milliseconds
         */
        if (elapsed > 17)
            _barcode.Clear();

        // Do not push in array if Enter/Return is pressed, since it is not any Character that need to be read
        if (e.KeyCode != Keys.Return)
        {
            _barcode.Add((char)e.KeyData);
        }

        // Barcode scanner hits Enter/Return after reading barcode
        if (e.KeyCode == Keys.Return && _barcode.Count > 0)
        {
            string BarCodeData = new String(_barcode.ToArray());
            if (!UseKeyboard)
                //MessageBox.Show(String.Format("{0}", BarCodeData));
                label1.Text = String.Format("{0}", BarCodeData);
            _barcode.Clear();
        }

        // update the last key press strock time
        _lastKeystroke = DateTime.Now.Millisecond;
    }

同样,只要我松开按钮,它就会起作用。 这是我的用户界面的图片:

enter image description here

同样,标签会更改,直到我开始向WinForm添加按钮。 在用户界面上,当用户扫描条形码时,写着“ Please Scan Serial ...”的行应更改。

0 个答案:

没有答案