Solved

How to call events from events in VB.NET

Posted on 2004-09-27
11
372 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
Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

 
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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
How does Google get such fast response times? 5 176
numbers ascending pyramid 101 191
Currency Conversion? 1 84
Filename to be appended with DateTimeof Download 3 55
This is an explanation of a simple data model to help parse a JSON feed
Entering a date in Microsoft Access can be tricky. A typo can cause month and day to be shuffled, entering the day only causes an error, as does entering, say, day 31 in June. This article shows how an inputmask supported by code can help the user a…
An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
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 …

785 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