我可以在WinForms中禁用组合框而不将其变灰吗?

MBU*_*MBU 4 .net c# controls combobox winforms

有没有办法禁用组合框的值更改而不在Windows窗体中将其变灰?我看到了一些帖子,但他们是为WPF而且没有帮助我的情况.

San*_*ath 6

在你的comobobox意志上设置这些就可以实现你正在寻找的技巧,Combo已启用,但没有人可以更改或输入任何内容,因此Appearance = Enabled,Behavior = Disabled :)

        comboBox1.DropDownHeight = 1;
        comboBox1.KeyDown += (s, e) => e.Handled = true;
        comboBox1.KeyPress += (s, e) => e.Handled = true;
        comboBox1.KeyUp += (s, e) => e.Handled = true;
Run Code Online (Sandbox Code Playgroud)

如果由于某种原因你不能使用lambdas,则可以关联跟随处理程序.右键单击 - >如果你有DropDownStyle = DropDown,则必须另外处理粘贴.

    //void comboBox1_KeyUp(object sender, KeyEventArgs e)
    //{
    //    e.Handled = true;
    //}

    //void comboBox1_KeyPress(object sender, KeyPressEventArgs e)
    //{
    //    e.Handled = true;
    //}

    //void comboBox1_KeyDown(object sender, KeyEventArgs e)
    //{
    //    e.Handled = true;
    //}
Run Code Online (Sandbox Code Playgroud)