0

I want a "baby" form from my "main form" to update itself, whenever my main form changes something, let's make this clear using examples:

My "baby" form:

  public partial class Ventoemparedes : Form
{
    private Form1 Opener { get; set; }


    public Ventoemparedes(Form1 opener)
    {
      Initializecomponent();

      //stuff i wanna do whenever Form1 textBox.Text changes
      method1();
    }

    private void method1()
    { //does stuff
    }
}

My main form:

public partial class Form1 : Form
{
  public Form1()
    {
      InitializeComponent();
    }

   private void textBox1_TextChanged(object sender, EventArgs e)
    {
        //execute method1 on Ventoemparedes somehow???
    }

    private void button1_Click(object sender, EventArgs e)
    {

        Ventoemparedes mostra = new Ventoemparedes(this);
        mostra.ShowDialog();

    }
}

On Form1 I have a button and a textBox. The button shows up the Form2 window. What I wanna do, is whenever the Form1 textBox value is changed, a method written in Form2 will get executed and thus, update my Form2 window.

It should behave like a "refresh".

Is such thing even remotely possible?

I'm not very well versed in partial-classes and forms interaction, so I apologize if this is easy stuff to do.

ng80092b
  • 621
  • 1
  • 9
  • 24

1 Answers1

2
  1. Make your 'baby' form global variable in your main form

    public partial class Form1 : Form {
    Ventoemparedes mostra; ...

  2. On 'baby' form add public method TextBoxFromMainChange and inside call method1

    public void TextBoxFromMainChange() { this.method1(); }

  3. Finally in main form on textbox change call 'baby' form method TextBoxFromMainChange

    private void textBox1_TextChanged(object sender, EventArgs e) { mostra.TextBoxFromMainChange(); }

Nick
  • 4,192
  • 1
  • 19
  • 30
  • I'm surprised to get an answer :) Thank you . Something is wrong because it gives me a unhandled error :( http://puu.sh/hmSq1/48dcd2b983.jpg "Object reference not set to an instance of an object" Thank you for answering anyways :) This happens if i change the `textBox` value without having the baby form opened. If i have it open the error is different, it would point me to `mostra.TextBoxFromMainChange();` and say `Object reference not set to an instance of an object. Use the "new" keyword to create a object instance.` – ng80092b Apr 22 '15 at 14:09
  • 1
    @ng80092b Yes, you said that you want to open the baby form on button1 click, so you will get null reference exception if you type in textbox without open the form. How to prevent this? In textbox change first check if 'mostra' variable is not null then call mostra.TextBoxFromMainChange(); 'if(mostra !=null) { mostra.TextBoxFromMainChange(); }' – Nick Apr 22 '15 at 14:17
  • I'm getting a similar error even if the baby form is opened, http://puu.sh/hmTfK/6d7dbdd91e.png . Also, when I have the `if(mostra != null) condition, it won't update anything at all, regardless if it has the baby window open or not – ng80092b Apr 22 '15 at 14:26
  • @ng80092b put a breakpoint on mostra.TextBoxFromMainChange() and inspect the mostra object properties – Nick Apr 22 '15 at 14:35
  • I aprecciate your effort and I'm sure this is close to the solution, but there's something wrong with this code, I changed my method1 to simply show a `MessageBox` saying hello, and it works on the Form2, fails when i change the textBox of Form1 – ng80092b Apr 22 '15 at 15:07
  • It's solved @Nikola Mitev, the problem is that i was creating a new instance when pressing the button1. What i had to do was set the `mostra` that was previously define above Form1, as `mostra = new Ventoemparedes(this); ` – ng80092b Apr 22 '15 at 15:49