# VBForums CodeBank > CodeBank - C# >  Editable ListBox

## NickThissen

Hi,

VB version here.

Here is a ListBox control that allows for in-place editing of its items. 


How to use: simply double-click the item you want to edit.  :Smilie: 


It works quite simple: when an item is double-clicked, a TextBox is displayed right over that item, with its text matching the item text. Once the edit is committed (by pressing Enter or by leaving the textbox, if enabled in the properties), the text is copied back to the listbox item.

Additionally, you can use the Delete key to delete selected items.


There are a few additional properties you may want to use:

- *CommitOnLeave* - If True, the edited text (in the TextBox) is committed (saved) when the TextBox loses focus (in other words: when you select a different item or some other control)
- *CommitOnEnter* - If True, the edited text is committed when the Enter or Return key is pressed (there is no support for multi-line items as of now, so you can't have a new-line anyway!)
- *AllowDelete* - If True, pressing Delete will remove the selected items.
- *ConfirmDelete* - If True, a confirmation MessageBox is shown before items are removed (via the Delete key)
- *ConfirmDeleteText* - The message in the 'delete confirmation' MessageBox.



If you are adding only Strings (text) to the ListBox, this is all you'll ever need. 
However, if you plan on adding some other object (let's say a custom class) to the ListBox, there is one thing you need to know. 

Since my ListBox cannot know what objects you may be adding as its Items, I cannot set the text of those items myself (unless they are simply strings). 
For that reason, there is an *ItemEdited* event which is raised when the user has edited an item that is not a string. You need to handle this event and control how you change the item's text manually.

For a simple example, let's say you have a class Person with two properties: Name and Age. You have overridden the ToString function so that it returns the Name property. This way, if you add instances of this Person class to the ListBox, the ListBox will display the Name property:

csharp Code:
public class Person
    {
        public string Name { get; set; }
        public int Age { get; set; }
         public override string ToString()
        {
            return this.Name;
        }
    }

Now, suppose you are adding some Person objects to my ListBox:

csharp Code:
for (int i = 1; i < 6; i++)
            {
                editListBox1.Items.Add(new Person() { Name = "Person " + i, Age = 3 * i });
            }

When the user edits the text of a person item, _you_ know that it should change the Name property. But my ListBox does not (and cannot) know that information, so you are in charge of changing the Name property.

To do that, you handle the ItemEdited event. It's event arguments (e) provide an *Item* and a *NewText* property. The Item property contains the Item being edited (as an Object), while the NewText property contains the string the user entered as the new text. In our case, the Item will be an instance of the Person class, so we can cast it to a Person, and then set the Name property:

csharp Code:
private void editListBox1_ItemEdited(object sender, EditListBox.EditEventArgs e)
        {
            // Get the person being edited (by casting e.Item to a Person object)
            Person p = (Person)e.Item;
             // And then change his/her name
            p.Name = e.NewText;
        }
Now, when the user edits an item, this code will make sure that the Name property is changed.


Note again that this is NOT required if you are using only strings in your ListBox. Also note that an exception will occur if you do not handle this event and you are using something else than strings.


I might add support for multi-line items at a later stage (using JuggaloBrotha's multi-line listbox), or the ability to move items around by dragging them (again using JuggaloBrotha's control).

As of yet, there is no support for databinding, and I doubt I will add it. I think that would get complicated quickly, but if you feel up to the task, don't hesitate to try!



Enjoy!

----------

