代码之家  ›  专栏  ›  技术社区  ›  Mario The Spoon

只读Windows窗体组合框

  •  2
  • Mario The Spoon  · 技术社区  · 14 年前

    有没有办法使windows窗体组合框成为只读的?

    还是使用验证事件的唯一方法?

    当做

    马里奥

    3 回复  |  直到 14 年前
        1
  •  3
  •   Henk Holterman    14 年前

    您可以将DropDownStyle设置为DropDownList,但这实际上不允许键入(但允许使用键盘进行选择)。

    如果您确实希望用户能够键入/看到不完整的单词,则必须使用事件。验证事件将是最佳选择。

        2
  •  3
  •   digEmAll    14 年前

    如果你设置 AutoCompleteMode = SuggestAppend AutoCompleteSource = ListItems 当用户键入内容时,组合框会自动显示以键入的字符开头的条目。

    SelectedIndexChanged SelectedValueChanged

    如果您也绝对不希望用户键入列表中没有的内容,那么是的,您必须处理例如 KeyDown 事件如下:

    private void comboBox1_KeyDown(object sender, KeyEventArgs e)
    {
        char ch = (char)e.KeyValue;
        if (!char.IsControl(ch))
        {
            string newTxt = this.comboBox1.Text + ch;
            bool found = false;
            foreach (var item in this.comboBox1.Items)
            {
                string itemString = item.ToString();
                if (itemString.StartsWith(newTxt, StringComparison.CurrentCultureIgnoreCase))
                {
                    found = true;
                    break;
                }
            }
            if (!found)
                e.SuppressKeyPress = true;
        }
    }
    
        3
  •  0
  •   SUHAIL AG    11 年前

    private void cmbCountry_KeyDown(object sender, KeyEventArgs e)
        {
            char ch = (char)e.KeyValue;
            if (!char.IsControl(ch))
            {
                string newTxt = this.cmbCountry.Text + ch;
                bool found = false;
                foreach (var item in cmbCountry.Items)
                {
                    DataRowView row = item as DataRowView;
                    if (row != null)
                    {
                        string itemString = row.Row.ItemArray[0].ToString();
                        if (itemString.StartsWith(newTxt,     StringComparison.CurrentCultureIgnoreCase))
                        {
                            found = true;
                            break;
                        }
                    }
                    else
                        e.SuppressKeyPress = true;
                }
                if (!found)
                    e.SuppressKeyPress = true;
            }
        }
    
    推荐文章