Click here to Skip to main content
15,881,172 members
Articles / Desktop Programming / Windows Forms
Article

TdhComboBox - An Inherited ComboBox With ReadOnly Property

Rate me:
Please Sign up or sign in to vote.
3.11/5 (13 votes)
12 Oct 2006CPOL6 min read 107.7K   687   23   29
This article describes a subclassed ComboBox with ReadOnly functionality.

Sample Image - TdhComboBox.gif

Introduction

The standard ComboBox really does need a .ReadOnly property. I've been using a kludge to emulate read-only behavior -- positioning both a ComboBox and a TextBox of equal sizes on my form(s) and setting one or the other as .Visible depending on the "mode" at the moment.

Recently, reading this article sparked ideas on how to achieve the goal of ComboBox with a .ReadOnly property that works the way I'd expect. The result of my effort is presented here as: TdhComboBox.

The demo example compares three standard ComboBoxes (one each with .DropDownStyle property set to 'DropDown,' 'DropDownList' and 'Simple') to three corresponding instances of TdhComboBox. In the screen-shot, the standard ComboBoxes are disabled and the TdhComboBoxes are read-only.

Using the Control

To use TdhComboBox as is, add a reference in your project to the class library 'TDHComboBox.dll.' The namespace used in this library is:

C#
using TDHControls.TDHComboBox;

The TdhComboBox control was written (and compiled) using VS2002 (.NET 1.0) with the intention that the source code be readily available to other developers regardless of the .NET version they are using.

The novel members of TdhComboBox's interface are:

  • Colors_Set() - The method sets the control's .BackColor and .ForeColor as appropriate to its current state. The developer shouldn't need to call this method; the main reason for writing it was to have a single section of code to perform this function (and, since the method exists, I decided to expose it).
  • Text_Set() - The method's signature is: [public bool Text_Set(string setValue, bool forceIfEmptyString)]. This method allows the developer to programatically select an .Item from the control's Items collection -- that is, it may trigger the control's SelectedIndexChanged Event. If the value of 'setValue' is not a member of the Items collection, the control's .Text property will nevertheless be set to the value of 'setValue' under the following conditions: 1) the control's .DropDownStyle property is not set to 'DropDownList,' or 2) the value of 'setValue.Trim()' is an empty string and the value of 'forceIfEmptyString' is 'true.' The method's return value reflects whether the value of the .Text property was successfully set.
  • ReadOnly - This boolean property determines whether the control is in read-only mode.
  • ReadOnlyAllowDrop - This boolean property determines whether the DropList may be opened when the control is in read-only mode.

  • AllowTextCut - When the control's .DropDownStyle property is set to 'DropDownList,' this boolean property allows the user to use the ContextMenu to 'Cut' or 'Delete' the value of the .Text property or 'Paste' a white-space string object to the .Text property -- even if the .Items collection doesn't contain a empty string or white-space string object.
  • ContextMenuEnabled - This boolean property determines whether the control's ContextMenu is enabled/available. Upon initialization, the TdhComboBox instance builds its own ContextMenu, or the developer may supply one by setting the standard .ContextMenu property.

It should go without saying that the "disabled" colors take precedence over any of the Color properties described below:

  • DropDownListBackColor - This Color property determines the background color to be used when the control's .DropDownStyle property is set to 'DropDownList' (and the control is not in read-only mode).
  • DropDownListForeColor - This Color property determines the foreground color to be used when the control's .DropDownStyle property is set to 'DropDownList' (and the control is not in read-only mode).

  • NormalBackColor - This Color property determines the background color to be used normally -- that is, when the control's .DropDownStyle property is not set to 'DropDownList' and the control is not in read-only mode.
  • NormalForeColor - This Color property determines the foreground color to be used normally -- that is, when the control's .DropDownStyle property is not set to 'DropDownList' and the control is not in read-only mode.

  • ReadOnlyBackColor - This Color property determines the background color to be used when the control is in read-only mode. This property takes precedence over the .DropDownListBackColor and .NormalBackColor properties.
  • ReadOnlyForeColor - This Color property determines the foreground color to be used when the control is in read-only mode. This property takes precedence over the .DropDownListForeColor and .NormalForeColor properties.

