-3

How do I send data from Form1 to Form2 dynamically?

Dynamically means that for example, I have two buttons in Form1. When I click button1, a label which is on Form2 will change. And then when I click button2 in Form1, the same label will change again. I need to do it without close Form1 or Form2

For example: enter image description here

Eugene Berdnikov
  • 2,150
  • 2
  • 23
  • 30
  • 1
    Forms are objects like any other. Any code which has a reference to an object can call methods and properties on that object. How do these forms relate and what have you tried? – David Sep 16 '17 at 19:59
  • I need a controller but this controller on form 1. when this controller changed, form 1 will send an information to form2. Also, this information can change every time or every minute. – Furkan Aydoğan Sep 16 '17 at 20:02
  • 1
    @FurkanAydoğan: So... `form2Instance.SomeMethod(someValue);` ? It's not clear what you've tried or where you're stuck. Instead of trying to vaguely describe what the code should be doing, create an actual example. Calling a method on an object is very introductory C#. – David Sep 16 '17 at 20:04
  • string ab; Form1 a = new Form1(); a.button1.Text = ab ; – Furkan Aydoğan Sep 16 '17 at 20:14
  • @FurkanAydoğan: And in what way does that not work? Is `button1` a public property on `Form1`? Do you ever show `a`? – David Sep 16 '17 at 20:17
  • its working but not change again when i clicked another button – Furkan Aydoğan Sep 16 '17 at 20:20
  • @FurkanAydoğan: Then clearly you've made a mistake somewhere in your code. Code which, by the way, *we can't see*. Try to understand that we can't see your screen from here. Show an example of what you're trying *in the question*. Not just one or two lines of code and then more vague descriptions, but an actual demonstrable example. – David Sep 16 '17 at 20:24
  • 2
    You have *entirely* misunderstood how Stack Overflow works. You are invited to start here: https://stackoverflow.com/help – David Sep 16 '17 at 20:36

1 Answers1

0

Sharing Data between Forms is not trivial, but possible. Asuming you have Form1 (the receiver) and Form2 (the sender):

  1. Modify Form1 to have a public function or property to take in the data
  2. Have the Form2 instance created by Form1. Hand a reference to Form1 to Form2, using the constructor or some public property on Form1.
  3. Have Form2 call the function of Form1 to run the code you want.
Christopher
  • 9,634
  • 2
  • 17
  • 31
  • I need a controller but this controller on form 1. when this controller changed, form 1 will send an information to form2. Also, this information can change every time or every minute. FORM1(SENDER) FORM2 (RECEIVER) – Furkan Aydoğan Sep 16 '17 at 20:07