添加到组合框的文本将突出显示

时间:2015-06-12 21:12:56

标签: c#

我有一种方法可以根据最后关注的内容在组合框或文本框的文本中附加或插入字符。我使用按钮传递角色作为参数,使用键盘或sendkeys不是一个选项。当我的方法将字符附加或插入到文本框中时,结果与预期的一样,但是当对组合框应用相同的方法时,组合框的文本会突出显示。显然这不是我正在寻找的功能,我相信这会阻止自动完成模式正常工作。

为什么组合框的行为与文本框不同?

代码:

private void createText(string lowerCaseChar, string upperCaseChar)
{
    Control FocusedTextComboBox;
    switch (lastTextComboBoxFocused)
    {
        case 54:
            FocusedTextComboBox = SearchTextBox;
            break;
        case 4:
            FocusedTextComboBox = VendorComboBox;
            break;
        case 6:
            FocusedTextComboBox = SectionComboBox;
            break;
        case 5:
            FocusedTextComboBox = DeptComboBox;
            break;
        default:
            FocusedTextComboBox = SearchTextBox;
            break;
    }

    if (FocusedTextComboBox is TextBox)
    {
        TextBox FocusedTextBox = (TextBox)FocusedTextComboBox;

        int SelectionStartNumber = FocusedTextBox.SelectionStart;

        switch (shift)
        {
            case true:
                FocusedTextBox.Text = FocusedTextBox.Text.Insert(FocusedTextBox.SelectionStart, upperCaseChar);
                break;
            case false:
                FocusedTextComboBox.Text = FocusedTextComboBox.Text.Insert(FocusedTextBox.SelectionStart, lowerCaseChar);
                break;
        }
        FocusedTextBox.SelectionStart = SelectionStartNumber + 1;
        FocusedTextBox.Focus();
    }
    else
    {
        ComboBox FocusedComboBox = (ComboBox)FocusedTextComboBox;

        if (FocusedComboBox.SelectionStart == 0 && FocusedComboBox.Text != "")
        {
            switch (shift)
            {
                case true:
                    FocusedComboBox.Text += upperCaseChar;
                    break;
                case false:
                    FocusedComboBox.Text += lowerCaseChar;
                    break;
            }
        }
        else
        {
            int SelectionStartNumber = FocusedComboBox.SelectionStart;

            switch (shift)
            {
                case true:
                    FocusedComboBox.Text = FocusedComboBox.Text.Insert(FocusedComboBox.SelectionStart, upperCaseChar);
                    break;
                case false:
                    FocusedTextComboBox.Text = FocusedTextComboBox.Text.Insert(FocusedComboBox.SelectionStart, lowerCaseChar);
                    break;
            }
            FocusedComboBox.SelectionStart = SelectionStartNumber + 1;
        }
        FocusedComboBox.Focus();
    }
} 

1 个答案:

答案 0 :(得分:0)

我认为将焦点设置回组合框是突出显示文本,在将文本分配给组合框之前移动FousedCombox.Focus()。