problem with delete-key

Hello2you

New member
Joined
Nov 15, 2008
Messages
4
Location
Belgium
Programming Experience
Beginner
pict.jpg


I have a tabcontrol and on the second page the details of products are visible. I putted a toolstrip on it. When I click the delete-button on the toolstrip the item will be deleted. But now I want allow the user to press the Delete-key on the keyboard. Only the administrator can delete products. When another user will delete products, he gets a message. I call the performClick event of the button, but nothing happens. The KeyPreview of the form is set on true.

This is my code. I putted it in the KeyDown-event of the tabcontrol. I also tryed to put it in the KeyDown-event of the form. But that doesn't change things.

VB.NET:
Private Sub tbcArtikels_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles tbcArtikels.KeyUp
        If Asc(e.KeyCode) = Keys.Delete Then
            If Login.gebr = 1 Then
                tsbVerwijderen.PerformClick()
            Else
                MessageBox.Show("Only the administrator is allowed to delete products.", "Message", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
            End If
            e.Handled = True
        End If
    End Sub
 
First up, why do you have a method named "tbcArtikels_KeyDown" handling a KeyUp event? Not illegal but kinda silly.

Secondly, given that the e.KeyCode property is type Keys, why would you call Asc() to convert it to an Integer and then compare that to a Keys value? Why would you not just compare Keys to Keys?

Finally, have you confirmed that that event is raised when you expect and that PerformClick is called?
 
Thanks for helping me. My problem is solved now.

I deleted Asc() and then it worked fine.

The reason I used this was because without that, the code e.Keycode was underlined blue. I don't know why?
 
The reason I used this was because without that, the code e.Keycode was underlined blue. I don't know why?
Um, the IDE doesn't underline errors without telling you why. All you had to do was mouse over it or look in the Errors window and you would have seen the error message that would have explained why.
 
Back
Top