Click here to Skip to main content
Click here to Skip to main content

Extending basic WinForms Controls and the DataGrid

, 26 Jul 2005
Rate this:
Please Sign up or sign in to vote.
.NET 1.1 - AutoComplete ComboBox, bindable CheckBox and DateTimePicker, ReadOnly simulator, extending the DataGrid for designer support of DataGridComboBoxColumn and more...

Figure 1 - Noogen.WinForms.dll added to the Toolbox.

Introduction

This article is for those who are still working in .NET 1.1 and are in need of:

  • An auto-complete ComboBox.
  • A working DataGridComboBoxColumn, not the one that is inherited from the existing buggy DataGridTextBoxColumn.
  • A DataGridCheckBoxColumn that supports single click.
  • Bindable CheckBox.
  • Bindable DateTimePicker.
  • To be able to make any control ReadOnly like the existing property on a TextBox without having to inherit every single existing control (Decorator pattern).
  • Designer support for the newly created DataGridColumnStyles.

First of all, I understand that .NET 2.0 will most likely make 90% of these controls obsolete. I was motivated to code this because my company is in immediate need of these controls in .NET 1.1, they won't run production materials on .NET 2.0 beta, and would rather wait for the final release of .NET 2.0 than purchase third party products.

Features

  • NCheckBox- is a bindable CheckBox.
  • NComboBox
    • AutoComplete - enable hint during input.
    • ShowDropDownDuringInput - force DropDown to display during input.
    • ClearSelection - Clear text selection when the control is not in focus.
    • DisableEntryNotInList - disable entry if the value is not in list. (Including disable of paste - by ContextMenu and Ctrl+V.)
    • CharacterCasing - provide the missing CharacterCasing property as seen in existing TextBox control.
    • Bug fix - allow reset SelectionIndex to -1 when the Text is empty.
  • NDateTimePicker - provide a BindableValue property and allow null to bind as MinDate.
  • ReadOnlyGroup
    • Simulate ReadOnly property on any existing control. This is because the Enabled property makes it hard to read with gray text.
    • Allow Color to indicate control in ReadOnly state.
  • NDataGrid
    • provide rich designer support for the newly created DataGridColumnStyles columns.
    • Allow for double-click with a key combination (default Keys.Control).
    • DataGridTextBoxColumnEx - Extended the existing DataGridTextBoxColumn for bug fixing and custom Color formatting.
    • DataGridComboBoxColumn - Add ComboBox and auto-complete support by implementing NComboBox. It was also re-written from scratch by deriving directly from DataGridColumnStyles.
    • DataGridCheckBoxColumn - Allow for single click to toggle CheckState.

Using the code

You can use these controls like any other control. Download the source, compile, create a Windows application project; browse and add the component to your Toolbox. Figure 1 is the result, after the assembly is added to the Toolbox. I suggest adding it to an empty category such as 'General' or 'My User Controls' so that the controls don't get mixed up with the existing framework controls.

Figure 2 below shows how Noogen.WinForms components would look on a form. You can recognize the ReadOnlyGroup control by the icon which looks like a square-faced guy wearing eye-glasses. ReadOnlyGroup implements IExtenderProvider to provide IsInGroup property (Figure 3). ReadOnlyGroup instance contains ReadOnly property to allow user to dynamically change ReadOnly states.

Figure 2 - Noogen.WinForms components on a form in design mode:

Notice in Figure 3 that Noogen.WinForms.NCheckBox allows you to set TrueValue, FalseValue, and NullValue. In this case, it is default to "Y", "N", and DBNull.Value. Other common values are "0", "1", "True", "False" etc... User can set AllowNull to false for NCheckBox to substitute FalseValue for null.

Figure 3 - Noogen.WinForms.NCheckBox properties:

The attached demo starts by generating states, its abbreviations, and some test data (see DemoForm.GenerateData() method). Strong-typed data binding is made possible through automatic DataSet generation with the XSD designer.

Figure 4 - Strong-typed definition of DataSet used in the demo:

Noogen.WinForms.DataGridComboBoxColumn exposes both DisplayMember and ValueMember as seen in Figure 5. I've also set MappingName to "state_code" with CityStates table.

Figure 5 - What's new with the DataGridColumnStyle collection editor?

The demo in action

Figure 6 - AutoComplete ComboBox demo.

Figure 6 demonstrates the Noogen.WinForms.NComboBox auto-complete function. Notice that the button has been clicked, which causes a change from "- Disable" to "+ Enable". The button allows you to toggle the ReadOnly state of the Details group. ReadOnly BackColor of ReadOnlyGroup can also be configured (gray as seen in Figure 6).

/// <summary>
/// ReadOnly toggle button handler.
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnEnableToggle_Click(object sender, 
                               System.EventArgs e)
{
  if (this.readOnlyGroup1.ReadOnly)
  {
    this.btnEnableToggle.Text = "- Disable";
    this.readOnlyGroup1.ReadOnly = false;
  }
  else
  {
    this.btnEnableToggle.Text = "+ Enable";
    this.readOnlyGroup1.ReadOnly = true;
  }
}

