?
Solved

How to call events from events in VB.NET

Posted on 2004-09-27
11
Medium Priority
?
395 Views
Last Modified: 2012-05-05
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
Comment
Question by:zorica329
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 4
  • 2
  • +1
11 Comments
 
LVL 4

Expert Comment

by:avi247
ID: 12164700
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
 

Author Comment

by:zorica329
ID: 12165682
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
 
LVL 14

Accepted Solution

by:
ptakja earned 1000 total points
ID: 12166006
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
Industry Leaders: 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!

 
LVL 4

Expert Comment

by:avi247
ID: 12166017
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
 
LVL 3

Expert Comment

by:Stimphy
ID: 12166620

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
 

Author Comment

by:zorica329
ID: 12172081
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
 
LVL 3

Expert Comment

by:Stimphy
ID: 12176619
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
 
LVL 4

Expert Comment

by:avi247
ID: 12181404
   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
 

Author Comment

by:zorica329
ID: 12181700
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
 
LVL 4

Expert Comment

by:avi247
ID: 12182653
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
 

Author Comment

by:zorica329
ID: 12195045
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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this post we will learn different types of Android Layout and some basics of an Android App.
This article will show how Aten was able to supply easy management and control for Artear's video walls and wide range display configurations of their newsroom.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
Starting up a Project
Suggested Courses

800 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question