Modified (overridden) members of TdhComboBox's interface are:

  • BackColor - The .BackColor property has not been explicitly overridden; however, the novel Color properties described above take precedence and may over-write values to which this property is set.
  • ForeColor - The .ForeColor property has not been explicitly overridden; however, the novel Color properties described above take precedence and may over-write values to which this property is set.

  • DrawMode - This property's default value is now 'OwnerDrawFixed' and the 'Normal' enumeration-value is not selectable. The control doesn't contain any code to specificly deal with 'OwnerDrawVariable' (so, it's probably best to stick with 'OwnerDrawFixed').

Other than the code for these properties, TdhComboBox is intended to behave as a standard ComboBox -- though, of course, I won't guarantee that it does.

Known Issues

  • In the OnDrawItem() method, when the .DataSource property is set (as opposed to an Items Collection having been generated via the .Items.Add() method), the section of code which determines the string value of the item being drawn *assumes* the item object can be cast to a System.Data.DataRowView object.
    • 2006 October 12: Version 1.0.004 corrects this issue.
  • When (.DropDownStyle == Simple), highlighting items in the "drop down list" as the mouse cursor passes over them is yet imperfect. Specifically, when the cursor moves back into the "virtual ClientRectangle" (which my code computes), I force an .Invalidate() in case the user's last action was to use the control's ScrollBar. The .Invalidate() causes the ScrollBar to return correct values (this is good), but it also triggers a Paint Event, which may actually happen *after* my code has drawn/highlighted the item (thus causing the highlighting to vanish).
  • When the control is used in a .NET 1.1 application, the mouse cursor may be used to select text (by clicking-and-dragging) in the EditBox portion of the ComboBox. When the control is used in a .NET 1.0 application, this doesn't seem to be an option. The interesting thing, to me at any rate, is that in both cases the DLL was compiled under .NET 1.0.

Wish List

  • Improve the code in the OnDrawItem() method dealing with the .DataSource property.
    • Resolved with Version 1.0.004
  • Come up with a way to hide the "drop-arrow" when the control is in ReadOnly mode and the .ReadOnlyAllowDrop property is false.
    • Resolved with Version 1.0.004
  • Enable the ContextMenu to copy specific entries/items from the control's DropList to the Windows clipboard.
  • Devise a resolution for the problem of highlighted items in the "drop down list" when (.DropDownStyle == Simple).

History

  • 2006 August 18: Submission of TdhComboBox ver 1.0.001 to The Code Project.
  • 2006 August 25: Version 1.0.002 - Added .AllowTextCut and .ReadOnlyAllowDrop properties and Colors_Set() and Text_Set(string setValue, bool forceIfEmptyString) methods.
  • 2006 October 12: Version 1.0.004
    • In the OnDrawItem() overridden method, use the base ComboBox .GetItemText() method to get the text value of the .Item being drawn. This corrects a know issue. [Thanks to Alan Brookland for this suggestion]
    • Added logic to over-paint (hide) the "drop-arrow" when ['this.ReadOnly' and '!this.ReadOnlyAllowDrop'].

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionQuestion For Any Who Have Downloaded This Project [modified] Pin
Ilíon31-Oct-06 3:28
Ilíon31-Oct-06 3:28 
AnswerMy (Simplified) Version of TdhComboBox Pin
pbnec24-Aug-08 0:29
pbnec24-Aug-08 0:29 
GeneralRe: My (Simplified) Version of TdhComboBox Pin
Ilíon26-Aug-08 2:50
Ilíon26-Aug-08 2:50 
GeneralRe: My (Simplified) Version of TdhComboBox Pin
pbnec3-Oct-08 22:29
pbnec3-Oct-08 22:29 
GeneralRe: My (Simplified) Version of TdhComboBox Pin
Ilíon3-Oct-08 23:55
Ilíon3-Oct-08 23:55 
QuestionRe: Question For Any Who Have Downloaded This Project Pin
RAJ NAVHAL25-Jun-09 2:42
RAJ NAVHAL25-Jun-09 2:42 
Generalv1.004 Pin
Glen Harvy13-Oct-06 11:55
Glen Harvy13-Oct-06 11:55 
GeneralRe: v1.004 Pin
Ilíon14-Oct-06 12:17
Ilíon14-Oct-06 12:17 
GeneralRe: v1.004 [modified] Pin
Glen Harvy14-Oct-06 13:05
Glen Harvy14-Oct-06 13:05 
GeneralRe: v1.004 Pin
Ilíon16-Oct-06 9:36
Ilíon16-Oct-06 9:36 
GeneralRe: v1.004 Pin
Ilíon25-Oct-06 2:43
Ilíon25-Oct-06 2:43 
GeneralRe: v1.004 Pin
Glen Harvy25-Oct-06 11:36
Glen Harvy25-Oct-06 11:36 
GeneralRe: v1.004 [modified] Pin
Ilíon26-Oct-06 5:09
Ilíon26-Oct-06 5:09 
GeneralRe: v1.004 Pin
Ilíon27-Oct-06 9:47
Ilíon27-Oct-06 9:47 
GeneralRe: v1.004 Pin
Glen Harvy28-Oct-06 0:45
Glen Harvy28-Oct-06 0:45 
GeneralKnown Issue OnDrawItem() Pin
aBrookland9-Oct-06 1:01
aBrookland9-Oct-06 1:01 
GeneralRe: Known Issue OnDrawItem() [modified] Pin
Ilíon10-Oct-06 3:14
Ilíon10-Oct-06 3:14 
GeneralRe: Known Issue OnDrawItem() Pin
aBrookland10-Oct-06 9:07
aBrookland10-Oct-06 9:07 
GeneralRe: Known Issue OnDrawItem() Pin
Ilíon12-Oct-06 10:43
Ilíon12-Oct-06 10:43 
GeneralThanks Pin
Glen Harvy19-Sep-06 4:29
Glen Harvy19-Sep-06 4:29 
GeneralRe: Thanks Pin
Ilíon13-Oct-06 5:50
Ilíon13-Oct-06 5:50 
GeneralLittle error Pin
marhalu25-Aug-06 0:02
marhalu25-Aug-06 0:02 
GeneralRe: Little error Pin
Ilíon25-Aug-06 2:04
Ilíon25-Aug-06 2:04 
GeneralRe: Little error Pin
marhalu25-Aug-06 2:36
marhalu25-Aug-06 2:36 
GeneralRe: Little error Pin
Ilíon25-Aug-06 14:00
Ilíon25-Aug-06 14:00 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.