in

code for eternity !!!

community website for .net freaks ;-)

Technology

AddItem Extension Method for BulletedList, CheckBoxList, DropDownList, ListBox, RadioButtonList

We would have to write the below code to add a new item (with value) to any of these 5 controls (BulletedList, CheckBoxList, DropDownList, ListBox, RadioButtonList)

C#:

    bulletedList.Items.Add(new ListItem("text", "value"));
    checkBoxList.Items.Add(new ListItem("text", "value"));
    dropDownList.Items.Add(new ListItem("text", "value"));
    listBox.Items.Add(new ListItem("text", "value"));
    radioButtonList.Items.Add(new ListItem("text", "value"));

VB:

    bulletedList.Items.Add(New ListItem("text", "value"))
    checkBoxList.Items.Add(New ListItem("text", "value"))
    dropDownList.Items.Add(New ListItem("text", "value"))
    listBox.Items.Add(New ListItem("text", "value"))
    radioButtonList.Items.Add(New ListItem("text", "value"))

I find it quite painful to write so much code just to add a new item (with value) to these controls and so I created a new AddItem extension method for the ListControl class (since all these 5 controls inherit from the ListControl class)

C#:

    public static void AddItem(this ListControl lc, string text, string value)
    {
        lc.Items.Add(new ListItem(text, value));
    }

VB:

    <Extension()> _
    Public Sub AddItem(ByVal lc As ListControl, ByVal text As String, ByVal value As String)
        lc.Items.Add(New ListItem(text, value))
    End Sub

Now I can simply write the above code in a much simpler and cleaner way like below:

C#:

    bulletedList.AddItem("text", "value");
    checkBoxList.AddItem("text", "value");
    dropDownList.AddItem("text", "value");
    listBox.AddItem("text", "value");
    radioButtonList.AddItem("text", "value");

VB:

    bulletedList.AddItem("text", "value")
    checkBoxList.AddItem("text", "value")
    dropDownList.AddItem("text", "value")
    listBox.AddItem("text", "value")
    radioButtonList.AddItem("text", "value")

Just 1 extension method which works for 5 controls to solve the problem :-) Cheers to object inheritance and cheers to Extension Methods ;-)

Note: You can find a list of other cool and useful Extension Methods coded by me here

Cheers,
Raj

~~~ CODING FOR ETERNITY !!! ~~~


I would really appreciate votes / kicks for this blog post if you found it useful ;-)

  kick it on DotNetKicks.com     Receive Email Updates


Comments

 

DotNetKicks.com said:

You've been kicked (a good thing) - Trackback from DotNetKicks.com

May 9, 2008 8:11 AM
 

codeitwell said:

Thats a really good post.

May 10, 2008 2:08 PM
 

matt said:

Very useful. Thanks!

May 12, 2008 5:44 AM
 

Dew Drop - May 12, 2008 | Alvin Ashcraft's Morning Dew said:

Pingback from  Dew Drop - May 12, 2008 | Alvin Ashcraft's Morning Dew

May 12, 2008 6:42 AM
 

parth.patel said:

Well, though the function is handy, writing short code is not a measurement of performance.

Sorry, but don't take my wrong.

The function will not affect the performance but if there is no reusability resides in the function then why should we wrap the function into another.

June 2, 2008 5:23 AM
 

eKnacks said:

You've been knacked. Keep up the good work.

December 10, 2008 9:50 PM
 

Francisco said:

QUE SABES VOS BOLAS TRISTE

December 29, 2008 11:14 AM
 

Justin said:

Looked like a real good solution to the issue I was having only to find it doesnt work with VS2005.

March 14, 2009 4:12 PM

Leave a Comment

(required)  
(optional)
(required)  
Add
Powered by Community Server (Non-Commercial Edition), by Telligent Systems