Sorry... I misled you. You can't do anything about the mouse clicks...
you have to change the DropDownStyle of the combo box. Here is the code
for my read-only combo box:
/// <summary>
/// A combo box that offers a <c>ReadOnly</c> property that
/// will display the currently selected item clearly but not
/// allow the user to change it.
/// </summary>
public class ComboBoxWithReadOnly : System.Windows.Forms.ComboBox
{
#region Private members
private bool ro;
private Color roBackColor;
private Color roForeColor;
private Color backColorSave;
private Color foreColorSave;
private ComboBoxStyle dropDownStyleSave;
private ContextMenu contextMenuSave;
#endregion
#region Constructor
/// <summary>
/// Creates a new combo box.
/// </summary>
public ComboBoxWithReadOnly()
{
this.ro = false;
this.roBackColor = SystemColors.Control;
this.roForeColor = SystemColors.WindowText;
}
#endregion
#region Public properties
/// <summary>
/// Sets the combo box so that it still has the standard
/// (enabled) appearance, but it cannot be manipulated by the
/// user.
/// </summary>
/// <value><c>true</c> if the user should not be allowed to
/// change the currently selected item in the combo box, <c>false</c>
if the user should
/// be able to change the combo box contents.</value>
[Description("Controls whether the user can change the combo box
state"), Category("Behavior"), DefaultValue(false)]
public bool ReadOnly
{
get { return this.ro; }
set
{
if (this.ro != value)
{
this.ro = value;
if (this.ReadOnly)
{
this.backColorSave = base.BackColor;
this.foreColorSave = base.ForeColor;
this.dropDownStyleSave = base.DropDownStyle;
this.contextMenuSave = base.ContextMenu;
base.BackColor = this.roBackColor;
base.ForeColor = this.roForeColor;
base.DropDownStyle = ComboBoxStyle.Simple;
base.ContextMenu = new ContextMenu();
}
else
{
base.BackColor = this.backColorSave;
base.ForeColor = this.foreColorSave;
base.DropDownStyle = this.dropDownStyleSave;
base.ContextMenu = this.contextMenuSave;
}
}
}
}
/// <summary>
/// Gets or sets a value specifying the style of the combo box.
/// This property replaces the <see cref="ComboBox.DropDownStyle"/>
property.
/// </summary>
new public ComboBoxStyle DropDownStyle
{
get { return base.DropDownStyle; }
set
{
if (this.ReadOnly)
{
this.dropDownStyleSave = value;
}
else
{
base.DropDownStyle = value;
}
}
}
/// <summary>
/// Gets or sets the shortcut menu associated with the control.
/// This property overrides the <see cref="Control.ContextMenu"/>
property.
/// </summary>
public override ContextMenu ContextMenu
{
get { return base.ContextMenu; }
set
{
if (this.ReadOnly)
{
this.contextMenuSave = value;
}
else
{
base.ContextMenu = value;
}
}
}
/// <summary>
/// Gets or sets the background color for the combo box.
/// This property overrides the <see cref="Control.BackColor"/>
property.
/// </summary>
public override Color BackColor
{
get { return base.BackColor; }
set
{
if (this.ReadOnly)
{
this.backColorSave = value;
}
else
{
base.BackColor = value;
}
}
}
/// <summary>
/// Gets or sets the foreground color for the combo box.
/// This property overrides the <see cref="Control.ForeColor"/>
property.
/// </summary>
public override Color ForeColor
{
get { return base.ForeColor; }
set
{
if (this.ReadOnly)
{
this.foreColorSave = value;
}
else
{
base.ForeColor = value;
}
}
}
#endregion
#region Methods for implementing read-only
/// <summary>
/// This method throws away keypresses in "read-only" mode,
preventing
/// the user from interacting with the control.
/// </summary>
/// <param name="e">Further information about the key down
event.</param>
protected override void OnKeyUp(KeyEventArgs e)
{
if (this.ro)
{
e.Handled = true;
}
else
{
base.OnKeyUp (e);
}
}
/// <summary>
/// This method throws away keypresses in "read-only" mode,
preventing
/// the user from interacting with the control.
/// </summary>
/// <param name="e">Further information about the key down
event.</param>
protected override void OnKeyDown(KeyEventArgs e)
{
if (this.ReadOnly)
{
e.Handled = true;
}
else
{
base.OnKeyDown (e);
}
}
/// <summary>
/// This method throws away keypresses in "read-only" mode,
preventing
/// the user from interacting with the control.
/// </summary>
/// <param name="e">Further information about the key press
event.</param>
protected override void OnKeyPress(KeyPressEventArgs e)
{
if (this.ReadOnly)
{
e.Handled = true;
}
else
{
base.OnKeyPress (e);
}
}
#endregion
}
}
jean-dot-paul-at-opelwilly-dot-com - 26 Feb 2005 07:56 GMT
Bruce,
tnx for sugestions, nowit works like I want itto work. The lack of being
able to change the background color of a disabled control is solved now. I
had more than once the complain that the disabled controls where not verry
wel readable so this way I have full control of the readebillity.
tnx cuagn,
Jean Paul
> Sorry... I misled you. You can't do anything about the mouse clicks...
> you have to change the DropDownStyle of the combo box. Here is the code
[quoted text clipped - 225 lines]
> }
> }