Hi ppl

I need to edit a listviewItem to change name so i use "beginedit()", but then i need to send the new name. The problem is, there is not way to see when edit is finished to send the new name.
So, any idea of how could i do to send the new edited name ?

Thanks

Dani AI

Generated

Short answer: was correct — for the WinForms ListView the edit session ends when the AfterLabelEdit event fires; the new text is provided on the event args (and will be null if the user cancelled). This is the natural place to validate and persist the new name for the item. (learn.microsoft.com)

A minimal, practical pattern that handles cancel/validation and persists changes (replace SaveNameForItem with the actual persistence call) looks like this:

// enable editing and hook the event (e.g. in form load)
listView1.LabelEdit = true;
listView1.AfterLabelEdit += listView1_AfterLabelEdit;

private void listView1_AfterLabelEdit(object sender, LabelEditEventArgs e)
{
    // e.Label == null means edit was cancelled
    if (e.Label == null) return;

    string newText = e.Label.Trim();
    if (string.IsNullOrEmpty(newText))
    {
        // reject the change and optionally restart editing
        e.CancelEdit = true;
        listView1.Items[e.Item].BeginEdit();
        return;
    }

    // persist/update backing store here; if save fails, cancel the edit
    try
    {
        SaveNameForItem(e.Item, newText); // implement this
        listView1.Items[e.Item].Text = newText; // keep UI consistent
    }
    catch
    {
        e.CancelEdit = true;
    }
}

Use the event arg’s CancelEdit property to reject a bad name (the control will restore the old text) or to force re-entry with BeginEdit. Handle persistence inside AfterLabelEdit so UI and model stay in sync. (learn.microsoft.com)

When starting an edit programmatically, ensure LabelEdit is enabled and the item is selected/focused before calling BeginEdit — calling BeginEdit on an unselected item (or too early) can fail; many developers set item.Selected = true and call BeginEdit via BeginInvoke if the edit must start immediately after adding the item. (learn.microsoft.com)

Note: this advice targets System.Windows.Forms ListView. The ASP.NET WebForms ListView uses templates and a different edit lifecycle (EditItemTemplate + ItemUpdating/ItemUpdated), so the same WinForms events do not apply there. (learn.microsoft.com)

Recommended Answers

All 6 Replies

The Listview control has an event called "AfterLabelEdit" This event will fire after you change the text with beginedit. it will fire this event and you can validate/update the changes then

Yeah, thanks, i didn't reallize that there was such method.

There is also a beforeLabelEdit event. Can't think of anything I would use that for. Don't forget to mark the thread as solved.

How do i mark as solved?

at the end of the this thread page, underneath my post and above the text box where you type your reply is a an area with purple and gray text, asking if you want to mark the thread as solved. just click the link that's in the middle of if.

Thanks

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.