-1

Im trying to change the text in Form1 when pushing the button on Form2

Form 2:

public partial class Form2 : Form
{
    public Form2()
    {
        InitializeComponent();
    }
    private void button1_Click(object sender, EventArgs e)
    {
        Form1 f1 = new Form1();
        f1.textCh = "Text has been changed";
    }
}

Form 1:

public partial class Form1 : Form
{

    public string textCh { 
        get
        {
            return this.textCh;
        }
        set
        {
            this.label1.Text = value;
        } 
    }

    public Form1()
    {
        InitializeComponent();
    }

    private void button1_Click(object sender, EventArgs e)
    {
        Form2 f2 = new Form2();
        f2.ShowDialog();
    }
}

When I'm pushing button nothing happens, the text remain the same.

1

Stefano Cavion
  • 641
  • 8
  • 16
Umar
  • 1
  • 4
  • Add f1.Show() so you can see the changed text. – Hans Passant Dec 11 '20 at 10:51
  • Thank you, didn't know about that, but is there a way to update text in Form1 without opening a new window? – Umar Dec 11 '20 at 10:54
  • Does this answer your question? [How to access a form control for another form?](https://stackoverflow.com/questions/4822980/how-to-access-a-form-control-for-another-form) –  Dec 11 '20 at 11:02
  • Also this duplicate: [Interaction between forms — How to change a control of a form from another form?](https://stackoverflow.com/questions/38768737/interaction-between-forms-how-to-change-a-control-of-a-form-from-another-form) –  Dec 11 '20 at 11:03

3 Answers3

1

Another method of passing Form1 to Form2 via the Show() method and the .Owner property:

// In Form1
Form2 f2 = new Form2();
f2.ShowDialog(this); // <-- pass Form1 via "this"

Then, in Form2, you CAST .Owner to type Form1:

// In Form2
Form1 f1 = this.Owner as Form1;
if (f1!=null && !f1.IsDisposed) 
{
    f1.textCh = "Text has been changed";
}
Idle_Mind
  • 38,363
  • 3
  • 29
  • 40
0

There are several way to do this here 2 examples

  1. This example use references whitout any use of events

    public partial class Form1 : Form
    {
     public Form1()
     {
         InitializeComponent();
     }
    
     private void button1_Click(object sender, EventArgs e)
     {
         var frm = new Form2(this);
         frm.Show();
     }
    
    }
    

here the Form2 is created passing the Form1 as parameter

 public partial class Form2 : Form
{
    Form1 _parent;
    public Form2(Form1 parent)
    {
        _parent = parent;
        InitializeComponent();
    }

    private void button1_Click(object sender, EventArgs e)
    {
        var lbl = (Label)_parent.Controls.Find("label1", false).First();
        lbl.Text = "new text";
        _parent.Update();
    }
}

Than the Form2 use that for set the value wanted.

  1. This example use events

     public partial class Form1 : Form
     {
         public Form1()
         {
             InitializeComponent();
         }
    
         private void button1_Click(object sender, EventArgs e)
         {
             var frm = new Form2();
             frm.UpdateLabelEvent += Frm_UpdateLabelEvent;
             frm.Show();
         }
    
         private void Frm_UpdateLabelEvent(string str)
         {
             label1.Text = str;
         }
     }
    

and here the code of Form2

 public partial class Form2 : Form
{

    public event Action<string> UpdateLabelEvent;
    
    public Form2()
    {
        InitializeComponent();
    }

    private void button1_Click(object sender, EventArgs e)
    {
        UpdateLabelEvent("new string value");
    }
}
Stefano Cavion
  • 641
  • 8
  • 16
0

Nothing changes in your Form1 because you are creating a new Form1 first and change the text there. The new Form1 is never shown, so you see no changes.

Solution

 private void button1_Click(object sender, EventArgs e)
{
    //Form1 f1 = new Form1(); GET rid of this line
    f1.textCh = "Text has been changed";
}

you need to make sure off course that f1 is known in form2, if you dont know how here is a simple way to do that

private void button1_Click(object sender, EventArgs e)
{
    Form2 f2 = new Form2();
    f2.f1 = this;
    f2.ShowDialog();
}

public Form2()
{
    public Form1 f1 { get; set; }
    ...
GuidoG
  • 11,359
  • 6
  • 44
  • 79