WINFORM or WPF: how to trigger a custom event inside the constructor of the class that emits it

I have userControl11 (either in winform or wpf) that has a special ValueChanged event. If I put it in a client form and set its value to 100 in form_load, it will raise a ValueChanged event. But if I set this value inside the constructor of UserControl1, the user event will not be fired. How can I make it do this?

whatever the technical reason, functionally it makes sense. If an object initializes its value from some sources unknown to the client form, and the client form has a text field attached to this usercontrol value, it is of course convenient that it can update its text field at any time, including when the form is loaded using only one single event handler. Without this, the client form must create another initializer for this associated text field when the form loads.

Below is the source code of my tests in winform:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace test
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void userControl11_ValueChanged()
        {
            MessageBox.Show(userControl11.Value.ToString());
        }

        private void Form1_Load(object sender, EventArgs e)
        {
           // This will trigger ValueChanged Event
            userControl11.Value = 100;
        }
    }
}



using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;

namespace customevent
{
    [DefaultEvent("ValueChanged")]
    public partial class UserControl1 : UserControl
    {
        private int m_value;

        public delegate void ValueChangedHandler();
        [Category("Action")]
        [Description("Value changed.")]
        public event ValueChangedHandler ValueChanged;

        public int Value
        {
            get { return m_value; }
            set { 
                m_value = value;
                if (ValueChanged != null)
                {
                    ValueChanged();
                }
            }
        }

        public UserControl1()
        {
            InitializeComponent();
            // this won't trigger ValueChanged Event
            this.Value = 100;
        }
        public UserControl1(int iValue)
        {
            this.Value = iValue;
            InitializeComponent();
        }

    }
}
+3
source share
4 answers

Inside the class, you can use On On.

, , , . ( - calss, .)

public UserControl1(IEnumerable<Action> subscribers)   {

   this.OnValueChanged(new EventArgs());
   this.Value = 100;
}

, , EventArg. On - , . On , , .

Edit:

Event Design MSDN , OnEvent ( On). .

EventArgs , , , , MSDN .

, , EventArg.

+3

, . Form_Load , .

, , .

- ...

  public UserControl1(IEnumerable<Action> subscribers)
  {
        InitializeComponent();

        this.ValueChanged += () => 
           {
               foreach(var subcriber in subscribers)
                    subscriber();
           } 

        this.Value = 100;
  }
+3

. . , , , . , , . , m_value.

, , . userControl11_ValueChanged(), , .

If you want to always generate an event with good chances that the subscriber sees, then assign a property to the UserControl OnLoad (or Load event) method override.

+1
source

Pausing events in the constructor does not really make sense (unless it is a static event): since the object is not yet fully initialized, no other object has been able to subscribe to the event yet ...

0
source

Source: https://habr.com/ru/post/1781087/


All Articles