简体   繁体   中英

Properties changing in a custom control

链接到图片

I've created a custom control for easy addition as rows to a TableLayoutPanel . One entire row as seen in the image is a single instance of the control. I've to implement some functionality so that whenever rate and quantity are filled, the (complete) change should cause a sum += (rate * quantity) in the Total box.

What I was thinking of was something on the lines of a OnPropertyChanged event handler inside ProductControl.cs , which is the class for the custom control. I surmise that I would need two property change handlers, one for Rate and one for Quantity . Each would check whether the other field is empty or not, and proceed to update a Product = Rate*Quantity value.

However, how would I access these handlers in the main form ? I need to update the Total box as Total += (Rate*Quantity) or Total += Product .

Source for ProductControl.cs -:

public partial class ProductControl : UserControl
{
        Dictionary<int, PairClass<string, double>> PC = new Dictionary<int, PairClass<string, double>>();
        public string Number
        {
            get
            {
                return ProdNo.Text;
            }
            set
            {
                ProdNo.Text = value;
            }
        }
        public ProductControl()
        {
            InitializeComponent();
        }

        public void SetMapping(Dictionary<int, PairClass<string, double>> map)
        {
            PC = map;
        }

        //This implements the functionality that whenever a Product Number is entered,
        //the Product Name and Rate appear automatically, by virtue of a mapping
        //between Product Number: Pair<Product Name, Rate>.
        private void ProdNoText_TextChanged(object sender, EventArgs e)
        {
            int x = 0;
            PairClass<string, double> pc = null;
            if (int.TryParse(ProdNoText.Text, out x))
            {
                PC.TryGetValue(x, out pc);
                if (pc != null)
                {
                    PNameText.Text = pc.First;
                    RateText.Text = pc.Second.ToString();
                }
            }
            else
            {
            }
        }
}

You can create a custom event for your control like this:

You first create the class ProductChangedEventArgs derived from EventArgs and containing all the informations the main form will need to handle che product change (let's say Rate and Quantity ). This class only needs a constructor that accepts Rate and Quantity and two public getters.

Then, in your class:

// This will be the signature of your event handlers
public delegate void ChangedEventHandler(object sender, ProductChangedEventArgs e);
// The event itself to which will be possible to bind callbacks functions
//   with the signature given by ChangedEventHandler
public event ChangedEventHandler Changed;

protected virtual void OnChanged(ProductChangedEventArgs e) 
{
  // This checks there's at least one callback bound to the event
  if (Changed != null){
    // If there are callbacks, call all of them
    Changed(this, e);
  }
}

Now all you need to do, is call OnChanged in whichever point you want the event to be emitted from.

You will, for example, call it in the setters of all your product properties.

private int _rate;
public int Rate{
  set{
    if(_rate != value){
      _rate = value;
      // Call the callbacks passing an EventArgs that reflects the actual state
      //   of the product
      OnChanged(this, new ProductChangedEventArgs(_rate, ... ));
    }
  }
}

Now in the main form, you can bind callbacks to the Changed event like so:

productControl1.Changed += new ChangedEventHandler( callback );

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM