0
public partial class A : UserControl
{
  private string _x;
  public string X {
    get { return _x; }
    set { 
      this._x = value;
      this.textBox1.Text = this._x; 
  }
}
public partial class B : WinForm
{
   public B() {
     //Add usercontrol A to Groupbox1
     //Set A.X = "hello world"
   }
}
public class MainForm: WinForm
{
    public void button1_Click(....) { 
       B bForm = new B();
       bForm.ShowDialog();
    }
}

At design time, I set the textbox1.Text="hello". In the Main Class, I have a button that will open a new form B and on that form B I have a group box to add this user control A and change the X property value = "hello world" but the textBox1.Text doesn't change on the UI. When I set break point after the set textbox1.Text = this._x, it shows the value did change to "hello world" but it's not reflected on the UI?

Why? And how to fix it?

Thanks a bunch.

HNGO
  • 317
  • 1
  • 5
  • 18
  • 2
    Do you want to change the text of textbox same as the X than you have to assign `this.txtBox1.Text = this._x;` – Rohit Vyas Feb 15 '13 at 06:29
  • Thanks. I just update the question. – HNGO Feb 15 '13 at 06:36
  • Do you use dataBinding? – nikita Feb 15 '13 at 06:38
  • No, I don't use databinding. Wonder why the set .Text directly doesn't work. – HNGO Feb 15 '13 at 06:40
  • @user858931 Show your ` //Show Form B` code – nikita Feb 15 '13 at 06:53
  • @nikita I just updated it. – HNGO Feb 15 '13 at 06:58
  • @user858931 Don't see your B constructor code, but keep in mind that your `X` property is serialized in `InitializeComponent` method and if you set `X` before `InitializeComponent` then it will be erased in `InitializeComponent` method. – nikita Feb 15 '13 at 07:00
  • It looks as if that value is overridden after you set it. I'd try to look for all the references to `textBox1` and put a breakpoint in every line where the Text property is changed (if there are such lines). If you don't find anything this way, I'd add a TextChanged event with a simple method with just a breakpoint in order to understand when/where the value is changed. – Francesco Baruchelli Feb 15 '13 at 07:01
  • i'll have some other tests it tomorrow. Will post update later. – HNGO Feb 15 '13 at 07:22
  • Have a look at this & it's a perfect solution:http://stackoverflow.com/questions/2881409/text-property-in-a-usercontrol-in-c-sharp – linguini Feb 15 '13 at 07:25

1 Answers1

3

My guess is (since I don't see all code and it's all like guessing game 8)) - there is InitializeComponent method in classB. Since X property doesn't have DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden) attribute it is serialized in InitializeComponent method with empty string - thus erasing previously explicitly set value.

nikita
  • 2,535
  • 2
  • 16
  • 23