Paste with Right Click in Listbox

I want to implement a popup menu that allows users to right click and paste in a listbox. I also want to allow this to happen under two conditions

  1. If the user has clicked in the cell and the cell has focus
  2. If the user is moving from another control, hovers over a cell, and right-clicks before the cell gets focus

I am able to accomplish item 2 with the code below.

     Dim c as new clipboard
     If c.textAvailable Then
          ListBox1.Cell(bdRow, bdCol)=trim(c.Text)
     end if
     c.Close

If the cell has focus nothing happens. Any suggestions?

What event are you trying to do this in?

[quote=240308:@Bryan Dodson]I want to implement a popup menu that allows users to right click and paste in a listbox. I also want to allow this to happen under two conditions

If the user has clicked in the cell and the cell has focus
If the user is moving from another control, hovers over a cell, and right-clicks before the cell gets focus
[/quote]

Normally, when the cell is edited, the right click menu will offer to paste. This is the default. No need to add anything.

I am doing this in the mouseDown event.

When the cell is edited I can use the default paste, but I also want to allow pasting with a right-click when the user hovers over a cell in the listbox without selecting the cell and entering edit mode.

[quote=240327:@Bryan Dodson]I am doing this in the mouseDown event.

When the cell is edited I can use the default paste, but I also want to allow pasting with a right-click when the user hovers over a cell in the listbox without selecting the cell and entering edit mode.[/quote]

You say it is working already, right ?

You cannot replace the content of the cell in code while it is edited. That is why you must use the default right click menu.

It works if the user is moving from another control, hovers over a cell, and right-clicks before the cell gets focus. It does not work if the cell is in edit mode. If I cannot replace the content of the cell while the cell is in edit mode, are there any work arounds?

  1. Can I take the cell out of edit mode?
  2. Can I call the default paste function?

I do things like this in the MouseDown event. Check for a right click with IsContextualClick and then stuff what I want in the cell I right-clicked on. I never go into edit mode for operations like this.

“I do things like this in the MouseDown event. Check for a right click with IsContextualClick and then stuff what I want in the cell I right-clicked on. I never go into edit mode for operations like this.”

I can do this if the user has not entered edit mode first. If the user has entered edit mode I cannot change the cell contents.

Can you take the cell out of edit mode first? Or address MyGrid.ActiveCell.Text directly?

I am hoping someone on this board knows how to take the cell out of edit mode first? Or address MyGrid.ActiveCell.Text directly. Either way and my problem is solved.

[quote=240340:@Bryan Dodson]I am hoping someone on this board knows how to take the cell out of edit mode first?
[/quote]

me.SetFocus should work. At that point The ActiveCell TextField (or textArea) actually has the focus. Setting it back to the listbox will get you out of edit mode/

[/quote]
Or address MyGrid.ActiveCell.Text directly.
.[/quote]

I though that would work… I think I’ve done that in CellGotFocus. Never tried it in any other event for sure.

  • Karen

This is in a CellAction Event in my code:

Me.ActiveCell.Text = “Some Text”

I am guessing if the cell is in Edit mode you would have access to it in other events like MouseDown, but can’t say for sure. Just try it.

Thank you Karen! It works like a charm.