Solved

How to call events from events in VB.NET

Posted on 2004-09-27
11
349 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
  • 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 250 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
 
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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 

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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
mergeTwo  challenge 13 72
Enhanced For loop vs ForEach Loop 4 56
Replace a tag with sed 2 42
Scripting vs. Programming languages 25 115
I know it’s not a new topic to discuss and it has lots of online contents already available over the net. But Then I thought it would be useful to this site’s visitors and can have online repository on vim most commonly used commands. This post h…
A short article about problems I had with the new location API and permissions in Marshmallow
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

757 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now