• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 403
  • Last Modified:

How to call events from events in VB.NET

Hello,

Users of application I am coding would prefer a button DELETE on the form instead of just pressing key delete.  I am working with data grids and every time I select a row and press KEY DELETE row is deleted.  How can I call event that fires when I press KEY DELETE from btnDelete_Click(....)In other words how can I call events from other events.

Thanks,

Zorica329
0
zorica329
Asked:
zorica329
  • 4
  • 4
  • 2
  • +1
1 Solution
 
avi247Commented:
In the datagrid events, find out which event is fired when Delete  is pressed. (its one fo these two.. i dont remember off hand). If its KeyDown, note down the KeyCode and Shift values, if Keypress, note down KeyAsii value

Dim intKeyCode as Integer
Dim intShift as  Integer
Dim intKeyAscii as Integer

Private Sub DataGrid_KeyDown(KeyCode As Integer, Shift As Integer)
   Debug.print KeyCode & "-" & Shift
    intKeyCode = KeyCode
     intShift = Shift
End Sub

Private Sub DataGrid_KeyPress(KeyAscii As Integer)
 Debug.print KeyAscii
 intKeyAsc = KeyAscii
End Sub

Now in btnDelete_Click, call the above identified event

Private Sub btnDelete_Click
 Call DataGrid_KeyDown( intKeyCode, intShift )
OR
 Call DataGrid_KeyPress(intKeyAcii)
End Sub
0
 
zorica329Author Commented:
Ok

I have tried to do this, but it does not work.  I am not sure if you correctly understood my question.  I am not pressing a delete key from the keyboard, but I am clicking on delete button on my form.  So when I click delete button, I want that exactly same thing happens as I pressed delete key on the keyboard.  So i want to call keypress/keydown (whatever) event from btndelete_click.

Thanks,

zorica
0
 
ptakjaCommented:
Calling event handlers from other event handler is generally considered poor programming practice. A better way would be to create a subroutine or function that performs your function. Then in your various event handlers you just call the subroutine.

The end result is the same, you have ONE routine that handles the delete function. But this is better programming practice that what you are considering.

For what it's worth...

Jeff
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
avi247Commented:
Yes. thats what I explained above. Let me elaborate.

1. Add DataGrid_KeyDown event to your form
2.  Add this code
Private Sub DataGrid_KeyDown(KeyCode As Integer, Shift As Integer)
   Msgbox "Keycode:" & KeyCode & "  Shift:" & Shift
End Sub
3. Poputlate your Datagrid, and hit the "Delete key from the KEYBOARD"
4. KeyDown event should fire  and you will get the values for KeyCode and Shift
( I think KeyCode = 46, Shift  = 0 for Delete key of keyboard.)

Now, we need to simulate this event using the "Delete button on the FORM"
1. When the Delete BUTTON  is clicked,  btnDelete_Click event is fired.
2. Add this code to the btnDelete_Click event
Private Sub btnDelete_Click
 Call DataGrid_KeyDown( 46, 0 ) ' set the values what you find by the Debug.print above.
End Sub
3. This in turn calls the KeyDown of DatsaGrid, thereby simulating a Delete Key click.

This has to work.
If KeyDown event does not get fired when "Delete key of keyboard is pressed" try using KeyPress instead.

0
 
StimphyCommented:

Private Sub DataGrid_KeyUp(KeyCode As Integer, Shift As Integer)
    If KeyCode = vbKeyDelete then Call  DoTheDeleteThing
end sub

Private Sub btnDelete_Click
     Call  DoTheDeleteThing
End Sub

Private Sub DoTheDeleteThing
     'Do Whatever Here
End Sub

Regaurds,
Stimphy
0
 
zorica329Author Commented:
The event that fires when I press a delete key on the keyboard is following

Private Sub dgTimeSheet_KeyUp(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles dgTimeSheet.KeyUp
        MsgBox(e.KeyCode) ' e.KeyCode = 46
    End Sub

KeyDown or KeyPress does not fire.

Next, this is what I want.

Private Sub btnDelete_Click(sender, e) handles btnDelete.Click
Call dgTimeSheet_KeyUp(sender, e) 'for e.KeyCode = 46 only, I don't want this event fired for any other key
End Sub

In other words, I like what a predefined function for pressing delete key does (I did not code this function, it comes with datagrid), I don't want to change that - I just want to call that function when I click delete btn.

Thanks,

Zorica
0
 
StimphyCommented:
Hello,


Private Sub dgTimeSheet_KeyUp(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles dgTimeSheet.KeyUp
        MsgBox(e.KeyCode) ' e.KeyCode = 46
End Sub

Private Sub btnDelete_Click(sender, e) handles btnDelete.Click
        Dim x As New System.Windows.Forms.KeyEventArgs(Keys.Delete)
        dgTimeSheet_KeyUp(sender,x)
End Sub

This should do what you want.

Regaurds,
Dave

0
 
avi247Commented:
   Private Sub btnDelete_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnDelete.Click
       Call dgTimeSheet_KeyUp(sender, New System.Windows.Forms.KeyEventArgs(Keys.Delete))
    End Sub
0
 
zorica329Author Commented:
Thanks to all,

None of these work.  My btnDelete_Click calls that function, however that function does not do anything, so nothing happens.  It is frustrating - this should be simple.  
Does anybody know how can I access the code behind delete key stroke.  Maybe I can just copy that to my btnDelete_Click.  If not, I will go hard way and define my own delete function.

Thanks for trying,

Zorica
0
 
avi247Commented:
One more try. I think the above did not work because the sender has to be datagrid.

  Private Sub btnDelete_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnDelete.Click
       Call dgTimeSheet_KeyDown(dgTimeSheet, New System.Windows.Forms.KeyEventArgs(Keys.Delete))
       Call dgTimeSheet_KeyUp(dgTimeSheet, New System.Windows.Forms.KeyEventArgs(Keys.Delete))
   End Sub

For me, the KeyDown event of Datagrid seems to fire. I am surprised why its not firing for you.

If it still does not work, let me know. I will try tonite with a populated grid and see what happens.
0
 
zorica329Author Commented:
Thanks for your help avi247 but it does not work.  I made my own function that does the same thing, so I will just accept ptakja solution to this problem.

Thanks,

Zorica
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

  • 4
  • 4
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now