4

I have 2 problems:

  1. "Which Button called this form?" (short version)

  2. I can not touch the button or button's form

Why do I want this?

I have many forms and need to know how the user got there. If I could get the Form (not the button) it may also solve the problem.

Long version: I need to copy some properties of source form/button to the new one without do it manually and I intend to use it later on Exception reporting to catch more info

Initially, i tried to do a "newForm.Caller = this;" on each button but there are 200+ forms and lots of buttons on each.

All forms and it's buttons are custom controls so I can do things there.

Tried things

  • I tried to do things with StackFrames and reflection at form constructor but don't work (889310)

  • I found this 10401190 for JAVA but it can't help

  • I thought I could use the OnClick override to store the last button instance in a static place in buttons/forms class then get it in the form constructor but seems to be the worst solution. (Many things open forms and the culprit would be the last button pressed)

  • The problem get worse when other things open Forms and I lost the reference (DataGridVewButton, timers, linked label, ...)


EDIT1: (oɔɯǝɹ) Another detail, forms can be called from external Plugins. So again I don't have acesses to the code to change it.


EDIT2: Example (Graham Bass,ShreyasKapur) FormA has a ButtonA that when clicked shows FormB FormA inherits FormBase Button inherits ButtonBase FormB inherits FormBase

I can NOT change FormA neither ButtonA codes, only FormBase and ButtonBase codes


Edit3: (Bradley Uffner) ShowDialog() forms have the Owner property that solves part of the problem. Thanks Bradley, I forgot about that! Unfortunately, all existing code uses the parameterless constructor. "Displays this form as a modal dialog box with no owner window" (1)

Community
  • 1
  • 1
Rafael
  • 345
  • 3
  • 16
  • This seems like a case for messaging. Are you using a framework? – Graham Bass Sep 21 '15 at 16:57
  • @graham-bass I am not. – Rafael Sep 21 '15 at 17:06
  • As I understand, in button OnClick can happend anything and also Form.Show()? And ShowDialog() too? Also form creation can be in OnClick and not in too? – Spawn Sep 21 '15 at 17:26
  • @Spawn, Exactly but I think that the button solution can be simpler – Rafael Sep 21 '15 at 17:45
  • you're looking for something like the call stack? – Beth Sep 21 '15 at 17:52
  • Have you tried listening for an event on a wrapping element? I think you may be on the right path trying to save the reference, and you can use e.OriginalSource to get that referring button/element. – Graham Bass Sep 21 '15 at 17:54
  • You said "forms and it's buttons are custom controls". You could easily add optional overloads and use them wherever you want. Could you provide us with some code of your setup? – Shreyas Kapur Sep 21 '15 at 17:57
  • @GrahamBass If I understood I would change thousands of lines of code I don't own and all external-plugin-called forms wouldn't work – Rafael Sep 21 '15 at 19:29
  • @ShreyasKapur: How can a overload in a Button click change the form being called by the original click code? – Rafael Sep 21 '15 at 19:30
  • @Beth I thought I could use the call stack to get the previous caller and then create the reference. – Rafael Sep 21 '15 at 19:34
  • 1
    If the window is a dialog, then Window.Owner should have a reference to the window that opened it. If it isn't a dialog it probably won't help. – Bradley Uffner Sep 21 '15 at 19:45

1 Answers1

2

I would think that you are trying to solve the wrong problem.

When your forms are this interconnected, you coupled them to tightly. By coupling them even more tightly by looking back to who called you, you are only making you problem worse. See also: the comefrom instruction.

I would suggest passing parameters between your forms to supply them the data they need. But keep the number of parameters to an absolute minimum, and don't try to use something like caller, that would be cheating.

oɔɯǝɹ
  • 7,219
  • 7
  • 58
  • 69
  • I do understand you but there are many code to update and I are not allowed to change it. (Second problem) – Rafael Sep 21 '15 at 17:03
  • 2
    When in a hole, i would suggest stop digging deeper (or at least not faster). But since i'm not in the hole, what do i know :-) – oɔɯǝɹ Sep 21 '15 at 17:05