开发者

setting Back color of a disabled combo to white color

开发者 https://www.devze.com 2023-03-08 04:14 出处:网络
hi I have a combo box which can enabled a开发者_开发知识库nd 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?I found follow

hi I have a combo box which can enabled a开发者_开发知识库nd 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?


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.


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


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

As stated below this does NOT work.


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

精彩评论

暂无评论...
验证码 换一张
取 消