物业变化传递价值

时间:2016-09-25 05:41:48

标签: c# winforms

我有两种形式:表格A和表格B.我还有一个属性字段类。

表单A包含我想要在更改属性时更改的标签。表单B包含将更改属性字段的代码。

物业类代码:

 public class Controller
    {
        private static string _customerID;
        public static string customerID
        {
            get { return _customerID; }
            set
            {
                _customerID = value;
                if (_customerID != "")
                {
                    FormA.ChangeMe();
                }
            }
        }
    }

表格B代码:

    private void something_Click(object sender, SomethingEventArgs e) {
     Controller.customerID = "Cool";
}

表格A代码:

public static void ChangeMe()
        {
            var frmA = new FormA();
            MessageBox.Show("Test: " + Controller.customerID); //This works! Shows Cool
            frmA.lb2Change.Text = Controller.customerID; //This kind of works..
            MessageBox.Show("Test2: " + frmA.lb2Change.Text); //This shows the correct value. Shows Cool
        }

传递属性字段值(我从MessageBox中知道)但是它不会更新表单标签本身的值。为什么是这样?我究竟做错了什么?我也相信有更好的替代方法可以实现预期实现的ChangeMe()方法 - 如果有任何建议的话?

2 个答案:

答案 0 :(得分:1)

您可以执行以下操作

  1. 定义代理
  2. 实施财产变更通知
  3. <强>代表

    public delegate void OnCustomerIDChanging(object sender,CancelEventArgs e);
    public delegate void OnCustomerIDChanged(object sender,object value);
    public class Controller
    {
        private static string _customerID;
        public event OnCustomerIDChanging CustoerIDChanging;
        public event OnCustomerIDChanged CustoerIDChanged;
        public static string customerID
        {
            get { return _customerID; }
            set
            {
               // make sure that the value has a `value` and different from `_customerID`
               if(!string.IsNullOrEmpty(value) && _customerID!=value)
               {
                   if(CustomerIDChanging!=null)
                   {
                        var state = new CancelEventArgs();
                        // raise the event before changing and your code might reject the changes maybe due to violation of validation rule or something else
                        CustomerIDChanging(this,state);
                        // check if the code was not cancelled by the event from the from A
                        if(!state.Cancel)
                        {
                             // change the value and raise the event Changed
                             _customerID = value;
                             if(CustomerIDChanged!=null)
                                 CustomerIDChanged(this,value);
                        }
                   }
               }
            }
        }
    }
    
    在表单中以及启动控制器对象时

    var controller = new Controller();
    controller.CustomerIDChanging +=(sd,args) =>{
         // here you can test if you want really to change the value or not
         // in case you want to reject the changes you can apply 
         args.Cancel = true;
    };
    controller.CustomerIDChanged +=(sd,args) =>{
    
         // here you implement the code **Changed already**
    }
    
      

    上面的代码可以让您很好地控制代码,也可以使您的控制器代码可重用和清理。相同   您可以通过实施INotifyPropertyChanged接口

    来获得结果

    <强> INotifyPropertyChanged的

    您可以查看此article以获取更多信息

答案 1 :(得分:0)

在静态方法ChangeMe中,每次都要创建一个新表单,要更改值。而不是你想要改变现有表格的价值。因此,您的Controller需要此FormA的实例。试试这样:

public class Controller
{
    //You can pass the form throught the constructor,
    //create it in constructor, ...
    private FormA frmA;

    private string _customerID;
    public string customerID
    {
        get { return _customerID; }
        set
        {
            _customerID = value;
            if (_customerID != "")
            {
                frmA.ChangeMe();
            }
        }
    }
}

现在您不需要在FormA

中保持静态
public void ChangeMe()
{
    MessageBox.Show("Test: " + Controller.customerID);
    this.lb2Change.Text = Controller.customerID;
}
相关问题