Reputation: 368
How to hide DropDownList in ComboBox?
I want use ComboBox only for displaying text.
This control look nice, for me is better than TextBox plus Button.
So control must be enabled, but without any items.
When user click arrow (or alt + down key) DropDownList should'n show, because ill select value from custom DataGridView in order to fill back text in ComboBox.
Edit. Alternative solution is set DropDownHeight to 1, with show only 1 pixel line after clicking control.
Edit. Real solution. Answer below
Upvotes: 3
Views: 10231
Reputation:
[Updated]
OK, you can't set the Combo Box to Read Only, but you can set Enabled = false
.
I've never tried this, but perhaps you could set the MaxDropDownItems
to 0.
But, yo'd still set the Combo Box's Text to the value you want in code.
[Edit]
Another idea: Set DropDownHeight
to 0 (...or 1 if it won't accept 0).
Upvotes: 1
Reputation: 2324
You can intercept the messages that cause the box to drop-down, in a subclass. The following snippet defines a control NoDropDownBox, that ignores the mouse clicks that result in a drop-down of the combo box:
public class NoDropDownBox : ComboBox
{
public override bool PreProcessMessage(ref Message msg)
{
int WM_SYSKEYDOWN = 0x104;
bool handled = false;
if (msg.Msg == WM_SYSKEYDOWN)
{
Keys keyCode = (Keys)msg.WParam & Keys.KeyCode;
switch (keyCode)
{
case Keys.Down:
handled = true;
break;
}
}
if(false==handled)
handled = base.PreProcessMessage(ref msg);
return handled;
}
protected override void WndProc(ref Message m)
{
switch(m.Msg)
{
case 0x201:
case 0x203:
break;
default:
base.WndProc(ref m);
break;
}
}
}
Upvotes: 6
Reputation: 3604
If the DropDownStyle is set to DropDown and the Text is set to "Something" then your ComboBox won't dropdown when user clicks the button.
At least, I'm getting that behaviour in WinForms (C# 4.0). Is that what you are trying to achieve?
Upvotes: 0
Reputation: 948
You will have less trouble and create a better end result by simply creating a usercontrol with a textbox and button that is styled in the way you want. If you figure out a way to remove the functionality of the combobox, all you're really doing is creating unneeded complexity.
Upvotes: 3
Reputation: 479
Maybe it's better to create a custom control once and use it any time you do need this functionality. If you are working with Windows Forms maybe the easiest way is to inherit class UserControl and create your component using the visual designer writing a little code. You can also descend ComboBox class and code your own drawing logic but it seems to be requiring more work.
Upvotes: 1