How to start to edit listview items programatically

B

bz

Hi,

How to start to edit listview items programatically, like in explorer
when user right click on a file the select Rename?

I know how to make items editable with a click, but I want to add a
button / right click menu for this too

Thanks
 
J

Jeff Gaines

I know how to make items editable with a click, but I want to add a
button / right click menu for this too

I use:

private void DoEditItem()
{
// Only handle if 1 item selected
JLVItemEx lvItem = GetSelectedListViewItem();

if (lvItem == null)
{
FireMessageSender(this, "Please Select One Object To Edit", true);
return;
}
m_EditingLabel = true;
lvItem.BeginEdit();
}

private JLVItemEx GetSelectedListViewItem()
{
if (this.SelectedItems.Count != 1)
return null;

return (JLVItemEx)this.SelectedItems[0];
}

The key things being to ensure you have a selected ListViewItem and call
lvItem.BeginEdit();
You need to switch JLVItemEx for ListViewItem if you are using standard
ListViewItems.
Yo need to trap AfterLabelEdit if you want to validate the new text/name.
 
B

bz

Thanks for answer.
It worked for me just by using BeginEdit (that was the method I was
looking for)

I don't understand what is the other code you wrote. What is it good
for? And what is JLVItemEx

Thanks

I know how to make items editable with a click, but I want to add a
button / right click menu for this too

I use:

private void DoEditItem()
{
        //      Only handle if 1 item selected
        JLVItemEx lvItem = GetSelectedListViewItem();

        if (lvItem == null)
        {
                FireMessageSender(this, "Please Select OneObject To Edit", true);
                return;
        }
        m_EditingLabel = true;
        lvItem.BeginEdit();

}

private JLVItemEx GetSelectedListViewItem()
{
        if (this.SelectedItems.Count != 1)
                return null;

        return (JLVItemEx)this.SelectedItems[0];

}

The key things being to ensure you have a selected ListViewItem and call
lvItem.BeginEdit();
You need to switch JLVItemEx  for ListViewItem if you are using standard
ListViewItems.
Yo need to trap AfterLabelEdit if you want to validate the new text/name.
 
J

Jeff Gaines

Thanks for answer.
It worked for me just by using BeginEdit (that was the method I was
looking for)

I don't understand what is the other code you wrote. What is it good
for? And what is JLVItemEx

JLVItemEx is one of my classes based on a ListViewItem, it has additional
properties like Path, FileType etc.
The code is split into small functions in my library and I just copied and
pasted it.
For instance GetSelectedListViewItem() is a function I use all the time so
having it in a function means I just have to call the function and know I
will either get a valid ListViewItem or null returned, it saves a lot of
repetitive code.
The same with DoEditItem() - it can be called from a menu, the F2 key or
even from outside the file it is in.
 
B

bz

JLVItemEx is one of my classes based on a ListViewItem, it has additional
properties like Path, FileType etc.

Thanks, I thought it was some NET class I wasn't aware of.

Regards
Bogdan
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top