|
-
May 14th, 2010, 01:35 PM
#1
Set ListBox item text without knowing what kind of object it is
Hi,
I'm creating an editable ListBox control, where the user can double-click an item to edit it. When double-clicked, I simply overlay a TextBox over the item, so the user can change the text and then commit it by pressing Enter or leaving the TextBox. The 'commit' is simply this:
Code:
Private Sub CommitText()
Me.Items(editingIndex) = textBox.Text
textBox.Visible = False
End Sub
where 'editingIndex' is the index of the item being edited, and 'textBox' is the TextBox object.
If the ListBox contains strings, this works just fine. The item being edited is replaced by the textbox Text string.
However, if the ListBox contains some other object (let's say a custom class), this is a problem! It still "works", in that it still sets the text of the item, but the item is no longer an instance of that custom class! It is now a String, and all other information about the item is lost.
So, I really want to let the user be able to change the text representation of ANY object, without replacing the object with a string. Obviously, I cannot know how the object displays its text (well, by the ToString function of course, but I cannot know what the ToString function returns).
As an example, consider this class:
vb.net Code:
Public Class Person Property Name As String Property Age As Integer Property Gender As String Public Overrides Function ToString() As String Return Me.Name End Function End Class
When an instance of this is added to the ListBox, it will display the Name property. So, when the user edits this instance, I want to change the Name property of the class, instead of replacing the class instance by a string...!
Now, I am having a few ideas how to approach this, but none really work the way I want.
I am pretty sure that I'm going to have to provide some kind of event that the user has to handle. I'm thinking something like this:
- When the item edit is committed, an event is raised
- This event contains the Item being edited (as an object, the user has to cast it himself), and the new text of the item.-
- The user handles this event, and assigns the new text to the correct property.
For the Person class, this could look something like this
vb.net Code:
Private Sub EditListBox1_ItemEdited(ByVal sender As Object, ByVal e As EditEventArgs) Handles EditListBox1.ItemEdited Dim p As Person = DirectCast(e.Item, Person) p.Name = e.NewText End Sub
My ListBox should raise this event, and assign the new item like this
vb.net Code:
Dim e As New EditEventArgs() e.Item = Me.Items(editingIndex) e.NewText = textBox.Text textBox.Visible = False RaiseEvent ItemEdited(Me, e) '<-- e.Item is edited here so it displays the correct new text Me.Items(editingIndex) = e.Item
I think this should work (although I haven't tested it), but there's one problem with this approach:
When the items are just strings, the user still needs to implement this event handler, while he shouldn't have to (I can simply assign the new text string to the item). So, if the user is simply using strings, he still needs to do this for every instance of my listbox:
vb.net Code:
Private Sub EditListBox1_ItemEdited(ByVal sender As Object, ByVal e As EditEventArgs) Handles EditListBox1.ItemEdited e.Item = e.NewText End Sub
(e might have to be passed ByRef for this to work..)
I don't want to force to user to do this if he is just using strings, my listbox should do that automatically...
So what I'm really looking for is a way so that I can implement the default behavior (for String items), and still allow the user to override this behavior if he is not using strings.
I may be overlooking the obvious solution here, but I can't figure it out... Overriding is done when you inherit a class, but the user isn't going to inherit my custom listbox (well he CAN, but he shouldn't have to). He is just going to handle the event. And if he doesn't handle the event, my listbox uses the default implementation...
Any tips?
Last edited by NickThissen; May 14th, 2010 at 01:41 PM.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|