C#winform textbox.text不会改变

问题描述 投票:0回答:2
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();
    }
}

在设计时,我设置了textbox1.Text =“hello”。在Main Class中,我有一个按钮,它将打开一个新的表单B并在该表单BI上有一个组框来添加此用户控件A并更改X属性值=“hello world”但textBox1.Text没有更改UI。当我在设置textbox1.Text = this._x之后设置断点时,它显示值确实变为“hello world”但是它没有反映在UI上?

为什么?以及如何解决它?

谢谢一堆。

c# winforms
2个回答
2
投票

我的猜测是(因为我没有看到所有代码而且它都像猜测游戏8)) - 在InitializeComponent中有classB方法。由于X属性没有DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)属性,因此它在InitializeComponent方法中使用空字符串序列化 - 因此擦除先前显式设置的值。

© www.soinside.com 2019 - 2024. All rights reserved.