Figure 7 - ContextMenu on NDateTimePicker

Figure 7 shows how you can quickly set the value of Noogen.WinForms.NDateTimePicker control with the help of ContextMenu. NDateTimePicker exposes BindableValue property to return DBNull.Value if current value is a MinDate.

Figure 8 - Single click DataGridCheckBoxColumn and CellPainting event.

Notice that the CheckBox on the DataGrid is checked, but it is not so in the Details group. The DataGrid cell is active and so it's in edit mode. The data wouldn't change until the cell looses focus. Also notice the yellow background of HOUSTON. This is possible with the CellPainting event explained in the code below. I have also added CellEditing and CellCommitting events allowing you to dynamically cancel/accept edit or commit. All three DataGridComboBoxColumn, DataGridTextBoxColumnEx, and DataGridCheckBoxColumn contain all the events just mentioned.

this.dataGridTextBoxColumnEx1.CellPainting += 
   new Noogen.WinForms.DataGridCellPaintEventHandler(
                   dataGridTextBoxColumnEx1_CellPainting);
private void dataGridTextBoxColumnEx1_CellPainting(
  object sender, Noogen.WinForms.DataGridCellPaintEventArgs e)
{
  if (e.CellValue.Equals("HOUSTON"))
    e.BackBrush = new SolidBrush(Color.Yellow);
}

You've got to try it to really get the feel of it. If you haven't downloaded it yet, go check out the demo. Also take a look at its source to see how little code you are required to write. Almost everything is designable.

Points of interest

ReadOnlyGroup was made possible with the use of [DllImport] SetWindowLong and WS_DISABLED. This may not be the best way in .NET, but it's the easiest. Another way is to use NativeWindow to subclass WndProc and filter out all Keyboard and mouse input there. If that doesn't work, there is always the [DllImport] of SetWindowHook.

Known problem(s)

  • Sometimes, when the UI is slow, you must keep the mouse in the area of DataGridCheckBoxColumn for about half a second after the first click in-order for the cell to recognize the click. Inspect the DataGridCheckBoxColumn.Edit and IsCursorInside(bounds) and you will see what I mean.

History

  • 26th July - Version 1.0.0 uploaded.
  • 28th July - Article updated.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

Share

About the Author

Noogen
Web Developer
United States United States
If at first you don't succeed, refract and refract again...

Comments and Discussions

 
GeneralProblem with booleans PinmemberMember 373376212-Jan-10 14:18 
Questionbindable check box - why row is changing ? Pinmemberparfilko30-Dec-09 11:13 
GeneralMultiple Combo's in Grid PinmemberMember 295308310-Jun-09 6:18 
QuestionWon't Maintain a Selection in ComboBox Pinmembernweiher8-Nov-07 8:12 
GeneralRe: Won't Maintain a Selection in ComboBox PinmemberMember 409621311-Dec-08 4:31 
GeneralSelecting multiple lines with ctrl key has issues Pinmembermrstu18-May-07 20:52 
Hi,

I am using the datagrid, and row select works fine for a single line and when holding down shift and selecting multiple lines, but if I use the ctrl key to multi select it has very odd behavour, is this by design?
GeneralRe: Selecting multiple lines with ctrl key has issues Pinmembermrstu18-May-07 21:00 
GeneralSelectionChangeCommitted event not raised PinmemberZoltan Balazs16-Mar-07 14:52 
GeneralRe: handling event on combobox value selected in a datagrid Pinmemberaks_16102-Mar-06 19:29 
GeneralImprovement for overlapping combobox on datagrid cell Pinmemberxebbmw12-Oct-05 6:53 
GeneralErrors on designer Pinmemberxebbmw12-Oct-05 5:08 
GeneralIsCursorInside PinmemberAndrea Zanetti12-Oct-05 4:10 
GeneralSome notes on GDI objects leakeage PinsussRiccardo Munisso12-Oct-05 4:03 
QuestionExcellent Solution by adding designer support for grid Pinmembert d8-Oct-05 13:28 
GeneralThank you so much for this control Pinmemberconvergence24-Sep-05 7:07 
GeneralKeyboard Pinsusszanettia15-Sep-05 0:09 
GeneralInfinite Loop PinmemberNregaya9-Aug-05 18:38 
GeneralRe: Infinite Loop PinmemberNoogen22-Aug-05 7:07 
GeneralAutoComplete Problem PinmemberVeeran Kutty5-Aug-05 13:16 
GeneralRe: AutoComplete Problem PinmemberNoogen5-Aug-05 15:53 
GeneralRe: AutoComplete Problem PinmemberVeeran Kutty6-Aug-05 10:18 
GeneralRe: AutoComplete Problem PinmemberNoogen7-Aug-05 3:39 
GeneralArticle problem Pinmemberdarisole26-Jul-05 21:57 
GeneralRe: Article problem PinmemberNoogen27-Jul-05 3:43 

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

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

| Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.150414.1 | Last Updated 27 Jul 2005
Article Copyright 2005 by Noogen
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid