Click here to Skip to main content
15,885,767 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I tried DirectCast(sender, Form).Name (which should say frmA) but this returns the name of the form just loaded (frmB), not the form frmA which called this Form_Load (because of frmB.Show()). Of course, there are ways around it. I can write the name of the calling Form frmA onto a label or text box on this form frmB, but I would like to learn how to use sender and EventArgs.

What I have tried:

I tried DirectCast(sender, Form).Name (which should say frmA) but this returns the name of the form just loaded (frmB), not the form frmA which called this Form_Load.
Posted
Updated 12-Sep-21 20:36pm
v2

1 solution

Basically, you probably shouldn't - it's against the principles of OOPs.
If you have two forms: a Parent form which creates and displays a Child form, then OOPs says that the Child should be independent: it should have no idea that the Parent even exists, much less what type it is. Indeed, once the Child has been created, the Parent could even be Closed and Disposed and it should have no affect on eth application (unless it it the main application form, in which case the app closes as well)

Instead, the child should communicate back to the Parent via Properties and Events, just like Controls on your Forms do at the moment.
Have a look here:
Transferring information between two forms, Part 1: Parent to Child[^]
Transferring information between two forms, Part 2: Child to Parent[^]
Transferring information between two forms, Part 3: Child to Child[^]
They show teh various ways to do it - teh code is in C# but it';s pretty easy to follow and online converters can help if you don't understand the syntax: Code Converter C# to VB and VB to C# – Telerik[^]

But if there is a really, really good reason why you need to get information on a form's creator, it is available in the Load event hander via the sender parameter, which holds the instance of the Parent Form:
VB
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    Console.WriteLine(sender.GetType())
End Sub
Will show you the class type.

But just because you can do something, that doesn't mean you should: if you think that that will help you, you are probably just making life harder for yourself in the future - a lot harder!
 
Share this answer
 

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