EditClear MenuItem (Delete)

I will make a long (too long) story short:

i had troubles setting the delete key as the MenuItem.Key of EditClear. It works nicely in another project (thanks shao shen for the trick), worked fine in a brand new (vlean) project / and I never was able to make it not working in a brand new project.

I stripped down the project with troubles until I get it working fine without changing the code that deals with EditClear.

At last, minutes ago, I found (watch the code below) that ListBox.KeyDown (after checking ListBox.CellKeyDown (comment the code) is the faultive.

Note: the code in ListBox.CellKeyDown stayed commented when I started to test the code in ListBox.KeyDown.

LBN: I am no more able to make the code working once I started to comment the arrow code to try to know what If block is faultive (and none is… if I can tell that), until I found that now, even with the whole block of IFs is commented (watch below), no the Delete key does no more works.

And I know that I do not saw a black cat while I was driving my car or walking earlier (today / yesterday).

Desesperate, I am completely desesperate ! Help !

Below is the (minutes ago) offending code !

[code] If Key = Chr(28) Then // Left Key: move to top
If LB.ListIndex <> 0 Then // Goes to the previous Row #
// Go to the Previous Row
LB.ListIndex = LB.ListIndex - 1

  // Keep the focus in the ListBox
  Me.SetFocus
  
  // Key handled !
  Return False
  
Else // The Selected Row is #0
  // Loop: go directly to the last Row
  LB.ListIndex = LB.ListCount - 1
  
  // Keep the focus in the ListBox
  Me.SetFocus
  
  // Key handled !
  Return False
End If

End If

If Key = Chr(29) Then // Right Key: move to bottom
If LB.ListIndex < (LB.ListCount - 1) Then
// increase the value of the selected Row #
LB.ListIndex = LB.ListIndex + 1

  // Keep the focus in the ListBox
  Me.SetFocus
  
  // Key handled !
  Return False
  
Else // Loop: go directly to the first Row
  LB.ListIndex = 0
  
  // Keep the focus in the ListBox
  Me.SetFocus
  
  // Key handled !
  Return False
End If

End If

If Key = Chr(30) Then // Bottom Key: move to bottom
If LB.ListIndex <> 0 Then // Goes to the previous Row #
// Go to the Previous Row
LB.ListIndex = LB.ListIndex - 1

  // Keep the focus in the ListBox
  Me.SetFocus
  
  // Key handled !
  Return True
  
Else // The Selected Row is #0
  // Loop: go directly to the last Row
  LB.ListIndex = LB.ListCount - 1
  
  // Keep the focus in the ListBox
  Me.SetFocus
  
  // Key handled !
  Return True
End If

End If

If Key = Chr(31) Then // Top Key: move to bottom
If LB.ListIndex < (LB.ListCount - 1) Then
// increase the value of the selected Row #
LB.ListIndex = LB.ListIndex + 1

  // Keep the focus in the ListBox
  Me.SetFocus
  
  // Key handled !
  Return True
  
Else // Loop: go directly to the first Row
  LB.ListIndex = 0
  
  // Keep the focus in the ListBox
  Me.SetFocus
  
  // Key handled !
  Return True
End If

End If
[/code]

The delete key is meant to delete a Row from the ListBox (you may guess) and the test file loaded in the ListBox is the same, always dragged from the Finder. The only change is the Row I select (to be deleted). BUT IT CANNOT BE THAT.

You return FALSE in the top code for “key handled” and TRUE in the bottom code for “key handled”
You should also wrap your code in the [ CODE ] [/ CODE ] tags (or select your text and click the “code” button in the toolbar, the piece of paper with the blue coloured open and close tags)

set the key of MainMenuBar.EditClear to DEL (without MenuModifier)

add MenuHandler EditClear to your Window

Function EditClear() As Boolean
  dim x as Integer = lb.ListIndex
  if lb.Selected(lb.ListIndex) = true then
    lb.RemoveRow(lb.ListIndex)
    lb.Selected(x) = true
    return true
  else
    return false
  end
End Function

or add this to your code

  If Key = Chr(127) Then // remove row
    dim x as Integer = lb.ListIndex
    if lb.Selected(lb.ListIndex) = true then
      lb.RemoveRow(lb.ListIndex)
      lb.Selected(x) = true
      return true
    else
      return false
    end
  End If

Thank you for your answers.

shao:
Yes for the False and True use.

That is what I’ve done.

But the problem comes from elsewhere since when I wanted to know which of the four Arrow Keys If block was involved, the problem come back and stayed.

fro some non-related reasons (I changed the way ListBox1.KeyDown even code is done), Delete started to work all-of-a-sudden on yesterday afternoon.

It tooks me time to realize.