Click here to Skip to main content
15,892,059 members
Articles / Programming Languages / C#
Article

NullableComboBox

Rate me:
Please Sign up or sign in to vote.
4.00/5 (6 votes)
17 Jul 2005CPOL1 min read 49.4K   426.9K   27   4
A combobox which can show null for business objects.

Introduction

This is a combo box which can bind to business objects and can show null values (empty).

Background

The problem with the combo box is that after you bind data to it, it is not possible to show an empty Text property. It is also not possible to delete an already selected value. This combo box can do both. It is designed that you bind business objects to it, but I think (never tried it) it should also work with .NET DataTables.

Using the code

I call the little trick I do "lazy binding". With this I mean, that I don't bind the data in the first place when you set the datasource. I wait until the user actually tries to drop down the combo box. So you have the effect that the user sees an empty combo box in the beginning. The deleting of already selected items works nearly similar. In that case (if the user press DEL or BACK button), I remove the databinding of the combo box (but still remember the datasource). The code itself is really simple:

The only interesting method is the one that does the data binding:

C#
public void SetDataBinding(IList dataSource, object objectToModify, 
                                      string propertyName, string displayMember)
{
    // init combo box and delete all databinding stuff
    this.DisplayMember = String.Empty;
    this.Items.Clear();
    this.ValueMember = String.Empty;
    this.Text = String.Empty;

    // init private fields
    this.mDataSource = dataSource;
    this.mObjectToModify = objectToModify;
    this.mPropertyName = propertyName;
    this.mProperty = 
      this.mObjectToModify.GetType().GetProperty(this.mPropertyName);
    this.mDisplayMember = displayMember;
    this.mNullableMode = true;
    
    // get selected item
    object selectedItem = 
      this.mProperty.GetValue(this.mObjectToModify, null);

    // if not null, bind to it
    if (selectedItem != null)
    {
        this.DataSource = this.mDataSource;
        this.SelectedItem = selectedItem;
    }
    // do nothing and set datasource to null
    else
        this.DataSource = null;
}

The drop down event:

C#
protected override void OnDropDown(EventArgs e)
{
    // if no datasource is set, set it
    if (this.mNullableMode && this.mDataSource 
           != null && this.DataSource == null)
        this.DataSource = this.mDataSource;

    base.OnDropDown(e);
}

And the key down event:

C#
protected override void OnKeyDown(KeyEventArgs e)
{
    // if DEL or BACK is pressed set property to null and data source to null
    if (this.mNullableMode && (e.KeyCode == 
             Keys.Delete || e.KeyCode == Keys.Back))
    {
        // next line is very important:
        // without you may get an OutOfRangeException
        this.DroppedDown = false;
        this.DataSource = null;
    }

    base.OnKeyDown(e);
}

License

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


Written By
Software Developer (Senior)
Austria Austria
Born and living in Vienna, Austria. Started with Pascal in 1993 and MS-DOS 5.0. After that a little C++ in OS/2 and loads of VBA with Access in Windows 95,98, NT. To get more professionel I started C# in 2002 and did some MCP exams on that. After working for my own company I got hired by different companies. Currently I'm employed at the Federal Chambers of Commerce as a Senior Software Engineer.

Comments and Discussions

 
GeneralDisplayMember does not work Pin
davidlolo20-Apr-10 23:21
davidlolo20-Apr-10 23:21 
GeneralRe: DisplayMember does not work Pin
Rainer Halanek20-Apr-10 23:27
Rainer Halanek20-Apr-10 23:27 
You need to set the DisplayMember to the name of the property it should show. Alternativly you can override the ToString method of your class.
____________________________________________________________________
Never underestimate the power of stupid people in large groups.

GeneralBug fix Pin
Ed.Poore15-Nov-06 23:58
Ed.Poore15-Nov-06 23:58 
GeneralGreat! Pin
csg12-Nov-06 9:46
csg12-Nov-06 9:46 

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.