-1

on WPF i have windows form 1 that open form2

on form2 i have

 public partial class form2 : Window
    {

        public int temp1;

        public form2()
        {
            InitializeComponent();

            temp1 =123 ;
            this.tempTextBox.Text = temp1.ToString();

        }

    }

on form1 I want open the form2 but edit the value of temp1 what i try to do is :

    Window newWind = new form2();
    (newWind as form2).temp1=555;
    newWind.Show();

but when form 2 open , I see on tempTextBox = 123

i want see there 555

how can i do this please?

thanks!

huysentruitw
  • 27,376
  • 9
  • 90
  • 133
jordan
  • 119
  • 2
  • 3
  • 10
  • What is the value of `temp1` though... You would have to reset the value of `tempTextBox` to `temp1` to see the change – austin wernli Jan 07 '16 at 18:42
  • You're setting the temp1 value, but I don't think you're ever setting the textbox text. You set it to 123 in your intialization of the form "this.tempTextBox.Text = temp1.ToString()" but then you set the variable, which does nothing to change the text box. I imagine if you put a breakpoint in and check temp1, it holds 555. You just need to update the textbox. – Aaron Jan 07 '16 at 18:43
  • You are setting `tempTextBox.Text` to the value, not binding it. To setup bindings properly you'll need to a) implement INotifyPropertyChanged so the property change notifications work, b) make `temp1` a property that has get/set accessors, and c) bind `tempTextBox.Text` to the `temp1` property. There are plenty of examples online of all that. Alternatively, just make the set accessor for the property update the textbox value as Aaron said. – Rachel Jan 07 '16 at 18:52

1 Answers1

1

Change it to a property, modify the textbox text in the setter.

private int _temp1;
public int temp1{
get { return _temp1; }
set { 
    _temp1= value; 
    this.tempTextBox.Text = value;
    }
}
Aaron
  • 1,313
  • 16
  • 26
  • This is the correct answer. To explain why: you are setting the Text from the the temp1 field during the constructor, then failing to re-set the Text when the temp1 field is changed later. – hoodaticus Jan 07 '16 at 20:52