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

最佳答案

在您的 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;

如果由于某种原因您不能使用 lambda,则可以关联以下处理程序。如果您有 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;
    //}

关于c# - 我可以禁用 WinForms 中的组合框而不使其变灰吗?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/5507829/

10-11 01:53