0

I have something like this

public class something : Inherits NativeWindow

Private WithEvents form As Form

Public Sub New(ByVal form As Form)
    Me.form = form
End Sub

end class

The usage is this:

new something(Me)

I would like to know if it's possibly in C# or VBNET using reflection or something else to detect the calling Form instead of passing it as a parameter, something like this:

public class something : Inherits NativeWindow

Private WithEvents form As Form

Public Sub New()
    Me.form = (callingform) ' If I call this from Form1 Class then the expected result is that Form1.
End Sub

end class

So the usage should be this:

The usage is this:

new something()

This is because my class inherits a NativeWindow and I need to assign the handle to the calling form.

(I want to avoid the solution of inheriting a Form instead of a NativeWindow).

ElektroStudios
  • 19,105
  • 33
  • 200
  • 417
  • @Soner Gönül I'm asking for a C# or VBNET solution... – ElektroStudios Dec 07 '13 at 17:14
  • 3
    This is an extremely bad idea. A function/subroutine's behavior should be based solely on it's parameters and/or it's object state. It's behavior should never change because of who is calling it. – RBarryYoung Dec 07 '13 at 17:35

2 Answers2

2

You could get the active form, if the code is called from an active form. This is the case if it is called from a button click, for instance.

Me.form = Form.ActiveForm
Olivier Jacot-Descombes
  • 104,806
  • 13
  • 138
  • 188
  • Thankyou, just a little question: I instance the object at the main declarations, I mean outside all subs, without any button eventhandler or that. I'm getting the expected result but could you clarify if this ever will be safe (I mean if I ever will get the desired form this way)? – ElektroStudios Dec 07 '13 at 21:51
  • I think yes. The WinForms events never involve multithreading, i.e. they are all executed in the main thread (UI-thread). If an event is fired (like button click or form load), any running event code (or normal code) is run to the end before the new event is processed, unless you start a new thread explicitly. – Olivier Jacot-Descombes Dec 09 '13 at 14:01
1

Edit: Important: the link in the first comment below this answer suggests that stack trace result is not always reliable for telling you where you come from (amongst other info.). Please be careful to read and choose the better approach to suit your case. I have not used the method below beyond simple testing.

One way is to use the following code in the calling form.

//calling form
Form2 f2 = new Form2();

//called form
StackFrame frame = new StackFrame(1, true);
var callerFileName = frame.GetFileName();

Source is little wonders of getting caller's infomration

NoChance
  • 5,632
  • 4
  • 31
  • 45
  • See this SO question [StackTrace/StackFrame don't return expected info in a production environment](http://stackoverflow.com/questions/15368309/stacktrace-stackframe-dont-return-expected-info-in-a-production-environment) – L.B Dec 07 '13 at 20:58
  • Yes it only returns the name, not the object. The way I understood the question is that you wanted to know the caller only. – NoChance Dec 07 '13 at 21:38
  • My English is not very good, I need to know which Form has called the constructor, I mean I need to detect the Form inside the "New" sub, but I need to know the object to set it, not only the name, I'm trying to convert the filename into the form following this steps http://www.codeproject.com/Questions/269979/Convert-String-Variable-into-Form-Object without success – ElektroStudios Dec 07 '13 at 21:44
  • Your English is fine, it is me who did not get the question. Now, if form1 started a new form, say form2, do you need another copy of form1 (I guess this is not possible) or do you need the original form1 back (this is easy, close form2 or hide form2 or use @Olivier Jacot-Descombes answer below). – NoChance Dec 07 '13 at 21:49