Results 1 to 1 of 1

Thread: Editable ListBox

  1. #1

    Thread Starter
    PowerPoster
    Join Date
    Apr 2007
    Location
    The Netherlands
    Posts
    5,070

    Editable ListBox

    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.


    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:
    1. public class Person
    2.     {
    3.         public string Name { get; set; }
    4.         public int Age { get; set; }
    5.  
    6.         public override string ToString()
    7.         {
    8.             return this.Name;
    9.         }
    10.     }

    Now, suppose you are adding some Person objects to my ListBox:
    csharp Code:
    1. for (int i = 1; i < 6; i++)
    2.             {
    3.                 editListBox1.Items.Add(new Person() { Name = "Person " + i, Age = 3 * i });
    4.             }

    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:
    1. private void editListBox1_ItemEdited(object sender, EditListBox.EditEventArgs e)
    2.         {
    3.             // Get the person being edited (by casting e.Item to a Person object)
    4.             Person p = (Person)e.Item;
    5.  
    6.             // And then change his/her name
    7.             p.Name = e.NewText;
    8.         }
    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!
    Attached Files Attached Files

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width