将禁用组合的背景颜色设置为白色

发布于 2024-11-09 12:39:25 字数 66 浏览 0 评论 0原文

你好 我有一个可以在运行时启用和禁用的组合框。现在我需要使背景颜色保持不变,即使它被启用或禁用。任何人都可以帮助我吗?

hi
I have a combo box which can enabled and disabled at run time.now i needed to make make the back color constant even if it is enabled or disabled.any one can help me?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

紫竹語嫣☆ 2024-11-16 12:39:25

我找到了以下解决方案。

解决方案 1:

在禁用时将下拉样式设置为 "DropDownList",然后在启用控件时将其重置为 "DropDown"

combobox.DropDownStyle = ComboBoxStyle.DropDownList;

解决方案 2:

转到此处 http://www.codeproject.com/Articles/ 22454/ReadOnly-ComboBox

第一个解决方案对我有用,第二个解决方案你可以尝试一下。

I found following solutions.

Solution 1:

set the dropdownstyle to be "DropDownList" when disabled and then reset it to "DropDown" when you enabled the control

combobox.DropDownStyle = ComboBoxStyle.DropDownList;

Solution 2:

Go here http://www.codeproject.com/Articles/22454/ReadOnly-ComboBox

First solution work for me and 2nd solution u can try it.

淡忘如思 2024-11-16 12:39:25

这对我有用

comboBox1.DropDownHeight = 1;
comboBox1.KeyDown += new KeyEventHandler(comboBox1_KeyDown);
comboBox1.KeyPress += new KeyPressEventHandler(comboBox1_KeyPress);
comboBox1.KeyUp += new KeyEventHandler(comboBox1_KeyUp);

现在在每个处理程序中只需设置 e.Handled = true

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;
}

现在,当您必须将其用作启用时,只需删除处理程序并设置 DropDownHeight

comboBox1.KeyDown -= new KeyEventHandler(comboBox1_KeyDown);
comboBox1.KeyPress -= new KeyPressEventHandler(comboBox1_KeyPress);
comboBox1.KeyUp -= new KeyEventHandler(comboBox1_KeyUp);

This worked for me

comboBox1.DropDownHeight = 1;
comboBox1.KeyDown += new KeyEventHandler(comboBox1_KeyDown);
comboBox1.KeyPress += new KeyPressEventHandler(comboBox1_KeyPress);
comboBox1.KeyUp += new KeyEventHandler(comboBox1_KeyUp);

Now in each of this handlers just set e.Handled = true

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;
}

Now when you have to function it as a Enabled just remove the handlers and set the DropDownHeight

comboBox1.KeyDown -= new KeyEventHandler(comboBox1_KeyDown);
comboBox1.KeyPress -= new KeyPressEventHandler(comboBox1_KeyPress);
comboBox1.KeyUp -= new KeyEventHandler(comboBox1_KeyUp);
旧情勿念 2024-11-16 12:39:25

如果这是 WinForms,则将 BackColor 属性设置为您想要的任何值。

如下所述,这不起作用。

If this is WinForms then set the BackColor Property to whatever you want it to be.

As stated below this does NOT work.

感受沵的脚步 2024-11-16 12:39:25

根据您的具体需求,谷歌搜索 为我找到了一个潜在的解决方案:

如果您想要使组合框处于禁用状态(不可更改,但看起来与启用时相同),则快速将 Enabled 属性从 true 设置为 false,然后再次按回车键即可实现它,尽管方式有点hacky:

bool isDisabled = true;
private void comboBox1_Enter(object sender, EventArgs e)
{
    if(isDisabled)
    {
        comboBox1.Enabled = false;
        comboBox1.Enabled = true;
    }
}

Depending on exactly what you are after, Googling has found me a potential solution:

If what you're after is to get the combobox in a disabled state (unchangable, but looking the same as when enabled), then quickly setting the Enabled property from true, to false, then back again on Enter achieves it, although in a somewhat hacky manner:

bool isDisabled = true;
private void comboBox1_Enter(object sender, EventArgs e)
{
    if(isDisabled)
    {
        comboBox1.Enabled = false;
        comboBox1.Enabled = true;
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文