Click here to Skip to main content
15,885,877 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I have learned that keydown events don't work in user controls (from experience and reading dozens of posts). So I added Ctrl + P keydown code in the Form1.vb code and it responds as expected. My problem is how do I send the Ctrl + P event to the usercontrol so that it runs one of it's many subroutines. I have tried the code below and many variations of it but there seems to be no communication between the Form1 code and the UserControl.

The Solution Explorer shows the Form1.vb and the UserControl.vb at the same level under the project. Expanding the Usercontrol in the Solution Explorer, it looks very much like the Form1.vb expansion. The point I am trying to make is the UserControl is complex and has about the same number of subroutines as the Form1.vb.

I would show a screenshot of the Solution Explorer but I don't know how to do that with CodeProject.

What I have tried:

Public Sub PKey_KeyDown(sender As Object, e As KeyEventArgs) Handles MyBase.KeyDown
        If e.KeyData = (Keys.Control Or Keys.P) Then
            Dim UC1 As New Yacht_Race.UserControl1
            UC1.myPrint()
        End If
    End Sub
Posted
Updated 22-Jul-19 20:49pm

Your code doesn't work because it creates a new instance of your UserControl and calls the method on that. Since the new instance is empty, and not displayed it can't print whatever your user is looking at.

If you call the myPrint method on the instance of the control that is showing on your form, it should work. Normally, that means using the name that you gave it when your dropped the control on your Form in the designer.

But ... I've not had any problems with Keydown events in UserControls - they work fine with any control that has the user focus (which obviously is only ones that can accept keyboard input like TextBoxes for example)
 
Share this answer
 
Comments
Member 10628309 17-Jul-19 14:07pm    
I think you are suggesting that if I remove the line of code: Dim UC1 As New Yacht_Race.UserControl1 and change the next line from UC1.myPrint() to myPrint() That it will work...I can assure you is does not work. If I am misunderstanding what you are suggesting please try again to make me understand. Maybe a couple of lines of code would help.
OriginalGriff 17-Jul-19 14:16pm    
No, read what I said: "If you call the myPrint method on the instance of the control that is showing on your form"
That means exactly what it says: you need to use the name of the control instance that appears in the Name property of in the Properties panel in teh Visual Studio Forms designer.
See if you can find it - just open the designer and click the control to highlight it.
Member 10628309 17-Jul-19 15:00pm    
The name of the control is UserControl1. That is what shows in "the Name property of the properties panel in the Visual Studio Forms designer". Remember the code of the key down is in the Form1.vb file. It somehow has to communicate the event to the UserControl1 control.
OriginalGriff 18-Jul-19 2:04am    
So what do you think you need to do with the name? Come on - this isn't complicated!
Member 10628309 18-Jul-19 22:53pm    
UserControl1.myPrint() does not work...it wont even compile. In the mean time I have read at least 50 more posts and I am convinced that what I am trying to do can't be done. I am sure you will tell me to go back to school and learn something but I wont let that upset me.
This code works perfectly. It eliminated 20 lines of Else If code and prevented editing several hundred lines of code with many UserControl names, e.g. UserControl112.

Public Sub PKey_KeyDown(sender As Object, e As KeyEventArgs) Handles MyBase.KeyDown
        If e.KeyData = (Keys.Control Or Keys.P) Then
            Dim Ctrl As Control = Me.ActiveControl
            DirectCast(Ctrl, UserControl1).myPrint()
        End If
    End Sub
 
Share this answer
 
v2
Comments
phil.o 23-Jul-19 3:00am    
Now what happens if you click on a control which does not have a myPrint() method?
Ralf Meier 23-Jul-19 6:14am    
Additional to the comment from Phil.O :
Object as a type could be everything and also accepts everything as assignment.
To do this code-snippet in the right way you should check every time if Me.ActiveControl is the right type.
Next : to call a method of an Object which doesn't belong to the Object-type WILL (not COULD) cause a lot of future problems which are not easy-detected with the Debugger.
I suggest you read carefully what OriginalGriff has written inside the other Solution ... and ... read (only a little) about Types and Type-handling ...
Member 10628309 24-Jul-19 1:33am    
Thank you for your advice. When I seek advice on this web site I do not include code that I use to prevent a problem, e.g., try catch etc. I am always trying to make something work. After I get it working then I go back and look for problems. Yes, there have been several times that I had to scrap what I was trying to do and do a different way. But in the process I learn a lot. With regard to your comment about using Object...someone else gave me a similar comment and suggested that I use Dim Ctrl As Control = Me.ActiveControl followed by DirectCast(Ctrl, UserCtrl).myPrint(). What I didn't mention in my origional question was that I have 8 Identical UserControls on 8 tabs of a 12 tab TabControl. I was trying to write code that would work on any of the 8 tabs/usercontrols numbered UserControl110, 111, 112...117.
Ralf Meier 24-Jul-19 3:16am    
I don't understand exactly what you are doing there ... but :
Normally the action comes from the Control (in this case your UserControl) and not from the hosting Form.
The best way would be : you raise and Event with you UserControl and catch it on the hosting Form. Here you could decide what should happen next.

To your problem :
If you only do some tests you could (not should) create some 'quick-and-dirty' code. But if you create something which shall work in the field you must use well tested code (methods, Controls etc.) to prevent further problems. But this is your game - not mine ...
Ralf Meier 24-Jul-19 3:20am    
Additional :
Before casting you must check (with GetType , Type) if the Control-Types are really matching. Only in this case you shall use Casting ... but in this case you don't need it to do because smae types could assigned to each other (directly) ...

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900