Editing a cell in a Listbox

It would be helpful in my project if people could edit the names of the people in the listbox.

I have a 16 column listbox, but I want the user to be able to edit the first (0) column, where the name is, if they click on only that cell once (I have a different action set for double clicking in a cell already), inline in the cell itself.

Here’s the code i’m using now, and it sorta works, except it doesn’t seem to move to a different name if I click it; it “sticks” on the first name if I click anywhere else:

[code] if me.ListIndex>0 then goto noed
if me.listindex=-1 then goto noed

Me.CellType(me.listindex,0)=ListBox.TypeEditable
Me.EditCell(me.listindex,0)

x=me.listindex+1
ccname(x)=Listbox1.Cell(me.listindex, 0)

Me.CellType(me.listindex,0)=ListBox.TypeNormal
Listbox1.Invalidate

noed:[/code]

Am I doing something wrong? Thanks!

If this code is in CellClick or MouseDown, it is happening before ListIndex changes, so you will always edit the previously selected row.

Also, you may have a misconception about EditCell. It does not wait for the edit to complete, it sets up the cell for editing and returns immediately. ccname(x) will always have the original value, before the edit takes place. If that’s what you intend, then disregard my comment.

It is in CellClick, yes.

Then how do I allow a user to click once on a cell (the first one in the column, and there are 20 columns) and edit the name inline, and save the result to a corresponding value, in this case ccname(x), which is the stored names in an array with indexes of 1-20?

Thanks, Tim, for any help you can provide!

Any reason you can’t set the column to editable and leave it that way? When the user clicks, it will go into edit mode automatically. You can save the value in CellLostFocus.

Please don’t use “goto”

If me.listIndex =-1 then
Return. //exit the function
End if

Should do the trick much better.

Well, how would I set the column to editable? Bear in mind, I only want the user to be able to edit the name (row 0 of the column) not anything else.

When you load the listbox, set CellType(0,0) to Editable.

Wait,

That matches your code, but not your description of the problem, or the fact that ccname is an array.

[quote=135218:@Derek DiBenedetto]I have a 16 column listbox, but I want the user to be able to edit the first (0) column, where the name is, if they click on only that cell once (I have a different action set for double clicking in a cell already), inline in the cell itself.

Here’s the code i’m using now, and it sorta works, except it doesn’t seem to move to a different name if I click it; it “sticks” on the first name if I click anywhere else:[/quote]

If you want the entire first column to be editable, you can set ColumnType(0) to CellTypeEditable in the open event.

Perfect! Got it now, thanks a lot Tim.