I do not want the user to be able to change the value displayed in the combobox. I have been using Enabled = false
but it grays out the text, so it is not very
Not sure if this is what you're looking for but...
Set the DropDownStyle = DropDownList
Then on the SelectedIndexChanged event
If (ComboBox1.SelectedIndex <> 0)
{
ComboBox1.SelectedIndex = 0
}
This ugly part is that they will "feel" like they can change it. They might think this is an error unless you give them an alert telling them why they can't change the value.
Here is the Best solution for the ReadOnly Combo.
private void combo1_KeyPress(object sender, KeyPressEventArgs e)
{
e.KeyChar = (char)Keys.None;
}
It will discard the keypress for the Combo.
Michael R's code works, but...
The DropDownHeight = 1;
must be back to the default value when ReadOnly property is set to false. So, insert before base.OnDropDown(e)
: DropDownHeight = 106;
using System;
using System.Threading;
using System.Windows.Forms;
namespace Test_Application
{
class ReadOnlyComboBox : ComboBox
{
private bool _readOnly;
private bool isLoading;
private bool indexChangedFlag;
private int lastIndex = -1;
private string lastText = "";
public ReadOnlyComboBox()
{
}
public bool ReadOnly
{
get { return _readOnly; }
set { _readOnly = value; }
}
protected override void OnDropDown (EventArgs e)
{
if (_readOnly)
{
DropDownHeight = 1;
var t = new Thread(CloseDropDown);
t.Start();
return;
}
DropDownHeight = 106; //Insert this line.
base.OnDropDown(e);
}
private delegate void CloseDropDownDelegate();
private void WaitForDropDown()
{
if (InvokeRequired)
{
var d = new CloseDropDownDelegate (WaitForDropDown);
Invoke(d);
}
else
{
DroppedDown = false;
}
}
private void CloseDropDown()
{
WaitForDropDown();
}
protected override void OnMouseWheel (MouseEventArgs e)
{
if (!_readOnly)
base.OnMouseWheel(e);
}
protected override void OnKeyDown (KeyEventArgs e)
{
if (_readOnly)
{
switch (e.KeyCode)
{
case Keys.Back:
case Keys.Delete:
case Keys.Up:
case Keys.Down:
e.SuppressKeyPress = true;
return;
}
}
base.OnKeyDown(e);
}
protected override void OnKeyPress (KeyPressEventArgs e)
{
if (_readOnly)
{
e.Handled = true;
return;
}
base.OnKeyPress(e);
}
}
}
To complete this answer:
File -> New -> Project... Visual C# -> Windows -> Classic Desktop -> Windows Forms Control Library
type the Name of your control - OK and paste this code.
You can choose the name of your dll file:
Project - yourproject Properties...
The article ComboBox-with-read-only-behavior suggests an interesting solution:
Create both a readonly textbox and a combobox in the same place. When you want readonly mode, display the textbox, when you want it to be editable, display the combobox.
I dont know if that is what you are looking but this prevents the user from chosing any item from the drop down and still be able to type text in the combobox. If you dont want the user to type text in the combobox you can make it Dropdown list from the properties menu.
So you get Read Only combobox.
Make the selected Index -1 "comboBox.SelectedIndex = -1";
private void MyComboBox_comboBox_SelectedIndexChanged(object sender, EventArgs e)
{
MyComboBox_comboBox.SelectedIndex = -1;
}
This is how you would address the fact that a ComboBox
with Enabled = False
is hard to read:
A combobox that looks decent when it is disabled