Как изменить текст метки в форме C # - PullRequest
0 голосов
/ 03 апреля 2019

У меня есть приложение, которое изменяет текст метки на серийный номер, который сканируется с помощью сканера штрих-кода.Код работает до тех пор, пока у меня не появятся кнопки в форме.

Я пытался (1) сфокусироваться на метке, (2) создать групповой блок и поместить метку в групповой блок и сосредоточиться на этом, используя обаПорядок табуляции и такой:

 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.Если посмотреть на пользовательский интерфейс, строка с надписью «Пожалуйста, сканируйте последовательный ...» должна измениться, когда пользователь сканирует штрих-код.

...