winforms C# Windows窗体焦点开关

raogr8fs  于 2023-06-24  发布在  C#
关注(0)|答案(1)|浏览(149)

我做了一个windows窗体,当用户按回车键时,我希望焦点以特定的顺序从一个控件切换到另一个控件。
当在文本框上触发“离开”事件时,我的问题就出现了,导致它变得不可见。这个'Leave'事件似乎干扰了TextBox_KeyDown函数将焦点转移到不同文本框的能力。尽管TextBox_KeyDown已经切换了焦点,但当将文本框设置为不可见时,“Leave”事件似乎任意选择了一个不同的文本框来聚焦。
下面是我的代码与此问题相关:AddShapeDimRow是我用来向Panel中的TableLayoutPanel添加项的函数。

private void AddShapeDimRow(string dimensionId)
        {
            int height = 25;
            tlpDimensions.RowStyles[0].Height = height +6;
            tlpDimensions.Padding = new Padding(0);

            //Label
            var label = new Label()
            {
                Text = dimensionId,
                Height = height,
                Width = 50,
                Anchor = AnchorStyles.Right,
                TextAlign = ContentAlignment.MiddleRight
            };

            tlpDimensions.Controls.Add(label); // It will be placed in column 0

            //Panel
            var panel = new Panel()
            {
                Height = height,
                Width = 100,
                //Margin = Padding.Empty,
            };

            //Textbox
            var textBox = new TextBox()
            {
                Anchor = AnchorStyles.Left | AnchorStyles.Right,
                AutoSize = false, // Set AutoSize to false
                Height = height,
                Width = 100
            };

            //Hint TextBox
            var textBoxHint = new TextBox()
            {
                Anchor = AnchorStyles.Left | AnchorStyles.Right,
                AutoSize = false, // Set AutoSize to false
                Height = height,
                Width = 100,
                ForeColor = Color.Gray, // Set the color to gray
                Text = "0"
            };

            // Store the ID in the TextBox's Tag property, so we can retrieve it later
            textBox.Tag = dimensionId;
            textBoxHint.Tag = dimensionId + "_Hint";

            // Subscribe to the KeyDown event
            textBox.KeyDown += TextBox_KeyDown;

            // Subscribe to the Leave event
            textBox.Leave += TextBox_Leave;

            // Subscribe to the Enter and Click events for Hint TextBox
            textBoxHint.Enter += (s, e) =>
            {
                // On enter, switch to the TextBox
                textBox.Text = textBoxHint.Text;
                textBox.SelectAll();
                textBox.Visible = true;
                textBoxHint.Visible = false;
                textBox.Focus();
            };
            textBoxHint.Click += (s, e) =>
            {
                // On click, also switch to the TextBox
                textBox.Text = textBoxHint.Text;
                textBox.SelectAll();
                textBox.Visible = true;
                textBoxHint.Visible = false;
                textBox.Focus();
            };

            // Subscribe to the Leave event for TextBox
            textBox.Leave += (s, e) =>
            {
                // On leave, if the TextBox is empty, switch back to the Hint TextBox
                if (string.IsNullOrWhiteSpace(textBox.Text))
                {
                    textBox.Text = "";
                    textBoxHint.Visible = true;
                    textBox.Visible = false;
                }
            };

            textBox.KeyPress += (s, e) =>
            {
                if (!char.IsDigit(e.KeyChar) && !char.IsControl(e.KeyChar))
                {
                    e.Handled = true;
                }
            };

            // Add TextBoxes to the Panel
            panel.Controls.Add(textBoxHint); // It will be placed on the top
            panel.Controls.Add(textBox); // It will be placed on the bottom

            tlpDimensions.Controls.Add(panel); // It will be placed in column 1
        }

        private void TextBox_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                e.SuppressKeyPress = true; // Prevent the beep that Windows makes when you press Enter in a TextBox

                TextBox textBox = sender as TextBox;
                if (textBox.Text.Trim() == "0")
                {
                    textBox.Text = "";
                }

                Control senderControl = (Control)sender;
                Control parentControl = senderControl.Parent; // Get the Panel that contains the TextBox
                int lastControlIndex = tlpDimensions.Controls.Count - 1; // Index of the last control in the TableLayoutPanel

                
                if (tlpDimensions.Controls[lastControlIndex] == parentControl)
                {
                    // If the parent control (Panel) is the last control in the TableLayoutPanel, select txtMark
                    txtMark.Select();
                }
                else
                {
                    // Otherwise, select the next control
                    this.SelectNextControl(senderControl, true, true, true, true);
                }
            }
        }

        private void TextBox_Leave(object sender, EventArgs e)
        {
           //Calls a background function to recalculate the values of hint textboxes.
        }
tkclm6bt

tkclm6bt1#

我按照拉尔夫在评论中的建议做了。我已经修改了我的控件的TabIndex,它自己解决了问题。

相关问题